Monday, 20 May 2013

Command and Query Responsibility Segregation (CQRS)

Recently, I needed to design a new application with a non-functional requirement to have a good performance in searches and viewing the page results. So, it was mostly about the "Read".
I have been using Domain Driven Design or DDD for transactional applications for a while and my feeling was that this is not what I need this time exactly.
The main reason was the complicated domain models with several child objects and collections and so many mappings from database to the domain model and then to DTOs and finally to view models.
Domain models are the heart of any DDD applications, and I needed DTOs for my distributed fa├žade and them view models for the MVC part.
The answer for my new application was CQRS or Command Query Responsibility Segregation pattern:
"At its heart it is a simple notion that you can use a different model to update information than the model you use to read information."
Martin Fowler
Please note that it is not against DDD while you still need or can use DDD with it. When we say "CQRS" this often means: "CQRS Principle" + "Message-driven architecture with commands, events and queries" + "Domain-Driven Design Methodology".

I am not going to define it here or show you different diagrams or different aspects of it in this post. Instead, the main purpose of this post is to share some good sources that I have used to learn it and implement it into a way that worked for my application.

CQRS by Martin Fowler

CQRS Starting Page : This is a very complete reference page for CQRS by Rinat Abdullin which you can find sample implementations too.

Top 10 Reasons to do CQRS

CQRS Journey: This is from Microsoft patterns & practices

Clarified CQRS

Introduction to CQRS: a good article and easy to understand

DDD/CQRS:  A discussion group on Google

I am sure that you can find better sources, but these are what I liked :)

Happy CQRS!