Course contentsShow
← Software Engineering
Lesson 1353 of 1,93340. Event-Driven ArchitectureFree lesson

Eventual Consistency in CQRS

Manage the delay between write completion and read model updates, communicating consistency guarantees to users.

Eventual Consistency in CQRS

You'll learn how to manage the delay between command execution and updated query results, and why communicating this timing to users matters.

The Reality of Separate Models

In CQRS, your command side writes events to the event store immediately, but your query side (projections) updates asynchronously. This creates a window where a user completes a write operation successfully, yet subsequent reads don't reflect that change yet. This delay is eventual consistency — the guarantee that eventually all readers will see the update, just not instantly.

Understanding the Timing Gap

Think of updating your social media profile. You click "Save," see a success message, but when you refresh, the old photo briefly appears. A background worker is still processing your change across replicated databases. That's eventual consistency in action.

In CQRS:

  1. Command handler validates and stores events ✓
  2. Event store confirms success ✓
  3. Projection workers consume events and update read models (takes time...)
  4. User queries the read model (might still show old data)

The gap might be milliseconds or seconds, depending on message queue latency, projection complexity, and system load.

Communicating Consistency Guarantees

Don't leave users confused. Strategies include:

  • Optimistic UI updates: Show the expected result immediately in the interface
  • Status indicators: "Your changes are being processed"
  • Version/timestamp display: "Last updated 2 seconds ago"
  • Explicit messaging: "New items may take a moment to appear"

Some systems offer a correlation ID allowing users to poll: "Has event X been projected yet?"

Managing User Expectations

Document your consistency model. If your projection typically updates within 100ms, users planning integrations need to know they can't assume immediate read-after-write consistency. Design workflows accordingly — perhaps redirect to a "success" page rather than immediately re-querying.

Key Takeaway: Eventual consistency is inherent to CQRS; manage it by setting clear user expectations and providing feedback during the projection delay, rather than pretending reads are instantly consistent with writes.