Strong, eventual and read-your-writes
You save a change, reload the page, and the old value is back. Which machine you read from decides what you see. Here are the main consistency levels, played out on three replicas.
01 You saved it, then it was gone
A user edits their display name and presses Save. The request succeeds. The page reloads and shows the old name. A second later, it shows the new one.
Nothing is broken. The write went to one machine and the read came from another that had not caught up. What a reader may see after a write is called the system’s consistency level, and you get to choose it.
02 One leader, two followers
Most databases that scale reads use leader-follower replication. Writes go to the leader. The leader keeps a log of changes and each follower applies that log in order, a little behind. Reads can go to any follower, which spreads the load.
With asynchronous replication, which is the common default (for example in Postgres streaming replication and MySQL), the leader replies to the client before followers have the change. The gap is the replication lag: typically milliseconds, but seconds or more when a follower is busy or far away.
- has the new write
03 Eventual: it gets there, later
Eventual consistency promises one thing: if writes stop, every replica will end up with the same value. It says nothing about how long that takes, or what you see in the meantime.
- just applied
- serving a request
- stale answer
- all agree
One leader takes writes. Two followers copy its changes a little later and serve reads. All three hold Alice’s old display name, Al, at version v1.
04 Read-your-writes: you always see your own
Read-your-writes (also called read-after-write) adds one promise: after you write, your reads show that write or something newer. Other users may still see old data.
A common way to build it: the write returns a version, the client remembers it, and each read says how new it must be. A replica that is too far behind waits, or hands the read to the leader.
- just applied
- serving a request
- stale answer
- all agree
Same three replicas, all at v1. Alice’s session has no note of her writes yet.
Simpler versions work too: send a user’s reads to the leader for a few seconds after they write, or always read your own profile from the leader and everyone else’s from followers.
05 Strong: everyone sees the latest
Strong consistency (linearizability) means the system behaves as if there were one copy. Once any write is acknowledged, every reader sees it, Alice and Bob alike.
To get it, a read must reach a replica that is sure it has the latest write. Typically that means reading from the leader while it holds a lease, or reading from a quorum of replicas.
| Level | Promise | Typical way to get it |
|---|---|---|
| Eventual | Replicas agree once writes stop | Read from any follower |
| Read-your-writes | You see your own writes | Version token, or read from the leader after writing |
| Monotonic reads | You never go back in time | Keep each user on one follower |
| Strong | Everyone sees every acknowledged write | Leader with a lease, or a read quorum |
06 Monotonic reads and other in-between levels
Between eventual and strong sit several small promises. Each fixes one surprise users notice.
- Monotonic reads: once you have seen
v2, you never seev1again. Without it, a user who refreshes twice and hits two followers can watch a comment appear, then vanish. - Consistent prefix: you see writes in the order they happened, so a reply never shows up before the post it answers.
- Read-your-writes combined with monotonic reads is often called session consistency, and covers most of what a single user can see going wrong.
07 When the guarantee leaks
v2. If the guarantee matters across devices, keep the version on the server, against her account./me is one more stale replica. Clear or bypass it for the writer, or the careful routing behind it does not help.