How do you handle Eventual Consistency in a microservices system?
Topic: microservices, Data Management. Level: Intermediate.
Answer
Accept that read models and downstream services lag the source of truth briefly, and design for it: make consumers **idempotent** (safe on duplicates), tolerate **out-of-order** events (use versions/timestamps), expose the consistency to users honestly (e.g. "payment processing"), use **compensations** to fix divergence, and add reconciliation jobs to detect drift. Never assume a cross-service read reflects a write that just happened; instead confirm via events or poll with a status.
```java
// Version guard: ignore stale/duplicate events, apply only newer state
void onProfileUpdated(ProfileUpdated e) {
ReadRow row = readDb.find(e.userId());
if (row != null && e.version() <= row.version()) return; // stale/dup -> skip
readDb.upsert(e.userId(), e.data(), e.version());
}
```