Let only one machine do a job at a time, across many machines (and why that is hard).
A distributed lock lets only one machine at a time do a certain job or touch a certain piece of data, even when many machines run the same code.
Inside one program, a mutex stops two threads from changing the same thing together. With many servers, there is no shared memory, so a mutex does not work. If two servers both read a balance of 100, both subtract 30 and both write back 70, you lost a payment. You need something that all servers agree on to say "it is my turn".
A distributed lock is a record in a shared store: "resource X is owned by server A until time T". A server acquires the lock by creating that record if nobody else has. It releases it by deleting the record. If it crashes, the record expires after its time-to-live (TTL), so the system does not get stuck forever.
That expiry is also the source of the hardest problem. The owner might be slow, not dead: a long garbage collection pause, a busy CPU or a network delay. Its lock expires, another server takes it, and then the first one wakes up and keeps working, still believing it holds the lock. Now two servers act at once.
| Option | How it works | Good and bad |
|---|---|---|
| Database lock (SELECT FOR UPDATE, advisory lock) | Use a lock inside the database that already stores the data | Simple and strongly consistent with the data. Does not span several databases and holds a connection |
| Redis single instance (SET NX PX) | Set a key only if it does not exist, with an expiry. Release with a check on the owner value | Very fast and easy. If Redis fails over, the lock can be lost or given twice |
| Redlock (several Redis nodes) | Take the lock on a majority of independent Redis nodes | More fault tolerant. Depends on clocks and has no fencing token, so critics say it is unsafe for correctness |
| ZooKeeper (ephemeral nodes) | Create an ephemeral sequential node. The lowest number owns the lock. It vanishes if the session dies | Strong, with ordering you can use as a token. A heavier system to run |
| etcd or Consul (lease + key) | Create a key attached to a lease. A revision number acts as a fencing token | Built on Raft. Good safety. A new dependency |
| Cloud services (DynamoDB conditional writes, GCS, Kubernetes Lease) | A conditional write to a managed store | Little to run. Tied to the cloud product |
| Goal | Meaning | What you need |
|---|---|---|
| Efficiency | Avoid doing the same work twice (for example sending the same email twice would just waste money) | A simple lock is fine. An occasional double run is acceptable |
| Correctness | Two owners would corrupt data | A real consensus-based lock with fencing tokens, or better, avoid the lock with a transaction or a conditional write |
A freezes right after taking the lock (a long GC pause, a slow disk or a network stall). It cannot renew the lease while frozen. B retries every second.
| Term | Meaning in easy words |
|---|---|
| Mutex | A lock that lets one thread at a time into a section of code |
| Critical section | The code that only one owner may run at a time |
| Lease | A lock that is valid only for a limited time |
| TTL (time to live) | How long a lock record lives before it expires by itself |
| Acquire / release | Take the lock, and give it back |
| Renewal / heartbeat | Extending the lease while the owner is still working |
| Fencing token | A number that rises with each lock grant. Storage rejects lower numbers |
| Ephemeral node | A ZooKeeper entry that is removed when the session of its owner ends |
| Redlock | A Redis algorithm that takes the lock on a majority of independent nodes |
| Deadlock | Two owners wait for the locks of the other forever |
| Starvation | A waiter that never gets its turn because others keep winning |
| Choice | We gain | We lose |
|---|---|---|
| Long TTL | Fewer expiries during slow work | A crashed owner blocks others for longer |
| Short TTL | Quick recovery after a crash | A slow owner loses the lock mid-job, so you need renewals |
| Redis lock | Speed and simplicity | Weaker safety on failover or pauses |
| etcd or ZooKeeper lock | Strong safety and tokens | More to run and a little slower |
| Fencing tokens | Safe even with paused owners | The storage must check tokens, which not all systems can do |
| No lock, use a conditional write | No extra system and no expiry problem | Only works if the resource supports it |