A mutex on one machine is a solved problem with decades of hardware support behind it. Move the same idea across a network and it stops being a lock at all, because the two guarantees that made it work — that the holder is alive, and that it knows it still holds the lock — are both unavailable.
What you get instead is a lease: permission that expires. Almost everything difficult about distributed locking follows from the gap between what a lease promises and what people assume it promises.
First, do you need one?
The lazy answer is usually available and usually better, so it is worth ruling out before building anything.
If you are protecting a single row or document, use a conditional write — compare-and-set, an optimistic version column, a unique constraint. The database already has the atomicity you were about to construct, and it applies it without a second system that can fail. This is what the rate limiter uses and what inventory reservations use, and neither needs a lock service.
If you are trying to stop two workers running the same job, partition the work so only one worker is ever responsible for a given key. A consistent hash over job ids does this without coordination at all.
The remaining cases are the real ones: a leader that owns a shared external resource, mutual exclusion across systems that do not share a database, or a long operation with side effects that must not be run concurrently. Those need a lock, and they need to understand what a lock cannot do.
The failure every implementation has
Client A stalls, its lease expires, and it wakes up anyway
—
Client A acquires a ten-second lease and then stops — a stop-the-world garbage collection, a hypervisor freeze, a machine that gets suspended. Set the pause to fourteen seconds.
The lease expires. B legitimately acquires the lock. And then A wakes up and writes.
Every component behaved correctly. The lock service expired the lease exactly as promised. B acquired it exactly as promised. A’s only mistake was to be paused, and a paused process cannot know it was paused — from inside, no time passed. It resumes at the instruction after the one where it stopped, believing it still holds the lock, and destroys B’s work with no error anywhere.
You cannot fix this by shortening the lease, which only makes spurious expiry more common. You cannot fix it by checking whether you still hold the lock before writing, because the process can be paused between the check and the write. You cannot fix it with a better lock service, because no lock service can see inside your process.
Switch the mode to fencing tokens and run the same trace. The lock hands out a monotonically increasing number with each grant — A gets 1, B gets 2 — and the resource remembers the highest number it has accepted. A’s late write arrives carrying token 1, the file has already accepted token 2, and the write is refused.
Note where the enforcement lives. Not in the lock service, which cannot help; in the storage layer. The lock is advice about who should proceed; the token is the mechanism that makes a stale writer harmless. And it requires the resource to participate, which is precisely why this is so often skipped: if your storage cannot reject a write based on a version number, you cannot fence, and you should be honest that your lock is best-effort. A conditional update on a version column is a fencing token wearing different clothes, and it is available in every database you already run.
Elections, and the case where nobody wins
Leader election is the same problem from the other end: not “who may write” but “who is in charge”, which is the same question with a longer lease.
Cut the cluster and see who still gets to lead
—
Cut the cluster in different places, with the rule set to “a majority of whoever I can see”. Both sides elect a leader. Both are correct given their information — neither side can distinguish “the others are down” from “I am the one cut off”, and that is not an implementation weakness, it is genuinely undecidable from inside.
Switch to a strict majority of all nodes and the arithmetic does the work: only one group of three can exist in a cluster of five, so at most one leader can exist no matter where the cut falls. The minority side steps down while perfectly healthy.
Then set the split to give neither side a majority. Nobody leads, and every write is refused. This looks like a failure and is the system working — choosing to be unavailable rather than to be wrong. It is also why cluster sizes are odd: an even cluster has a split with no majority, and adding a sixth node to a five-node cluster makes availability worse, not better.
This is what Raft and ZooKeeper’s protocol implement, and it is the reason “just use etcd” is genuinely good advice. The consensus algorithm is not the part you should be writing.
Where Redlock fits
The algorithm that comes up in every discussion is Redlock: acquire the lock on a majority of N independent Redis nodes, and consider it held if you got most of them within the validity time.
The dispute is not about whether the implementation is correct. It is about whether the algorithm’s safety argument holds, and the objection is the one from the figure above: acquiring a majority tells you about a moment in the past, and a pause after acquisition puts you in exactly the two-writer situation regardless of how many nodes agreed. Redlock also leans on bounded clock drift across independent machines, which is an assumption rather than a guarantee.
The practical position: if your lock is an efficiency measure — you would prefer not to run the job twice, and running it twice is merely wasteful — Redlock and a single-Redis lock are both fine, and the single one is simpler. If your lock is a correctness measure — running twice corrupts data — then no lock protocol is sufficient on its own, and you need fencing at the resource. There is no configuration that moves you from the second category to the first.
The parts nobody mentions
Release must be conditional on ownership. A naive DELETE lock_key will happily delete
a lock that has since been acquired by someone else after your lease expired. Store a unique
holder id in the value and delete only if it matches, atomically — which means a small
script, not two commands.
Renewal is not free safety. A background thread extending your lease sounds like the fix, and it fails in the same way: the thread is inside the process that is paused, so it stops renewing exactly when you stop working. It reduces spurious expiry for slow-but-alive work, which is worth having, and it closes nothing.
Lock granularity is a throughput decision. One global lock is simple and serialises your whole system. Per-entity locks scale and multiply the ways two workers can deadlock by taking them in different orders. If you need more than one lock at a time, define a global ordering and take them in it.
Every acquisition needs a timeout and a plan for failing to get it. “Block until available” turns a lock contention problem into an outage. Decide, per call site, whether not getting the lock means retry, skip, or fail the request.
The lock service is now a hard dependency. If it is down, does your system stop or does it proceed unprotected? Both answers are defensible and the default — whatever your client library does on timeout — is usually neither.
The short version
You cannot have a distributed mutex. You can have a lease, and a lease cannot stop a paused process from waking up after expiry and finishing its work, because the process has no way to learn that time passed. Shorter leases, ownership checks and renewal threads all fail to close that gap.
Fencing closes it: a monotonically increasing token issued with each grant, and a resource that refuses any write carrying a token lower than the highest it has seen. The enforcement has to live at the resource, and if your storage cannot do that, admit that your lock is best-effort rather than pretending otherwise.
For leadership, require a strict majority of the whole cluster, keep the cluster size odd, and accept that an even split means nobody leads — unavailability chosen over divergence. Use etcd or ZooKeeper rather than writing the consensus layer.
And before any of it, check whether a conditional write on a version column does the job, because most of the time it does, and it does it without a second system that can be down during your incident.