Bird
Raised Fist0
HLDsystem_design~12 mins

CQRS (Command Query Responsibility Segregation) in HLD - Architecture Diagram

Choose your learning style9 modes available
System Overview - CQRS (Command Query Responsibility Segregation)

The CQRS system separates the operations that change data (commands) from the operations that read data (queries). This helps improve scalability and performance by allowing each side to be optimized independently. Commands update the write database, while queries read from a separate read database that is kept in sync asynchronously.

Architecture Diagram
User
  |
  v
Load Balancer
  |
  v
API Gateway
  |
  +-------------------+
  |                   |
Command Service    Query Service
  |                   |
Write Database     Read Database
  |                   |
  +--------+----------+
           |
    Event Bus / Message Queue
           |
       Event Handler
           |
       Read DB Updater
           |
        Cache Layer
Components
User
actor
Initiates commands and queries
Load Balancer
load_balancer
Distributes incoming requests evenly to API Gateway instances
API Gateway
api_gateway
Routes commands to Command Service and queries to Query Service
Command Service
service
Handles commands that modify data in the write database
Query Service
service
Handles queries by reading data from the read database
Write Database
database
Stores the authoritative data for writes
Read Database
database
Stores data optimized for queries, updated asynchronously
Event Bus / Message Queue
message_queue
Transfers events from Command Service to update the read database
Event Handler
service
Processes events from the queue to update the read database
Read DB Updater
service
Applies changes to the read database based on events
Cache Layer
cache
Speeds up query responses by caching frequent read data
Request Flow - 15 Hops
UserLoad Balancer
Load BalancerAPI Gateway
API GatewayCommand Service
Command ServiceWrite Database
Command ServiceEvent Bus / Message Queue
Event Bus / Message QueueEvent Handler
Event HandlerRead DB Updater
API GatewayQuery Service
Query ServiceCache Layer
Cache LayerQuery Service
Query ServiceRead Database
Read DatabaseQuery Service
Query ServiceAPI Gateway
API GatewayLoad Balancer
Load BalancerUser
Failure Scenario
Component Fails:Write Database
Impact:Commands cannot be persisted, so data updates fail. Queries still work from read database but data becomes stale.
Mitigation:Use database replication and failover to a standby write database. Queue commands temporarily if possible until DB recovers.
Architecture Quiz - 3 Questions
Test your understanding
Which component handles data modification requests in CQRS?
AQuery Service
BCommand Service
CCache Layer
DEvent Handler
Design Principle
CQRS separates commands and queries to optimize each independently. This improves scalability and performance by allowing writes and reads to use different data stores and update mechanisms, reducing contention and enabling asynchronous data synchronization.