In this lesson · topic 2 of 7
Topic 2
Three ways to say yes
6 min read
The acknowledgement rule
The leader has one decision to make for every write. How many copies must exist before it tells the client the write succeeded? There are three common answers.
Asynchronous. The leader tells the client yes as soon as the write is on its own disk. The followers get it afterwards. This is the fastest, and the client’s wait does not depend on the network at all.
Semi-synchronous. The leader waits until one follower has the write, then says yes. Now every acknowledged write exists on two machines.
Synchronous. The leader waits for every follower. Every acknowledged write exists everywhere.
The stepper walks one write through all three. The delays in it are made up to keep the arithmetic easy. The lab has the real thing.
One write, three rules for when to say yes
Step 1 / 5- leader log
- 4 entries
- followers
- 3 and 3
The write reaches the leader
The leader appends the fourth entry to its log. Followers still hold three. Say the link to follower 1 takes 10 ms one way and the link to follower 2 takes 40 ms.
Asynchronous: say yes now
The leader tells the client the write succeeded as soon as its own disk has it, and ships it to the followers in the background. The client waits about 1 ms.
Semi-synchronous: wait for one follower
Follower 1 applies the entry after 10 ms and its reply gets back after 20 ms. The leader says yes then. Follower 2 is still on its way.
Synchronous: wait for every follower
Follower 2 applies the entry after 40 ms and replies after 80 ms. Only then does the leader say yes. The slowest follower sets the pace of every write.
The leader dies at 5 ms
Under asynchronous replication the client was told yes at 1 ms, but the entry had not left the leader. Both followers hold three entries, so the write the client was told about is gone. Under the other two rules the client had not been told yet.
- The write reaches the leader. The leader appends the fourth entry to its log. Followers still hold three. Say the link to follower 1 takes 10 ms one way and the link to follower 2 takes 40 ms. leader log: 4 entries; followers: 3 and 3.
- Asynchronous: say yes now. The leader tells the client the write succeeded as soon as its own disk has it, and ships it to the followers in the background. The client waits about 1 ms. client waits: 1 ms; copies when told: 1.
- Semi-synchronous: wait for one follower. Follower 1 applies the entry after 10 ms and its reply gets back after 20 ms. The leader says yes then. Follower 2 is still on its way. client waits: 21 ms; copies when told: 2.
- Synchronous: wait for every follower. Follower 2 applies the entry after 40 ms and replies after 80 ms. Only then does the leader say yes. The slowest follower sets the pace of every write. client waits: 81 ms; copies when told: 3.
- The leader dies at 5 ms. Under asynchronous replication the client was told yes at 1 ms, but the entry had not left the leader. Both followers hold three entries, so the write the client was told about is gone. Under the other two rules the client had not been told yet. async: acknowledged write lost: yes; semi and sync: not yet acknowledged.
What each rule costs
A client waiting on a follower waits for a round trip, there and back. So the wait is the delay to the follower and the delay of its reply. With several followers the leader waits for the fastest one in the semi-synchronous case and for the slowest in the synchronous one.
That sets up the trade. A stricter rule means more copies before the client is told yes, which means a smaller window for lost writes and stale reads, which means slower writes. One slow or dead follower also stalls every write under the synchronous rule. Real systems rarely run fully synchronous for that reason. Semi-synchronous, where one follower is kept up to date and the rest follow, is a common compromise.
When the leader dies
If the leader fails, one follower is promoted. This is failover. The sensible choice is the follower that is furthest ahead, since it has the most data. Whatever the new leader never received is gone.
The danger depends on the rule. Under the asynchronous rule the client may have been told yes for a write that no follower has. Under the semi-synchronous rule an acknowledged write is on at least one follower, so promoting the furthest-ahead survivor keeps it, unless that follower was lost along with the leader. Under the synchronous rule every follower has it.
Keeping one user’s view sensible
Even when lag is unavoidable, some things can be promised to a single user. Two are worth knowing by name. Both come from the session guarantees in the Terry paper.
Read-your-writes. A user who writes something should see it on the next read. The usual fix is routing. If a read would land on a follower that has not yet applied this user’s last write, send it to the leader instead. A user can carry a token, such as the log position of their last write, so any node can check whether it is far enough along.
Monotonic reads. A user who has seen a value should never later see an older one. This breaks when consecutive reads hit different followers with different lag. One fix is to pin each user to one follower, since one follower only moves forward. A stronger fix uses the same token idea and also tracks the newest position the user has read.