CQRS
Command Query Responsibility Segregation splits the write model from the read model, letting each be optimized independently and often synced via events.
By Ratish Jain · Updated
Definition
CQRS (Command Query Responsibility Segregation) separates the model that mutates state (the *command* side) from the model that serves reads (the *query* side). Instead of one schema serving both, you maintain a write store optimized for consistency and a read store optimized for query shape.
The two sides are typically kept in sync asynchronously: commands emit events or change-data-capture streams that a projector applies to build denormalized read views.
The Problem It Solves
In read-heavy systems, a single normalized schema forces expensive joins on every query and couples read scaling to write scaling. Complex domains also suffer when write validation logic bleeds into read paths.
CQRS lets you scale reads independently (many read replicas or purpose-built stores), shape data per query without touching the write model, and keep the write side small and consistency-focused.
How It Works
- 1A client sends a command (e.g.
PlaceOrder) to the write side, which validates business rules. - 2The command mutates the authoritative write store in a local transaction.
- 3The change is published as an event or captured via CDC/outbox.
- 4A projector consumes the event and updates one or more denormalized read stores.
- 5Query requests hit the read store directly, returning pre-shaped data with no joins.
- 6Read models can be rebuilt at any time by replaying the event stream.
Eventual consistency
Because projection is async, a read immediately after a write may return stale data. Design UIs for this (optimistic updates, read-your-writes via the command response) rather than assuming instant visibility.
Tradeoffs
- Reads and writes scale and evolve independently.
- Read stores are denormalized for fast, join-free queries.
- Write model stays focused on invariants and validation.
- Pairs naturally with event sourcing and multiple read views.
- Significant added complexity and moving parts.
- Eventual consistency between write and read sides.
- Duplicated data and projection code to maintain.
- Overkill for simple CRUD domains.
Interview Questions
No. CQRS only mandates separate read and write models. You can sync them via CDC, the outbox pattern, or dual writes. Event sourcing is a common but optional partner that makes rebuilding read models trivial.
Return the new state directly in the command's response, use client-side optimistic updates, or route read-your-writes traffic to the write store. Pure query-side reads accept a small lag window.
- →Command side = writes/validation; query side = optimized reads.
- →Read stores are denormalized and often rebuilt by replaying events.
- →Sync is usually async → eventual consistency.
- →Great for read-heavy, complex domains; avoid for simple CRUD.
- →Event sourcing is optional, not required.
Related
Everything on Archprimer is connected — follow the graph.