January 13, 2021

Understanding the Architecture of Event-Based CQRS Design

In this article, I am going to explain the various components of a simple Event-based CQRS Architecture.


Take a look at Figure 1


Figure 1: Event-Based CQRS Architecture


Controller: This is also called the UI Controller. It is responsible for  intercepting both read and write operations comming from the client. So when you enter a url in your browser, and and hit <Enter>, the request immediately routes to the controller

Commands: Whatever request is coming from the controller could either be a command or a query.  A command represents  an intent to modify the state of an entity. This is normally in form of a DELETE, POST or PUT request

Command Gateway: A Command Gateway provides an interface for choosing how to dispatch your commands. Normally, the command coming from the controller ges to the CommandGateway. It could dispatch the command either synchronoously or asznchromously.

Command Handler:  A command handler receives and responds to specific type of commands. It then executes some business rule or logic based on the command. It retrieves the domain enities and make the neccesary changes on them.

Domain Entity: Domain Entities are aggregate entities that are modeled to reprsent the state of the components in the domain. An aggregate is an entity or group of entities that are always kept in a consistent state. For example, an Employee entity could be an aggregate. It could have additional aggregates like Address, NextOfKin etc. If the state of an aggregate changes, then a dmain event is generated.

Repository: A repository just as you may know provides functionality for managing aggregates. For example, a repository helps you find, save or delete an aggregate. A repository, could interface with a persistent storage(like a database) to store the aggregates. A repository could also use an event store to call up and aggregate and recreate it’s state at any point in history based on saved states.

Event Store: This is a storage of all the changes that have been applied to an entity. It is like a history of events. An event store can replay these historical changes and therefore rebuild the state of an entity at any point in time. Event store may also use a database for event storage.

Event: An event is a notification that there has been a change to the state of an entity. So when an update is made, then an event is emitted to say, what changes actually took place.

Event Bus: This is like a channel for generated events. They are normally backed up by a messaging topic in a publish/subscribe manner.

Event Handler: These are methods the receive events and handle the events. For example, when an event is emited for an update to an entity, then an event handler recieves this event from the event bus. It could then update the WriteDB by mirroring this changes to materialized views.

Query:  Think of a query as the opposite of a command. A query is a request for data without making and changes. When a query comes from the controller, it is routed to the query manager. Then the query manager manager executes the query and returns the results the to client.


I recommend you follow this tutorial on building an CQRS-based application.

0 0 vote
Article Rating
Notify of
Newest Most Voted
Inline Feedbacks
View all comments
1 month ago

I designed your diagram using LucidChart… See if you want to replace it 😉