CQRS

Use one model to change data and a separate, read-optimised model to query it, so each side can be designed and scaled for its own job.

Description

Most systems use one model for everything: the same tables serve writes with all their rules and reads with all their joins. CQRS splits them. Commands (place order, change address) go through a write model that enforces business rules. Queries read from a separate read model shaped exactly like the screens and reports that need it.

The read model is updated from the write side, often asynchronously through events, so reads are eventually consistent. In return, reads can be denormalised, cached and scaled independently, and the write model stays focused on correctness.

System design

PlaceOrder1save2OrderPlaced3update views4GET my orders5one fast lookup6Web appCommand APIvalidates, enforces rulesWrite DBnormalisedChange eventsQuery APIRead modeldenormalised views

CQRS system design diagram

  1. Web app (client).
  2. Command API (service): validates, enforces rules.
  3. Write DB (database): normalised.
  4. Change events (topic).
  5. Query API (service).
  6. Read model (database): denormalised views.
  7. Step 1: Web app → Command API: PlaceOrder.
  8. Step 2: Command API → Write DB: save.
  9. Step 3: Write DB → Change events: OrderPlaced.
  10. Step 4: Change events → Read model: update views.
  11. Step 5: Web app → Query API: GET my orders.
  12. Step 6: Query API → Read model: one fast lookup.
  1. The user places an order: a command.
  2. The command side checks the business rules and saves to the write database.
  3. The change is published as an event.
  4. A projector updates the read model, such as an "orders by customer" view with everything the screen needs. This happens moments later, not instantly.
  5. The user opens their order history: a query.
  6. The query side answers with one lookup in the read model, with no joins and no business logic.
AWS
Aurora or DynamoDB for writes; DynamoDB Streams or EventBridge to OpenSearch, DynamoDB tables or Redshift for reads.
Azure
Azure SQL or Cosmos DB for writes; change feed into Cosmos DB containers, Azure AI Search or Synapse for reads.
Google Cloud
Cloud SQL, Spanner or Firestore for writes; Pub/Sub into BigQuery, Firestore or Elasticsearch for reads.

When to use it

Use it when

  • Read and write needs are very different: complex validation on writes, many differently shaped views on reads.
  • Reads vastly outnumber writes and need to scale independently.
  • You're already event-driven, or using Event Sourcing, and need queryable views.
  • Different teams own the business logic and the reporting or search experiences.

Avoid it when

  • The domain is simple CRUD. CQRS adds a second model, synchronisation and eventual consistency for nothing.
  • Users must always read their own writes instantly and you can't design around the delay.
  • The team is small and the system is young. Start with one model and split later if the need appears.

Pitfalls

  • Users not seeing their change right after saving. Return the updated data from the command, or show a pending state.
  • Read models drifting out of sync after a bug. Make them rebuildable from the write side or the event log.
  • Applying CQRS to the whole system instead of the one area that needs it.
  • Two models doubling the work for every new field.

Alternatives

Materialized View

Choose it when you just need a few precomputed read views, not a separate command model.

Cache-Aside

Choose it when reads are slow but the model itself is fine; cache the hot items.

Read replicas

Choose it when you need more read capacity with the same schema; database replicas are the cheapest split.

Event Sourcing

Choose it when you also want the full history of changes. Often paired with CQRS.