Replication lag is the delay between a change on one side of a replicated system and its visibility on another. In CQRS, an event takes time to travel from the command side to the query side and update the view. A client that writes an aggregate and immediately queries the view can therefore see its previous version. section 13.2.3
Replication lag cannot be eliminated, but clients can detect whether a view has caught up. A command returns a token representing its update; the client passes the token to a query. If the view does not yet reflect the update, the query returns an error and the client can retry. section 13.3.3
A token can identify the update in two ways:
- Event ID: the view records processed event IDs, and the query checks whether the command’s event has been processed. This can reuse tracking needed for idempotency. section 13.3.3
- Aggregate versions: provider events carry aggregate versions, and the view records the latest version applied for each aggregate. The query checks that the view has reached at least the versions in the token. section 13.3.3
Figure 13.10 One solution for replication lag is for a command to return a token - event ID or aggregate version - representing its update. The view uses the token to verify that it’s up to date with respect to the command.
The token flow connects a command’s update to the view’s check for whether that update is visible. section 13.3.3
Updating a local UI model
A mobile or single-page application can update its local model when a command succeeds, using data returned by the command instead of immediately querying the view. The view will usually have caught up by the next user-triggered query. This approach may require the UI to duplicate server-side model-update logic. section 13.2.3
Monitor consumer lag—the number of events a view has yet to process—and end-to-end latency from a provider’s commit to the view reflecting it. Monitoring and recovery also matter because an event-handler defect can leave a view stale or incorrect while it continues answering queries. Replication lag is distinct from inconsistencies caused by events arriving out of order across channels. section 13.2.3 section 13.3.3





