0% completed
Strong vs Eventual Consistency
On This Page
Strong Consistency
Quorums
What Strong Consistency Gives You
What Strong Consistency Costs You
Eventual Consistency
What Eventual Consistency Gives You
What Eventual Consistency Costs You
How Long Is "Eventually"?
Read Your Own Writes
One Page, Several Answers
Strong vs. Eventual Consistency
Choosing Between Them
Using This in an Interview
Key Takeaways
Practice Questions
You update your profile picture. The page reloads, and it still shows the old picture.
Nothing is broken. Your write went to one machine, and your read came back from another machine. That second machine had not received the change yet.
Almost every large system keeps copies of its data on several machines. Keeping copies is called replication, and each copy is called a replica. The Redundancy and Replication lesson explains why systems keep copies. This lesson is about the question that copies create: after a write, what does the next read see?
There are two main answers, and they have different costs:
- Strong consistency: every read returns the most recent write. To promise this, the system makes each write wait.
- Eventual consistency: a read is answered immediately, but it may briefly return the old value. The copies catch up shortly after.
Strong Consistency
A system is strongly consistent when every completed write is visible to every later read. It does not matter which client reads, or which replica answers.
Here is a common way to provide it. The write goes to the primary, which is the node that accepts writes. The primary sends the change to its replicas. It does not confirm the write to the client until enough replicas report that they have saved it. This is called synchronous replication, because the write waits for the copying.
Quorums
"Enough replicas" usually means a quorum. A quorum is the minimum number of servers that must successfully complete an operation. A common choice is a majority quorum. With 5 replicas, a majority is 3. So a write succeeds only after 3 of the 5 replicas confirm it.
A write quorum alone does not make every read correct, because 2 replicas may still be behind. So the system also reads from a quorum. Here is the rule. Add the number of replicas that confirm a write to the number that answer a read. If the total is more than the number of replicas, every read reaches at least one replica with the latest write. With 5 replicas, 3 for writes plus 3 for reads is 6, which is more than 5. The Quorum lesson explains this in more detail.
What Strong Consistency Gives You
- Reads are always correct. No user ever sees an old value.
- Simpler application code. The application never needs to handle a stale read, which is a read that returns an old value.
What Strong Consistency Costs You
- Slower writes. Each write waits for the replicas it needs, plus the network time to reach them. Replicas in the same data center add about a millisecond. But a replica on another continent adds a full round trip. For example, a round trip between Virginia and Frankfurt takes about 90 ms. So a write that must wait for Frankfurt takes about 90 ms longer. This is why strong consistency across distant data centers increases write latency. No engineering can remove this delay, because it comes from distance.
- Refused writes. If too many replicas are unreachable, the system has a choice: accept the write and break its promise, or refuse the write. A strongly consistent system refuses. With a majority quorum of 3 out of 5, the system keeps working with 2 replicas down, but not with 3 down.
The classic use for strong consistency is money. You withdraw 100 dollars from an account that holds 500 dollars. Every later read, anywhere, must show 400 dollars. If some machine still shows 500 dollars, the same money could be spent twice.
Eventual Consistency
A system is eventually consistent when its copies converge, which means they all reach the same value. If no new updates are made, all copies of the data eventually become identical. The promise is that the copies will agree, not when they will agree.
The mechanism is the same replication, without the waiting. The write goes to one node, and that node confirms it immediately. The change travels to the other replicas in the background. This is called asynchronous replication, because the copying happens after the confirmation. A read that reaches a replica before the change arrives returns the old value.
What Eventual Consistency Gives You
- Fast writes. A write only needs one machine to confirm it.
- Better availability. The system keeps accepting reads and writes while some replicas are unreachable, because no node waits for another.
What Eventual Consistency Costs You
- A stale window. For a short time, updates are still spreading, so two users may briefly see different data.
- More careful application code. The application must behave correctly when a value is briefly old.
The Domain Name System (DNS), which maps domain names to server addresses, works this way. When a domain's address changes, some DNS servers keep returning the old address for minutes or hours. Nobody calls DNS broken, because the delay is known and planned for.
How Long Is "Eventually"?
Replication lag is the delay between a write on one node and its arrival at the other copies. This number decides whether eventual consistency is acceptable.
- Inside one data center, lag is usually a few milliseconds.
- Across regions, lag is usually tens to a few hundred milliseconds.
For most data, the stale window ends before a user can even reload the page. But lag is not always small. It grows under heavy write load, when a restarted replica is catching up, or when a network link is slow. A replica can fall minutes behind. If lag affects correctness anywhere in your design, monitor it.
Read Your Own Writes
Users rarely notice when other people see slightly old data. They notice when they see it themselves. For example, you post a comment, the page refreshes, and your comment is missing. The system works exactly as designed, but it looks like a bug.
The fix is small. For a short time after a user writes, send that user's reads to the primary. Everyone else keeps reading from replicas. This is called read-your-own-writes consistency.
Almost all traffic still uses the cheaper replica reads. And the one kind of staleness that users actually notice is removed. The cost is a small amount of routing logic, and slightly more load on the primary.
There are also levels between the two extremes:
- Causal consistency keeps related operations in order. For example, a reply never appears before the message it answers.
- Bounded staleness sets a limit on how old a read may be, for example 5 seconds.
Some databases, like Azure Cosmos DB and MongoDB, let you choose a consistency level for each operation.
One Page, Several Answers
A common mistake is choosing one consistency model for a whole system. Real designs choose for each piece of data.
Look at a single shopping page:
- The account balance should be strongly consistent, because a wrong number is a real problem.
- The stock count should be strongly consistent at checkout, because selling the last item twice costs money.
- The view counter can be eventually consistent, because nobody notices a few seconds of delay.
- The recommendations can be minutes old.
That is several answers on one page. So name the data, not the system. For example: "Balance reads go to the primary and are strongly consistent. Everything else is served from replicas."
Strong vs. Eventual Consistency
| Strong consistency | Eventual consistency | |
|---|---|---|
| After a write | Every read returns the new value | A read may briefly return the old value |
| Write speed | Waits for replicas to confirm | One node confirms immediately |
| Replicas unreachable | May refuse writes | Keeps accepting reads and writes |
| Copying style | Synchronous | Asynchronous, in the background |
| Example systems | Google Spanner, etcd, a single primary database | Cassandra and DynamoDB by default, DNS, most caches |
| Good fit | Money, stock, bookings | Feeds, counters, recommendations |
Think of the two models as the two ends of a range. Most real systems sit somewhere between them, on purpose.
Choosing Between Them
- The data counts something that must be exact, like money, stock, seats, or bookings: choose strong consistency.
- The data is content or a casual count, like feeds, likes, views, or recommendations: choose eventual consistency.
- Users read back their own changes: choose eventual consistency, plus read-your-own-writes.
- Replicas are in several regions, and writes must stay fast: choose eventual consistency, because strong consistency waits for the round trip on every write.
- You cannot decide yet: start with strong consistency, and relax specific reads when measurements show the need.
Three other lessons explain the theory behind this choice. The CAP Theorem lesson explains the choice during a network partition, which is a failure that cuts machines off from each other. The PACELC Theorem lesson extends it to normal operation, where requests trade latency against consistency. The Quorum lesson explains how many replicas must confirm each read and write.
Using This in an Interview
Do not answer "strong" or "eventual" for the whole system. Split the answer. Name the one or two pieces of data that must never be stale, and send those reads to the primary. Serve everything else from replicas. Then add read-your-own-writes for each user's own data, because that is the staleness users notice.
A common follow-up question is: "How stale can a replica get?" Answer with numbers: usually a few milliseconds inside a data center, but possibly minutes while a replica is catching up. That is why replication lag is monitored.
Key Takeaways
- Strong consistency promises that every read returns the newest write. Writes wait for replicas, and may be refused when too many replicas are unreachable.
- Across distant data centers, strong consistency increases write latency, because synchronous replication must cross the distance.
- A quorum is the minimum number of servers that must complete an operation. With 5 replicas, a majority quorum is 3.
- Eventual consistency confirms writes immediately. Updates spread over time, and readers may briefly see different data.
- If no new updates are made, all copies in an eventually consistent system become identical.
- Read-your-own-writes removes the staleness users notice most.
- Choose a consistency level for each piece of data, not for the whole system.
Consistency decides what readers see after a write. The next lesson, Primary-Replica vs Peer-to-Peer Replication, looks at how the machines that hold the copies are organized.
Practice Questions
Try each question first, then open the answer.
1. A database uses strong consistency, with replicas in Virginia and Frankfurt. A write in Virginia takes 5 ms locally, and each write must wait for Frankfurt to confirm. A round trip between the two takes about 90 ms. About how long does each write take, and why?
<details> <summary>Show answer</summary>About 95 ms. The local work takes 5 ms, and waiting for Frankfurt adds a round trip of about 90 ms. This is synchronous replication: the write is not confirmed until the distant replica has it. Faster servers cannot remove the 90 ms, because it comes from the distance between the data centers.
</details>2. A system has 5 replicas and uses a majority quorum for writes. How many replicas must confirm each write? Can the system still accept writes with 2 replicas down? With 3 replicas down?
<details> <summary>Show answer</summary>3 replicas must confirm each write. A majority of 5 is 3. With 2 replicas down, 3 are still running, so writes can still succeed. With 3 replicas down, only 2 are running, which is less than the quorum of 3. So the system refuses writes, to keep its consistency promise.
</details>3. A system has 5 replicas. Writes need 3 confirmations, and reads ask 3 replicas. Why does every read see the latest write? What changes if reads ask only 2 replicas?
<details> <summary>Show answer</summary>3 + 3 = 6, which is more than 5, so the groups always overlap. At least one of the 3 replicas that answer a read must also be one of the 3 that confirmed the write. So the read finds the latest value. If reads ask only 2 replicas, 3 + 2 = 5, which is not more than 5. The 2 replicas that answer a read could both be the ones that missed the write, so the read may return an old value.
</details>4. A user posts a comment and refreshes the page 50 ms later. The read goes to a replica, and replication lag is about 200 ms. What does the user see, and how can the design fix it?
<details> <summary>Show answer</summary>The comment is missing, because the replica has not received it yet. The system is eventually consistent, and the user read inside the stale window. The fix is read-your-own-writes. For a short time after a user writes, send that user's reads to the primary. Other users can keep reading from replicas, and will see the comment a moment later.
</details>5. A shopping page shows an account balance, a product's stock count, the product's view count, and recommendations. Which of these need strong consistency?
<details> <summary>Show answer</summary>The account balance and the stock count. A wrong balance is a real problem, and a wrong stock count at checkout can sell the last item twice. The view count and the recommendations can be eventually consistent, because a short delay does no harm. So the design sends balance and stock reads to the primary, and serves the rest from replicas.
</details>Reading Progress
0%
On This Page
Strong Consistency
Quorums
What Strong Consistency Gives You
What Strong Consistency Costs You
Eventual Consistency
What Eventual Consistency Gives You
What Eventual Consistency Costs You
How Long Is "Eventually"?
Read Your Own Writes
One Page, Several Answers
Strong vs. Eventual Consistency
Choosing Between Them
Using This in an Interview
Key Takeaways
Practice Questions