Tuesday 26 July 2011

CQRS Queries (aka the read side)

Commands mutate state and trigger events which describe the changes which have occurred inside the write store. It is the responsibility of the read store to subscribe to these change events and update their read store appropriately (using event handlers).

All application queries from end users are made to the read store, which can be anything from a relational database to XML files. This data store is simply a thin read layer that's heavily optimised for reads. Each application's data view (think view models in MVC), typically maps to denormalised data tables rather than using joins (which slow down queries and prohibit scaling without headaches).

That's it, in a nutshell! The read side is a lot simpler than the write side of CQRS and something developers are more accustomed to. Working with denormalised data rather than third normal form makes queries quick and very easy to maintain; something for the junior devs :)

Consistency is over rated!!!

In a CQRS system, making a write does not immediately change what is read.

This is why CQRS systems are described as offering eventual consistency. CQRS evangelists question the importance of real-time consistency. They argue that by the time end users see data it is already stale. FOr example, requesting a data object via a web app results in a series of delays before the data can be displayed to the end user and hence is to a degree stale i.e. retrieving data from the data store, populating a DTO, returning data in a web response, page rendering in a browser.

I think I agree and if this is an issue for you, I suggest you question the importance of real-time consistency for your business. I'd argue that there is a time period/delta that for which in-consistencies can be tollerated and hidden from the end users. However, I do think that efforts should be made to keep this to a minimum and perhaps this delta should be actively monitored, especially for systems where it matters (e.g. a message system with high SLAs).

No comments:

Post a Comment