Distributed locks and coordination services
A lock that can expire
Section titled “A lock that can expire”A lock on one machine is simple: whoever holds it runs, and the operating system knows if the holder dies. A lock shared by several machines has to survive the holder crashing, so it is given a lease, a time limit after which the lock is released by itself. The last lesson showed the problem with that: a holder that pauses past the lease can wake up and act as if it still held the lock. A distributed lock that is safe is therefore a lease plus a fencing token, with a storage that refuses a token older than one it has seen.
Google’s Chubby paper describes the same design. Its locks are advisory: holding a lock named F neither is necessary to access the file F, nor prevents other clients from doing so, and the authors rejected mandatory locks. For protection, a lock holder can ask for a sequencer, an opaque string that describes the state of the lock just after acquisition and holds the lock name, the mode and the lock generation number. The client passes it to the servers it uses, and a server is expected to test whether the sequencer is still valid and reject the request if it is not. That is the fencing token of the last lesson, under another name.
Mental model. A lock is a ticket from a taker-a-number machine. The ticket says who is being served now. A counter that checks the number on each customer’s ticket can ignore a customer who comes up with an old one, however sure that customer is that it is their turn.
What is the lock for?
Section titled “What is the lock for?”Kleppmann separates two reasons to take a lock. The first is efficiency: the lock saves you from doing the same work twice, and if it fails, the result is a minor increase in cost. The second is correctness: the lock keeps concurrent processes from corrupting state, and if it fails, the result is a corrupted file, data loss or permanent inconsistency. His advice follows from that. For an efficiency-only lock, a simple single-node lock is enough, with the code saying plainly that the lock is approximate and may sometimes fail. If you need a lock for correctness, he recommends a proper consensus system such as ZooKeeper, and he notes that the Redlock algorithm has no facility for generating fencing tokens, which makes it unsafe for that use. This is his argument, written for a specific algorithm, and you should read it with its counter-arguments in mind. The distinction itself is useful whichever side you take.
Sort these uses of a lock by what a failure of the lock would cost.
Challenge
What is the lock for?
A lock can save work, or it can keep state correct, and the two fail differently. Put each use under what a failure of the lock would cost.
Sorting needs JavaScript.
Categories
- Efficiency
- A failure means some work is done twice.
- Correctness
- A failure corrupts state or loses data.
Check your answers at least once before looking at the answers.
Coordination services
Section titled “Coordination services”Most teams do not build the consensus system, because there are services built for it. They have a few things in common. They hold coordination data, not bulk data: the etcd documentation sets itself against databases that partition data across many replication groups, which it calls poor candidates for distributed coordination because of their long latencies. Their state changes are replicated by a majority, which is why the quorum lesson shows what happens to them in a partition. And they offer the primitives of this module as ready-made calls.
- etcd attaches a time to live to a lease and revokes it when the server sees that time pass. It lists Lock RPCs and Election RPCs among its coordination primitives. Its documentation is candid about the limit: the lease mechanism itself does not guarantee mutual exclusion, and owning a lease cannot guarantee that the owner holds the lock. Mutual exclusion comes from version number validation, the conditions on revision number and lease that a request can carry. The documentation reads Chubby’s sequencer and Kleppmann’s fencing token as the same idea as etcd’s revision number.
- ZooKeeper builds a lock and an election from nodes in a tree, called znodes, created with two flags: ephemeral, so the node is deleted when the client’s session ends, and sequential, so the service appends an increasing number. Each client creates one, and the client with the smallest number is the leader or holds the lock. Each of the others watches the node just ahead of its own in the sequence, not the first one, to avoid the herd effect, in which every client wakes at once when the holder disappears.
- Chubby offers advisory locks and sequencers, as above, and its lock and file events let clients find out when a primary has been elected.
Each is still a lease underneath. A client whose session ends loses its ephemeral node, and a client that is paused may not know it. This lesson does not assume how ZooKeeper hands out a number for the protected storage to compare. The etcd documentation points to a note by Flavio Junqueira on how fencing should be done with ZooKeeper, which is the place to look.
What happens if the coordination service is partitioned?
Section titled “What happens if the coordination service is partitioned?”Take a service with five nodes and a partition that leaves two of them on one side. With a write quorum of three and a read quorum of three, the three-node side can read and write and the two-node side can do neither, as the quorum lesson showed. A lock service behaves the same way: clients that can only reach the minority cannot acquire a lock or renew a lease. That is the intended behaviour, and it has a cost you can see: an application that depends on the service stops making progress on the minority side, even though its own machines are healthy. A lock held by a client on that side will expire, and the client has to be written to stop working when it cannot renew.
- A distributed lock is a lease plus a fencing token. The lease releases a crashed holder’s lock, and the token makes a late holder harmless if the storage checks it.
- Chubby calls the token a sequencer, and its locks are advisory. ZooKeeper builds locks and elections from ephemeral sequential znodes, and etcd offers lock and election calls over leases.
- Decide what the lock is for. For efficiency, a failure costs some duplicated work. For correctness, it corrupts state, and then a consensus-backed service and a token check are needed.
- Coordination services hold coordination data, are replicated by majority, and refuse service on the minority side of a partition.
Next: Two-phase commit.
Your progress
Built by GhaziAlibi.
© ShardCraft. All rights reserved.