CQRS
Separate writes from read models when their needs diverge.
The problem and the pattern
A reservation command checks capacity and creates a booking. A dashboard query needs a tenant summary optimized for reading. CQRS separates these responsibilities; it does not require separate databases, an event bus, or event sourcing. Start with distinct handlers and measure before adding projection infrastructure.
Follow the flow
Make it concrete
This excerpt isolates the concept. Connect it to the booking API as you work through the exercise.
public sealed record ReserveBookingCommand(string TenantId, int Seats);
public sealed record TenantBookingSummary(int BookingCount, int Seats);
// Command enforces invariants. Query projects the dashboard view.Trade-offs
- Different models can simplify complex reads and writes.
- Asynchronous projections add lag, replay, and monitoring work.
- CQRS and event sourcing are independent decisions.
When to avoid it
Avoid separate projection infrastructure for simple CRUD when a direct query already meets the requirement.
Your turn
A newly created booking does not appear in an asynchronous dashboard immediately. What contract must the UI understand?
Reveal a worked answer
The projection is eventually consistent. Return a receipt for the accepted write, explain the dashboard freshness, and monitor projection lag. Do not imply a successful write was lost merely because a read model has not caught up.