Video in progress

When is introducing CQRS justified, and when is it just unnecessary complexity?

CQRS is useful when the read and write load or shape differ sharply, or when report queries are wrecking a complex domain model. In plain CRUD, and with a strict "I wrote it, I read it right away" requirement, it only adds eventual consistency problems.

📖 Answer

CQRS has several levels, and often the first is enough:

  1. Separation at the code level. Commands go through the aggregate, while queries return DTOs directly with SQL/projections. One DB. This is almost free and useful in many projects.
// write: through the domain
orderService.cancel(new CancelOrder(orderId, reason));

// read: bypasses the domain, in the shape you need
@Query("select new com.shop.OrderRow(o.id, o.status, c.name, o.total) " +
       "from Order o join o.customer c where o.createdAt > :from")
List<OrderRow> recent(Instant from);
  1. Separate read model tables: updated by events, denormalized.
  2. Separate storage: writes go to PostgreSQL, reads to Elasticsearch or ClickHouse.

Cases where levels 2–3 are justified:

  • reads outnumber writes 100 to 1 and need complex filtering/search;
  • reports are slowing down the main DB;
  • the read side belongs to another team or another technology.

The cost:

  • eventual consistency: a user places an order and doesn't see it in the list. Solutions are showing it optimistically in the UI, waiting with a version after the write, or reading that user's own data from the write DB;
  • you need a mechanism to rebuild projections;
  • monitoring: how far the read model is lagging (a lag metric).

A common interview mistake: thinking CQRS and event sourcing are a mandatory package. They are independent: you can have CQRS without event sourcing, and event sourcing without CQRS.