Sub topic 10
Living with Eventual Consistency
This chapter assumes you know basic database transactions, request/response APIs, and the publish/subscribe model from earlier chapters. It covers what eventual consistency means for a system built on events, and how to design for it.
What eventual consistency means
In a single database, a transaction makes a change visible to every reader at once. In an event-driven system, each service owns its own data. When one service publishes an event, the others update their copies later. For a short time, services disagree.
Eventual consistency is the guarantee that, if no new changes arrive, all copies of the data will converge to the same state. It does not say how long that takes. The delay is usually milliseconds, but a broker outage or a slow consumer can stretch it to minutes.
Where users notice it
The classic symptom is read-your-writes failure. A user submits a form, the page reloads, and the new item is missing because the read model has not yet processed the event. Other common symptoms:
- A dashboard total that lags behind the order list.
- Two services reporting different stock levels for a few seconds.
- A confirmation email sent before the payment record is visible elsewhere.
Design techniques
You cannot remove the delay, but you can make it harmless.
- Return what you know. After a write, return the created resource in the response. The client can show it without querying the read model.
- Show pending state. Display "Processing..." instead of pretending the change is complete.
- Read from the source of truth when it matters. For a user's own just-written data, query the owning service directly. Use the lagging read model for everything else.
- Carry versions. Include a version number or timestamp in events. A consumer can ignore an older event that arrives after a newer one.
- Poll or subscribe for completion. If a client must wait, give it a status endpoint or a push notification instead of a blocking call.
- Decide per business rule. Ask how stale the data can be before it causes harm. A product catalogue can lag by minutes. An account balance check before a withdrawal usually cannot.
# A read model that ignores stale events using a version number.
read_model = {}
def apply_order_event(event):
current = read_model.get(event["order_id"])
if current and current["version"] >= event["version"]:
return # older or duplicate event: skip
read_model[event["order_id"]] = {
"status": event["status"],
"version": event["version"],
}
apply_order_event({"order_id": "o-1", "status": "shipped", "version": 2})
apply_order_event({"order_id": "o-1", "status": "placed", "version": 1}) # arrives late
print(read_model["o-1"]["status"]) # shippedCommon errors
- Assuming events arrive instantly. Code that publishes and then immediately queries another service will fail intermittently, and the failures are hard to reproduce.
- Hiding the delay in tests. Tests that wait a fixed sleep are flaky. Poll with a timeout instead.
- No convergence check. If a consumer drops an event, the copies diverge permanently. Pair retries and dead-letter queues with periodic reconciliation that compares source and copy.
- Ignoring the business. Product owners must agree on acceptable staleness. Engineers cannot decide it alone.
Exercise
A user places an order and is redirected to "My orders", which reads from a search index updated by an order.placed event. The new order is sometimes missing. Name two ways to fix this without making the event path synchronous.
Show answer
Any two of these: return the created order from the write response and render it directly, show a "Processing" entry for the pending order, or read the user's recent orders from the order service (the source of truth) instead of the index. A version number does not fix the missing order, but it keeps stale events from overwriting newer data once they arrive.