Editorials

Resilience Patterns ? CQRS

CQRS (Command Query Responsibility Segregation, http://martinfowler.com/bliki/CQRS.html) is a technique that separates data creation and modification from data querying. In a pure CQRS model a stored procedure would not both insert/update data and then retrieve data. Those are two distinct operations, and therefore separated.

This is similar to the Materialized View discussion from yesterday. In Materialized Views we separated the creation of data from the retrieval of data by the use of a view for reporting. In CQRS this concept is extended to the entire database rather than a limited subset.

In most applications you want your Create/Update actions to be extremely fast. Usually it is acceptable for you read operations to be a little slower (we’re not talking minutes or days here necessarily, but sometimes that is even acceptable). When the read demands do not have the performance requirements of the write commands it makes sense to separate the two processes from a scalability and resilience perspective.

Ok, I’ll do want to acknowledge that CQRS can complicate your application infrastructure, and that it does not make sense to use this pattern in any and every application you ever build. This is a pattern you would use when you have requirements for Resilience or massive scale.

Amazon is a great example. You want your shopping cart and the final purchase process to be blazingly fast. But the search for products could be slower without impacting the user experience in a way they may even know about. You don’t have to know inventory to give an estimated ship date if inventory is not one hundred percent up to date.

Separation of the shopping cart, order data, inventory, and product listing is then extremely useful when it comes to the Resilience of Amazon. They may not have the latest product listing from their data store, but they do have what is in cache. Shopping cart data is probably maintained on the browser, and maybe in some sort of session cache, or user cache. Placing an order may be on a completely different system with lots of redundancy assuring the part of the system that brings you revenue is always available.

Have you ever had to write a system using CQRS? Why not share some of the benefits or hurdles you had to overcome? Leave your comments here online, or drop an Email to btaylor@sswug.org.

Cheers,

Ben