liveBook · Manning

Replication lag

As explained in Microservices Patterns, Second Edition

A delay between a change being made on one side of a replicated system and becoming visible on another.

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.
EventualConsistencyToken

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

How Microservices Patterns, Second Edition covers it

How deep it goes
Details token-based detection and retry, including event-ID and aggregate-version implementations, alongside a local UI alternative.
Examples
Illustrates a mobile or single-page application updating its local model after a successful command.
What you can do after
Apply token checks and retries to detect whether a CQRS view reflects a command’s update.
How it connects
Connects CQRS and eventual consistency with idempotency, event ordering, and consumer lag.

Also explained in

Compare how 6 books explain replication lag →

Related concepts

This explainer was generated by Manning's knowledge graph from the text of Microservices Patterns, Second Edition. The section links point to the parts of the book it draws on.