Distributed Locks, Leases and Fencing Tokens
Overview
Once work runs on many machines, a normal in-memory lock is not enough: machines share no memory, can pause or die silently, and have clocks that disagree. This lesson shows how to make sure only one machine does a job at a time anyway.
Detailed Notes
What is a distributed lock
A distributed lock is a lock shared by processes on different machines, kept in a service they can all reach (a database or a coordination service). It makes sure only one worker at a time performs a task, such as running a nightly job or acting as leader.
It is harder than a local lock: there is no shared memory, only messages that can be lost or delayed, and you can never tell for certain whether a silent machine is dead or just slow (the idea behind heartbeats).
The problem it solves
A nightly job sends invoices. It runs on three servers for reliability, but only one should send each night, so the first server to take a lock runs the job.
Then Server A takes the lock and freezes for 40 seconds (a long memory-cleanup pause). A crashed holder must not block everyone forever, so the lock has an expiry. It expires, Server B takes it and starts sending. Server A wakes up, still believes it holds the lock, and sends too. Customers are billed twice.
How it works
Leases. A lease is a lock with an expiry time (a TTL). The holder must renew it regularly, like a heartbeat. If it stops (crash, network cut), the lease expires and someone else can take over. The price is the risk above: a holder that is merely slow may lose the lease without knowing.
Fencing tokens. A fencing token is a number that increases every time the lock is granted. The holder sends its token with every write, and the protected resource (say, the database) remembers the highest token seen and rejects older ones.
A gets lock, token 33 -> pauses, lease expires
B gets lock, token 34 -> writes with 34 (accepted)
A wakes, writes with 33 -> REJECTED (33 < 34)
The goal: do not rely on the lock alone; make the resource check the token.
Leader election. Picking one node for a special role (like the primary database) is a lease in disguise: the lease holder is the leader, and if it dies the others elect a new one.
Coordination services. Tools like Apache ZooKeeper and etcd (the store behind Kubernetes) keep small, critical data such as locks, leases and leader identity. They run on 3 or 5 machines and use a consensus algorithm (Raft, Paxos) so a majority (quorum) agrees on every change: with 3 machines one failure is tolerated, with 5 two are. Every write needs a majority round trip, so they hold small coordination data only.
Pair the lease with idempotency keys on side effects (see the Resilience chapter) so a takeover never duplicates work.
A simple example
A hotel room key card auto-expires at checkout (the lease). Each new card has a higher number (the token) and the door only accepts the newest, so a guest with the old card cannot walk back in.
System design example
Three workers compete to run the invoice job. The winner gets a 30-second lease from etcd with token 34 and renews it while working. Each database write carries the token, so if the winner pauses and another worker takes over, the old winner's token 33 is rejected.
Summary
- Distributed locks decide "who may act", but machines can pause or fail silently.
- Leases expire so a dead holder cannot block forever.
- Fencing tokens let the resource reject a stale former holder.
- ZooKeeper and etcd use quorum consensus for small, critical coordination data.
- Coordinate only where correctness truly needs it; it costs latency.
Architecture Diagram
Safe 'run once' work across several workers.
- 1Workers compete to acquire a lease from the coordination service.
- 2The winner receives an increasing fencing token and renews the lease.
- 3The database rejects writes carrying stale tokens.
Common Interview Questions
Finished this lesson?
0 of 91 lessons done · 0% of the course

