CQRS - Command Query Request Segregation

Overs the years i have the idea of decoupling the query and actions on a resource and came up with examples where ever I could. Bit i was not able to articulate the same in terms of a pattern. I recently found there is a similar pattern and obviously a popular pattern in today microseconds world. This pattern is called CQRS(Command Query Request Segregation). Yes, I know, who would have come up with such a mouthful, but the underlying concept is pretty simple. This pattern specifies that all queries and command (actions that mutate state) should be segregated.

This means an API to access a resource should segregated the query and command interfaces. This would help the API consumer and producer in managing and consumer the api easily without worrying about unintended side effects.

Thuis pattern is particularly useful in a microseconds architecture. Also another pattern which gels well with CQRS is Event Sourcing.

Event Sourcing is the processing of mutating state via events rather than a direct synchronous manipulation of the resource. The main advantage of this pattern is that the mutator, producer, resource and the consumer can work within an asynchronous non bounded context.

This means a producer need not worry about a consumers and both of them van be decoupled from each other. Similarly multiple state changes can happen on a single resource without locking out the consumers.

These patterns are not a set of implementation patterns that magically make your API asynchronous and extensible but a set of design patterns which when impoliteness properly help your API to evolve without excessive coupling.

You can use messaging, micro services , service bus and other integration patterns to implement CQRS-EQ . Even before you dive into the details you should first access if this patterns applied to your current use case.

For example if you are working on a trading application with lite latency and string consistency requirements using event sourcing might not be the best pattern to implement (EQ can be implemented for low latency applications as well but they are better implanted as synchronous real time calls). A customer days management system can be implemented using CQRS-EQ and would make sense to implement it as a set of micro services serving different consumers in an enterprise.

I will add more examples on how to implement the same and other related patterns next time...

Comments

Popular posts from this blog

Understanding SNOMED-CT data model: Basics

Master Data Management in Financial sector

Why the role of IT in Financial Institutes is more important than ever