Eventual consistency
Also called: eventually consistent.
A promise that the copies of some data (a cache, a search index, another service's table) may disagree for a while after a write, usually a short while, but all agree once writes stop. Until then, a screen that reads one copy can show the old value. When a read must see the latest write, it reads from the source, which is slower and puts the load on one place.
Change the name saves it in the users DB. The search index gets it 0.8 s later, the orders copy 2.2 s later, and the cache (another team's, so it can only expire) when its 2 s TTL runs out. Keep editing sends 4 changes. Reading the profile from the source is always new, but slower.
Reads: 0 · Old answers: 0 · Profile read: 5 ms · On the users DB: 0
Every copy says "Sam". Change the name and watch each copy catch up at its own pace.
Say it in a prompt
When a user renames themselves in the users service, publish a UserRenamed event. The search indexer and the orders service update their own copies from it. The profile cache belongs to another team and hears no events, so we can't delete its key on write as cache-aside would; it keeps entries for 2 seconds (TTL). Accept that screens can show different names for a few seconds. On the profile page only, read the name from the users service so the person sees their change right after saving; other screens keep reading their copies. Add a nightly job that compares each copy with the users table and fixes any that drifted. Vague vs precise prompt
Vague prompt
make sure the new name shows up everywhere right away Typical resultMakes every screen read the users service directly. Pages get slower and every read lands on one database, and the search index and the other services still show the old name for a while anyway.
Precise prompt
Publish UserRenamed when a name changes; search and orders update their copies from it, and the profile cache (another team's, so it can only expire) has a 2 s TTL. On the profile page only, read the name from the users service. A nightly job compares the copies with the users table and fixes drift. Typical resultMost screens stay fast and read their own copy. The person who renamed sees the new name at once, other screens catch up within seconds, and a copy that missed an event is fixed by the nightly job.
Seen on
- All Things Distributed (Werner Vogels): Defines eventual consistency: if no new updates are made to an object, eventually all accesses return the last updated value. The time before that is called the inconsistency window.
- Amazon DynamoDB docs: Reads are eventually consistent by default and may not show a write that just finished; a read repeated a short time later should. A strongly consistent read (ConsistentRead: true) returns the latest data.
- etcd docs: Linearizable (strong) reads go through consensus, so they have higher latency and lower throughput; serializable reads are faster but may return stale data.
You might describe it as
- all the copies will match in a moment
- two screens show different names for a few seconds
- fine to be a bit out of date, as long as it catches up
Not to be confused with
- Read model
Eventual consistency is a promise about every copy of some data, that they agree once writes stop; a read model is one kind of copy, and it usually keeps that promise.
- Read replica
Eventual consistency covers every kind of copy, from caches and search indexes to other services' tables; a lagging read replica is just one reason two copies disagree.