Understanding CQRS with Real-Life Examples
1. CQRS in an Online Store (Real-Life Example)
In this diagram, we see an online store architecture applying CQRS:
-
API Gateway: The entry point for all client requests. It routes incoming calls to the right backend service.
-
Frontend Service: Connects to various backend services depending on the operation.
-
Products Service (Write Side): Responsible for updating product information (e.g., adding new products, changing prices, updating stock).
-
Products Search Service (Read Side): Maintains a denormalized, read-optimized view of product data so customers can quickly search and filter products.
-
Reviews Service: Handles user reviews separately, but it also feeds into the search service so that reviews can be displayed alongside products.
Notice that the Products Service and the Products Search Service do not share the same database. Instead, updates from the Products Service trigger events (via message queues or change notifications), and the Search Service builds a read-optimized view for fast queries.
This ensures that the online store can handle heavy read loads (product searches) efficiently, without slowing down the write operations (stock updates, price changes).
2. Separation of Update and Read Operations
The second diagram illustrates the core principle of CQRS — separating reads and writes:
-
Service A (Write Side): Handles update operations. It writes to a write-optimized database, which is usually normalized to enforce business rules and consistency.
-
Service B (Read Side): Handles read operations. It queries a read-optimized database, often denormalized for speed and tailored to the queries the application needs.
-
Message Broker: Acts as a bridge between the two sides. When Service A makes updates, it sends events/messages to the broker. Service B consumes these messages to keep its read database in sync with the latest state.
This pattern allows the system to scale efficiently:
-
Reads (which usually account for 80–90% of traffic) can be scaled independently.
-
Writes can remain strict and consistent without slowing down read queries.
Why CQRS Matters
By splitting reads and writes, CQRS provides:
-
Performance: Queries become faster thanks to read-optimized models.
-
Scalability: Read and write workloads can scale separately.
-
Flexibility: Different databases can be used for different purposes (e.g., SQL for writes, NoSQL for reads).
-
Event-driven architecture: Easily integrates with event sourcing and message brokers.
Of course, CQRS also adds complexity. It’s most valuable in large-scale, high-performance systems, such as e-commerce, financial services, and IoT platforms.
✅ In short:
The first image shows a real-world e-commerce implementation of CQRS, where product updates and searches are handled by separate services.
The second image illustrates the theoretical principle — how updates and reads use separate services, databases, and a message broker to stay in sync.


Comments
Post a Comment