What if splitting your system's tasks could make it faster and easier to fix problems?
Why CQRS (Command Query Responsibility Segregation) in HLD? - Purpose & Use Cases
Start learning this pattern below
Jump into concepts and practice - no test required
Imagine a busy restaurant where the same waiter takes orders, serves food, and handles payments all at once. When many customers arrive, the waiter gets overwhelmed, causing delays and mistakes.
Doing everything in one place slows down the process. Mixing order-taking and payment handling leads to confusion and errors. It becomes hard to scale when more customers come, and fixing problems takes longer.
CQRS splits the work into two parts: one team handles commands (like placing orders), and another handles queries (like checking the menu or bill). This separation makes each part faster, simpler, and easier to manage.
function handleRequest(request) {
if (request.type === 'read') {
return readData();
} else {
return writeData();
}
}function handleCommand(command) {
return writeData();
}
function handleQuery(query) {
return readData();
}It enables systems to handle many users smoothly by separating reading and writing tasks, improving speed and reliability.
Online shopping sites use CQRS to let many customers browse products quickly while processing orders and payments separately without slowing down.
CQRS separates commands (writes) from queries (reads).
This separation improves performance and scalability.
It reduces errors by simplifying each part's responsibility.
Practice
CQRS in system design?Solution
Step 1: Understand CQRS concept
CQRS stands for Command Query Responsibility Segregation, which means separating commands (writes) from queries (reads).Step 2: Identify the main benefit
This separation allows each part to be optimized and scaled independently, improving performance and maintainability.Final Answer:
To separate read and write operations for better scalability -> Option CQuick Check:
CQRS = Separate reads and writes [OK]
- Thinking CQRS combines operations into one service
- Confusing CQRS with security encryption
- Assuming CQRS reduces server count directly
Solution
Step 1: Define Command role in CQRS
Commands are responsible for write operations that modify the system's state.Step 2: Differentiate from Query
Queries only read data without changing it, so they are not commands.Final Answer:
It processes write operations that change system state -> Option AQuick Check:
Command = Write operations [OK]
- Confusing commands with queries
- Thinking commands handle caching
- Assuming commands manage security
Solution
Step 1: Understand asynchronous update in CQRS
In CQRS, the read side is often updated asynchronously via events after the write completes.Step 2: Identify read side behavior after write
Because of this delay, the read side may temporarily show stale data until it receives the update event.Final Answer:
The read side may show the old email briefly due to asynchronous update -> Option DQuick Check:
Read side updates asynchronously = possible stale data [OK]
- Assuming immediate read consistency
- Thinking reads block until writes finish
- Believing read data is deleted during update
Solution
Step 1: Identify how read model updates in CQRS
The read model updates via events sent by the command handler after state changes.Step 2: Diagnose missing updates
If the read model is not updating, likely the events are not being sent or processed properly.Final Answer:
The command handler is not sending events to update the read model -> Option BQuick Check:
Missing events cause read model stale data [OK]
- Blaming database corruption without evidence
- Confusing query side roles
- Assuming synchronous updates cause deadlocks here
Solution
Step 1: Understand scalability needs in CQRS
Separating reads and writes allows scaling read replicas independently to handle high traffic.Step 2: Use event sourcing for asynchronous updates
Event sourcing helps keep read models updated asynchronously, balancing freshness and availability.Step 3: Evaluate other options
Single database limits scalability; synchronous updates reduce availability; disabling caching hurts performance.Final Answer:
Use event sourcing to asynchronously update read models and deploy multiple read replicas -> Option AQuick Check:
Event sourcing + read replicas = scalable, fresh reads [OK]
- Using single DB limits scalability
- Synchronous updates reduce availability
- Disabling cache hurts performance
