SkillSprout
Chapter 30 · Lesson 1
Advanced
4 min read

CQRS: Separating Reads and Writes

Overview

Most systems use one data model for both saving and showing data. As an app grows, the needs of reading and writing pull in opposite directions. CQRS solves this by giving each side its own model, and often its own database.

Detailed Notes

What is CQRS

CQRS stands for Command Query Responsibility Segregation. It is a fancy name for a simple idea: use different models for changing data and for reading data.

  • A command asks the system to change something: "place order", "cancel order". It can be accepted or rejected, and it returns little or nothing.
  • A query asks a question: "show my orders". It never changes anything.

In CQRS the write side handles commands and the read side handles queries. Each has its own code, and often its own database.

The problem it solves

One database table for everything works at first. Later the two jobs clash:

  • Writes need strict rules (validation, no overselling stock) and a tidy, normalised structure.
  • Reads need speed and convenience: a product page may join data from five tables, and a dashboard may need totals.
  • Most systems read far more than they write, say 100 reads for each write, so you want to scale reads independently (see Scalability).
  • Adding indexes makes reads faster but slows writes, so one schema is always a compromise.

How it works

  1. The client sends a command to the write side.
  2. The write side validates it and saves the change in the write database, which is tuned for correctness.
  3. The change is announced as a message through a broker (see Messaging Systems), for example "order placed".
  4. A small projector consumes these messages and updates the read database, a copy already shaped like the screens that need it (a read model). It could be a document store, a search index or a cache.
  5. Queries go straight to the read database, with no joins and no business rules.

Because step 4 happens after step 2, the read side is briefly behind. This is eventual consistency: a user may save something and not see it for a moment. Common fixes are showing the saved value straight from the client, or reading from the write side for the user's own latest change.

Projectors should be idempotent, since a broker may deliver a message twice. You can also run several read models from the same changes, one per use case.

A simple example

A restaurant has a kitchen and a menu board. Taking an order (command) goes to the kitchen, which follows strict rules. Customers read the menu board (query), a simplified copy that is fast to read and updated when the kitchen changes things. If the board lags by a minute, that is eventual consistency.

System design example

An online shop has an orders service. Commands go to a relational database that enforces stock and payment rules. Each saved order publishes an "order placed" message. A projector builds a read model in a search index, shaped as "customer order history", so the "My orders" page is one fast lookup. During a sale, you add more read replicas and projectors without touching the write side. CQRS adds cost, so use it only where the read and write needs really differ; a simple CRUD app does not need it.

Summary

  • Commands change data; queries read it. CQRS gives each its own model.
  • It lets you tune and scale reads and writes separately.
  • A projector keeps the read model updated from the write side's changes.
  • The price is eventual consistency and more moving parts.
  • Use it for read-heavy or complex domains, not simple apps.

Architecture Diagram

CQRS: separate write and read sides
Architecture
Client
Write sidevalidates rules
Brokerchange messages
Read databaseshaped for screens

Commands update the write database; a projector keeps the read database in sync for queries.

  1. 1A command reaches the write side, which validates and saves it.
  2. 2The change is published as a message.
  3. 3A projector updates the read database.
  4. 4Queries read from the read database.

Common Interview Questions

Finished this lesson?

0 of 91 lessons done · 0% of the course