Consistency Models
How distributed systems agree on the "truth". From strict to eventual consistency.
1What is Consistency?
2Why Does This Matter?
The Problem: Single Database Doesn't Scale
(easy!)
One database = one source of truth. Reading and writing always consistent. But what happens at scale?
Users
US
EU
Asia
Multiple databases = better performance but... how do we keep them in sync?
Without Consistency Guarantees
- • User changes password in US, EU still has old password
- • Bank shows $100 in NYC, $0 in LA for same account
- • Two users buy the "last" item simultaneously
- • Message sent at 2pm appears before message sent at 1pm
With Proper Consistency
- • Password change propagates to all regions
- • Bank balance is accurate everywhere (or clearly pending)
- • Only one user can buy the last item
- • Messages appear in correct order
3Consistency Levels (Spectrum)
Consistency exists on a spectrum from strongest to weakest. Stronger = safer but slower.
Strong Consistency (Linearizability)
strongestEvery read returns the most recent write. All operations appear to execute in a single, total order.
Sequential Consistency
strongOperations appear in some sequential order, same order seen by all. But not necessarily real-time order.
Causal Consistency
mediumOperations that are causally related must be seen in order. Concurrent operations can be seen in any order.
Eventual Consistency
weakIf no new writes, all replicas will eventually converge to the same value. No guarantee how long.
Comparison Table
| Model | Guarantee | Availability | Latency | Use When |
|---|---|---|---|---|
| Strong | Latest value always | Low | High | Correctness critical |
| Causal | Ordered if related | Medium | Medium | Replies, threads |
| Eventual | Eventually same | High | Low | High scale, staleness OK |
4Read Your Own Writes
The Problem Without It
Solutions
5Conflict Resolution
With eventual consistency, two users might update the same data simultaneously. Who wins?
The Conflict Scenario
Highest timestamp wins. Simple but can lose data.
Earliest timestamp wins. Preserves original.
Automatically merge without conflict. Works for certain data types.
Keep both versions, let application or user resolve.
6Real-World Examples
Can't have $100 in one branch and $0 in another. Account balance must be accurate everywhere. Worth the latency hit.
If like count shows 999 instead of 1000 for a few seconds, no one cares. Scale and availability matter more.
Items might briefly appear/disappear as replicas sync. Acceptable trade-off for availability.
View count can be eventual, but actual purchase must be consistent to avoid overselling.
Replies must appear after the message they reply to. But order of unrelated messages is flexible.