Updated 24 October 2026
Distributed locks, leases and fencing tokens
When you need mutual exclusion across machines and how to get it safely: lock services, leases with expiry, why Redis locks can fail, fencing tokens, leader election, optimistic alternatives, and avoiding locks altogether.
Try it yourself, in a playground:Fencing tokensHeartbeats and failure detectionDeadlocksReservation holds
Sometimes exactly one machine should do something: run a scheduled job, process a partition, charge a card, hold a seat. On a single machine a mutex does it. Across machines, processes pause, networks partition and clocks drift, so a "lock" can be held by two processes at once without either noticing. Interviewers probe this whenever a design says "we take a lock". Knowing about leases and fencing tokens separates a careful answer from a naive one.
Why it is hard
A distributed lock must expire, or a crashed holder would block everyone forever. But expiry creates the danger:
- Process A acquires the lock with a 10-second lease.
- A pauses for 15 seconds (a long garbage collection, a stalled VM, a slow network).
- The lease expires; process B acquires the lock and starts writing.
- A wakes up, still believing it holds the lock, and writes too.
A has no way to know it was paused. No timeout value fixes this in general; it only makes it rarer.
Leases
A lease is a lock with a time limit that the holder must renew. Lock services such as ZooKeeper, etcd and Consul provide leases backed by consensus, so the lock state itself survives node failures. The holder:
- Renews well before expiry (every third of the lease).
- Stops work as soon as a renewal fails or it is unsure, before the lease could have expired.
- Treats the lease as advisory: a hint that it should be the only worker, not a guarantee.
See consensus and coordination.
Fencing tokens
The robust fix is to make the resource reject stale holders:
- Every time the lock is granted, the lock service returns a monotonically increasing token (33, then 34).
- The holder sends its token with every write.
- The storage system remembers the highest token it has seen and rejects writes with a lower one.
In the scenario above, B holds token 34 and writes; when A wakes and writes with token 33, the write is refused. Correctness no longer depends on timing. ZooKeeper's zxid or etcd's revision can serve as tokens. A conditional write (UPDATE ... WHERE version = ?) is the same idea.
Redis locks
SET key value NX PX 10000 is a quick, popular lock. Be honest about its limits:
- A single Redis instance is a single point of failure; with asynchronous replication, a failover can lose a lock and grant it twice.
- Redlock (majority across independent Redis nodes) still depends on timing assumptions and provides no fencing token.
- Release safely by deleting only if the value is still yours (a Lua script comparing a random value).
Redis locks are fine for efficiency (avoid doing the same work twice; occasional duplication is harmless). For correctness (double charging, overselling), use a consensus-backed service with fencing, or a database constraint.
Leader election
Leader election is a long-lived lock: one scheduler, one partition owner, one primary. The same rules apply: leases, renewals, and fencing tokens (often called epochs or terms) so that an old leader's actions are rejected after a new one takes over. See Design a Job Scheduler.
Often you do not need a lock
Many "lock" problems are better solved without one:
| Need | Lock-free approach |
|---|---|
| Do not sell the same seat twice | conditional update or unique constraint in the database |
| Process each job once | queue with visibility timeout plus idempotent handlers |
| One worker per partition | partition assignment by the consumer group |
| Do not crawl a URL twice | deduplication by key in a set or table |
| Avoid lost updates | optimistic concurrency with version numbers |
| Charge a card once | idempotency key stored with a unique constraint |
The database already provides atomic, durable mutual exclusion on a row. See transactions and isolation, Design Ticketmaster and Design a Web Crawler.
Choosing
- Can a database constraint, conditional write or idempotency key do it? Use that.
- Is duplicate work merely wasteful? A Redis lock with a TTL is enough.
- Must it never happen twice? A consensus-backed lease with fencing tokens checked by the resource.
Checklist
- Every lock has an expiry; holders renew and stop when unsure.
- Fencing tokens checked by the protected resource for correctness.
- Redis locks only for efficiency, released with a value check.
- Leader election with epochs that reject old leaders.
- Prefer constraints, conditional writes, idempotency and partition ownership over locks.
Questions people ask
What is a distributed lock?
A distributed lock gives mutual exclusion across machines, so that exactly one process runs a scheduled job, processes a partition, charges a card or holds a seat. It is harder than a mutex because processes pause, networks partition and clocks drift, so a lock can be held by two processes at once without either noticing. The lock must expire, or a crashed holder would block everyone forever, and that expiry is what creates the danger.
Lock vs lease: what is the difference?
A lease is a lock with a time limit that the holder must keep renewing, so a crashed holder cannot block everyone forever. The holder renews well before expiry, every third of the lease, and stops work as soon as a renewal fails or it is unsure. A lease is advisory, not a guarantee: a process paused past expiry can wake up still believing it holds the lock, which is why correctness needs fencing tokens.
What is a fencing token?
A fencing token is a monotonically increasing number the lock service returns each time it grants the lock, which the protected resource uses to reject stale holders. The holder sends its token with every write, and the storage system rejects any write with a lower token than the highest it has seen. If process A pauses holding token 33 and B then acquires token 34, A's later write is refused, so correctness no longer depends on timing.
Is the Redis Redlock algorithm safe?
Redlock is not safe for correctness, because it still depends on timing assumptions and provides no fencing token. It takes a majority across independent Redis nodes, but without a fencing token nothing stops a process that paused past its lease from writing after another process has taken the lock. Redis locks are fine for efficiency, where occasional duplicate work is harmless; for double charging or overselling, use a consensus-backed service with fencing or a database constraint.
How do you make a distributed lock safe for correctness?
Use a consensus-backed lease from a lock service such as ZooKeeper, etcd or Consul, with fencing tokens checked by the protected resource. Consensus keeps the lock state alive through node failures, and the fencing token makes the resource reject writes from a stale holder, so correctness no longer depends on timing. ZooKeeper's zxid or etcd's revision can serve as the token, and a conditional write (UPDATE ... WHERE version = ?) applies the same idea.
What can you use instead of a distributed lock?
A database constraint, a conditional write or an idempotency key solves many lock problems better than a lock, because the database already provides atomic, durable mutual exclusion on a row. To avoid selling the same seat twice, use a conditional update or unique constraint; to charge a card once, store an idempotency key with a unique constraint; to process each job once, use a queue with a visibility timeout and idempotent handlers.