← Lessons

quiz vs the machine

Gold1440

System Design

Eventual Consistency in CQRS

Why the read side lags the write side and how to design around the gap.

5 min read · core · beat Gold to climb

The lag is built in

In CQRS the command side writes, then events flow to projections that update read models. There is a moment between a successful write and the read model reflecting it. During that window a query may return stale data. This is eventual consistency.

Why it happens

  • The write commits first.
  • An event is published and delivered asynchronously.
  • A projection processes it and updates the view.

Each hop adds delay, usually small but never zero.

Designing around it

  • Set expectations in the interface, such as showing your change is processing.
  • Read your own writes by returning the new value from the command response or reading from the write side briefly.
  • Version or timestamp read models so a client can tell if its update has landed.

Key idea

CQRS read models update asynchronously, so accept a small staleness window and design the interface to handle reads that have not caught up yet.

Check yourself

Answer to earn rating on the learn ladder.

1. Why can a CQRS query return stale data right after a write?

2. Which technique helps a user see their own change immediately?