Video in progress

What is CQRS, and when does separating the read model from the write model pay off?

CQRS means separate models for writes (commands) and reads (queries), sometimes separate databases. The write model holds the business rules, while the read model is denormalized and ready for the screen. It pays off when the shape of reads and writes differ a lot.

📖 Answer

In plain CRUD, one entity is both written and read. The trouble starts when the screens get complicated: the "order list" page joins 7 tables, with filters, sorting and statistics. The write model, meanwhile, is normalized and shaped around invariants.

With CQRS:

POST /orders  -> OrderCommandService -> orders, order_items (normalized schema)
                        |
                        v  event: OrderPlaced
               OrderViewProjector -> order_list_view (one wide table or Elasticsearch)
GET /orders   -> straight from order_list_view

The read side has no joins, has the indexes it needs and scales separately.

The cost is clear: if the read model is updated asynchronously, it lags a little — a user may place an order and not see it in the list right away. You have to handle this in the UI (show the new record locally, or read your own write from the write model). On top of that, two models, a projector and a rebuild mechanism — there's more code.

I apply CQRS not to the whole system but to the exact spot that hurts. There's a small variant too: a materialized view or a separate "read" table inside one DB — that's the CQRS idea as well, just cheaper. CQRS doesn't require event sourcing; they're often mentioned together, but they're independent.