CQRS and eventual consistency
CQRS and eventual consistency
CQRS separates commands that change authoritative state from queries that read a representation optimized for retrieval. Separate databases, asynchronous projections and event sourcing are optional choices, not definitions of CQRS.
In an inventory system, the write side checks stock and commits reservations. A read model may list remaining stock for browsing, but a delayed projection cannot authorize reservations. With one database, commands and queries may use different code paths while sharing a transactionally consistent store. With asynchronous projections, eventual consistency means replicas may converge when updates stop and processing succeeds; it is not a fixed-time guarantee. Include versions or sequence numbers, projection checkpoints, lag metrics and a recovery plan. An absolute snapshot can replace older snapshots by version; equal versions should describe the same logical state. A conflicting payload at an equal version is a contract violation to investigate, not another harmless duplicate. A delta requires ordered, non-duplicated application or recovery of gaps. Persist the projection checkpoint together with the applied change when recovery depends on it. A transactional outbox records the state change and publish intent together; an inbox/deduplication strategy handles retries. Event sourcing makes stored events authoritative for reconstruction; CQRS does not require that. Read-your-writes can be handled with a pending indicator or a query for authoritative state. These examples use deterministic in-memory state, not durable messaging.
Baseline: .NET 10 / C# 14. All C# results below are reasoned, not executed. Offline models and the separate Aspire lab are explicitly distinguished.
Separate command authority from a stale read
The command side has two units; the query projection still reports five. Try to reserve three.
Use the archived Fixture.csproj with an approved .NET 10 SDK. No third-party packages are required. Inspect the project, then build and run with the approved SDK.
using System;int authoritative = 2, projected = 5;bool accepted = 3 <= authoritative;if (accepted) authoritative -= 3;Console.WriteLine($"display={projected}; accepted={accepted}; remaining={authoritative}");projected = authoritative;Console.WriteLine($"after refresh={projected}");Expected result (not executed):
display=5; accepted=False; remaining=2after refresh=2The read-side display is informative, not permission to commit. A real write must atomically validate and update under concurrency.
Apply versioned absolute snapshots
Deliver versions 2, 1, 2 and 3. Apply only strictly newer snapshots.
Use the archived Fixture.csproj with an approved .NET 10 SDK. No third-party packages are required. Inspect the project, then build and run with the approved SDK.
using System;int version = 0, stock = 5;foreach (var snapshot in new[] { (2, 3), (1, 4), (2, 3), (3, 1) }) { if (snapshot.Item1 > version) { version = snapshot.Item1; stock = snapshot.Item2; } Console.WriteLine($"v{version}: {stock}");}Expected result (not executed):
v2: 3v2: 3v2: 3v3: 1Skipping old snapshots is valid only because each includes complete stock state for this identity. Delta events must not use this shortcut.
Reject a delta gap without corrupting state
A delta projection refuses v3 until v2 has been applied. Re-delivery after recovery is explicit.
Use the archived Fixture.csproj with an approved .NET 10 SDK. No third-party packages are required. Inspect the project, then build and run with the approved SDK.
using System;int version = 1, stock = 5;foreach (var change in new[] { (3, -1), (2, -2), (3, -1), (3, -1) }) { if (change.Item1 <= version) Console.WriteLine("duplicate"); else if (change.Item1 != version + 1) Console.WriteLine("gap"); else { stock += change.Item2; version++; Console.WriteLine($"v{version}: {stock}"); }}Expected result (not executed):
gapv2: 3v3: 2duplicateIn a real projection, persist the checkpoint with state and request missing events or rebuild from a trusted source. The toy schedule explicitly redelivers the skipped event.
Apply the concept
Design a read model for orders. Start at v4 with 8 units, deliver delta v6 (-2), then v5 (-3), then v6 again. Specify how the consumer reports lag.
Use the manual practice and knowledge check. Do not deploy or publish these fixtures.
Check Your Understanding
Solidify your learning with the interactive assessment directly below this lesson.
Concept Quiz & Questions
CQRS and eventual consistency — knowledge check
Apply the lesson concepts to the following scenarios.
Does CQRS require event sourcing?
Which model approves a reservation?
Why may a versioned absolute snapshot skip an older version?
What should a delta consumer do when it sees a gap?
What should commit atomically in a durable projection?