Sub topic 8
Living with eventual consistency
This chapter assumes you know the sync pipeline from the previous chapter: the primary database is the source of truth, and changes reach the search index through events, a queue, or change data capture (CDC). Here we look at what that delay means for users and for your design.
Why the index lags
A search index is a copy of your data. Each write goes to the database first. A pipeline then carries the change to the index. The index also batches new documents before they become searchable. Elasticsearch-style engines call this the refresh interval, and it defaults to about one second.
So there is a window in which the database and the index disagree. Eventual consistency means that if writes stop, the index will catch up. It does not promise when.
Common symptoms
- A user creates an item, searches for it at once, and does not see it.
- A user edits a price, but the result list still shows the old price.
- A deleted item still appears in results, and clicking it returns a 404.
- Two replicas of the index return slightly different results for the same query.
Design techniques
Read the source of truth for detail pages
Use the index to find IDs and show summaries. When the user opens an item, load it from the database. This hides stale data on the page that matters most, and it handles deleted items cleanly.
Hydrate results from the database
After a search returns IDs, fetch the current rows and drop any that no longer exist or are no longer visible. This costs one extra query per page. It also guards against stale permissions, which the access control chapter returns to.
Make updates idempotent and ordered
Retries and out-of-order events are normal. Index by document ID, so a retry overwrites the same document. Use an external version from the source database, so an older event cannot replace a newer one.
PUT /products/_doc/42?version_type=external&version=7
{
"name": "Trail running shoes",
"category": "footwear",
"price": 89.0,
"in_stock": true
}If the index already holds version 7 or higher, this request is rejected with a version conflict. The pipeline should treat that conflict as success, because the index is already at least as new.
Handle the user's own writes
Users notice staleness most right after their own change. Options:
| Technique | How it works | Cost |
|---|---|---|
| Optimistic UI | Show the new item in the client without querying | Client code only |
| Wait for refresh | The indexing call blocks until the change is searchable | Slower writes |
| Merge recent writes | Add the user's last few changes to the results | Extra logic |
Measure the lag
Track how long a change takes to become searchable. Record the database commit time in each document, then compare it with the time the pipeline indexed it. Alert when the lag passes a threshold you choose, such as 30 seconds. Also watch queue depth, since a growing backlog is the earliest warning.
Repair drift
Lost events and bugs leave the index permanently wrong, not just late. Run a periodic reconciliation job. It compares IDs and update timestamps between the database and the index, then reindexes mismatches.
Exercise
A user deletes a comment, and it still appears in search results for a few seconds. Name two design techniques that stop the user from reaching a broken page.
Show answer
Hydrate results from the database and drop IDs whose rows no longer exist. Alternatively, load the detail page from the source of truth and show a "not found" state. Optimistic UI that removes the item client-side also hides it from that user.
Exercise
The pipeline receives version 6 of a product after version 7 was already indexed. Using external versioning, what happens, and how should the pipeline respond?
Show answer
The engine rejects version 6 with a version conflict, so the newer document stays. The pipeline should treat the conflict as success and move on without retrying.