The Dynamic Duo: Event Sourcing and CQRS

Marat Miftakhov
3 min readJan 1, 2023

--

what is cqrs? what is event sourcing? command query responsibility segregation

What is event sourcing?

Event sourcing is a software design pattern that involves storing a log of events that describe changes to a system. These events can be used to reconstruct the current state of the system, as well as to track the history of changes made to the system.

What is CQRS?

CQRS, or Command Query Responsibility Segregation, is a pattern that separates the responsibility for issuing commands (which change the state of the system) from the responsibility for querying the state of the system. In a CQRS system, the command and query responsibilities are typically handled by separate components or microservices.

One way to implement CQRS with event sourcing is to have a separate event store for each microservice. Each microservice’s event store contains only the events that were produced by that microservice, and the events are used to reconstruct the current state of the microservice. Queries are then handled by a separate read model, which is built from the events in the event store and optimized for querying.

Example

Let’s take a look on a practical example of event sourcing and CQRS using a simple to-do list application:

  1. A user creates a new to-do list item by issuing a “CreateToDoItem” command.
  2. The command handler for the “CreateToDoItem” command generates a “ToDoItemCreated” event and stores it in the event store.
  3. The event store then sends the “ToDoItemCreated” event to any subscribed event handlers.
  4. One of the subscribed event handlers is a projection that updates the read model with the new to-do list item.
  5. The read model is then queried by the user interface to display the updated to-do list.

In this example, the command handler is responsible for issuing commands and updating the event store, while the read model is responsible for handling queries and displaying the current state of the to-do list to the user. The event store acts as a buffer between the command handler and the read model, allowing for a separation of concerns between the two.

Advantages

One advantage of this approach is that it allows for a separation of concerns between the write and read models, which can make the system easier to maintain and scale. It also allows for greater flexibility in the design of the read model, as it can be optimized for the specific queries that are needed by the system. For example, if the user interface needs to display the to-do list in a different format (e.g. as a calendar view), the read model can be modified to accommodate this without affecting the command handler or the event store. Similarly, if the logic for handling commands needs to be changed (e.g. to add validation), this can be done without affecting the read model or the user interface.

Another advantage of event sourcing is that it provides a complete history of the changes made to the system, which can be useful for debugging, auditing, and analysis purposes. It also allows for the possibility of undoing or redoing changes made to the system, which can be useful in certain scenarios.

Disadvantages

One disadvantage is complexity. Event sourcing and CQRS can introduce additional complexity to a system, as they involve maintaining multiple stores (the event store and the read model) and possibly multiple command and query handlers. This can make the system more difficult to understand and maintain, especially for developers who are not familiar with these patterns.

Another disadvantage is that the use of event sourcing and CQRS can potentially have an impact on the performance of a system, as there is added overhead for storing, processing, and querying events. In some cases, this overhead may be acceptable, but in other cases it may be necessary to optimize the system to ensure that it performs well.

Event sourcing and CQRS can be powerful tools for building scalable and maintainable systems. However you should carefully consider the trade-offs and potential disadvantages when deciding whether to use these patterns in your architecture.

--

--