Distributed Coordination and Locking
Distributed locks coordinate processes accessing shared resources across multiple machines. They’re fundamental for preventing concurrent writes to the same record, ensuring single-leader behavior, and guarding non-idempotent operations.
The hard part: distributed environments don’t have shared memory or synchronized clocks. Network failures partition nodes without warning. Long GC pauses can make a healthy node look dead. Building a correct distributed lock is genuinely tricky.
Why It’s Hard
Common Pitfalls
The Implementation Options
RDBMS locks
Relational databases offer two approaches you can use without any extra infrastructure.
Best for: scenarios where all clients use the same database, no need for cross-DB coordination, simple use cases like “guard one scheduled job.”
Redis simple lock
The canonical Redis lock pattern:
SET resource_key my_random_value NX PX 30000
NX = set only if key doesn’t exist. PX 30000 = expire in 30 seconds. my_random_value = unique token chosen by client.
To release, use a Lua script that checks the value matches before deleting:
if redis.call("get", KEYS[1]) == ARGV[1]
then return redis.call("del", KEYS[1])
else return 0
end
Pros: Very fast, simple. Lock auto-expires if client crashes. Cons: Single Redis instance is a SPOF. Replication is asynchronous — failover can produce two clients thinking they hold the lock. No fencing token. No safety beyond the TTL.
Redlock
Redis’s multi-master lock algorithm. Uses N independent Redis nodes (typically 5):
- Client attempts
SET NX PXon each Redis master, recording timing. - If set succeeds on a majority (3 of 5) and total time < TTL, lock acquired.
- Effective validity = TTL − elapsed time.
- Failure → delete any partial locks, retry later.
The validity window in step 3 is the TTL minus the time spent acquiring the majority. That remainder matches reality only when clock drift between the client and each Redis master is small relative to the TTL, and when the acquire itself is not delayed for long. A clock step makes the remainder a lie: the keys may already be gone, or still alive, while the client believes the opposite, so two clients can both claim the majority. The algorithm also assumes those masters fail independently. A partition that hides a majority does not hand the lock to someone else; the client waits until the keys expire. Liveness is given up for the length of the lease. The random value is only an ownership check on delete. It does not increase, so a later holder cannot be ordered ahead of an earlier one.
ZooKeeper locks
ZooKeeper’s classic lock recipe uses ephemeral sequential znodes:
- Client creates
/locks/mylock/lock-NNNNNwithEPHEMERAL+SEQUENTIALflags. - Sequential flag appends a monotonically-increasing suffix.
- Client lists children, checks if its znode has the lowest number.
- If yes → it has the lock.
- If no → set a watch on the immediate predecessor, wait for it to be deleted.
- Release = delete the znode (or it auto-deletes when client disconnects).
The cost: you need to run and maintain a ZooKeeper ensemble.
etcd locks
etcd is a Raft-based KV store with built-in concurrency primitives. The Lock API uses a lease (TTL) plus an atomic compare-and-swap:
lease = etcd.leaseGrant(ctx, 10) // 10s TTL
txn = etcd.txn(ctx)
.If(compare(CreateRevision(key), "=", 0))
.Then(put(key, "locked", WithLease(lease.ID)))
.Commit()
This atomically creates the key under the lease if it didn’t exist. Lock held until Unlock or lease expires.
Fencing Tokens: The Safety Mechanism
A fencing token is a monotonically-increasing number issued with the lock. The protected resource (database, storage system) checks the token on every operation and rejects operations with stale tokens.
1. Acquire lock → get token N
2. Operation: write_data(payload, token=N)
3. Storage checks: latest_token >= N? Reject if smaller.
This protects against the “paused holder” problem. Even if your client paused for an hour past TTL, when it resumes and tries to write with token N, the storage rejects because someone else has token N+1.
The check on the resource is ordered. A stale token must not move the high-water mark:
if fencingToken <= lastToken:
reject the caller as a stale holder
apply the write
lastToken = fencingToken
Token 34, once accepted, makes a later write carrying 33 fail. The lock service cannot apply that rule on a store that ignores the number.
| System | What you send as the token | Ordered? |
|---|---|---|
| etcd | Creation revision of the lock key | Yes. The revision increases on every successful create. |
| ZooKeeper | zxid, or the znode version | Yes. The sequential suffix only orders who waits; the zxid orders the writes. |
| Consul session | LockIndex on the KV pair, with the session id | Yes. LockIndex increments on each acquire. The session TTL or health check releases the key if the holder dies. |
| DynamoDB conditional write | A version attribute on the item | Yes, if the write is conditional on the version. A mismatch fails with ConditionalCheckFailedException. |
Redis SET NX | The random value stored in the key | No. It only stops one client from deleting another’s key. |
| Hazelcast FencedLock | The token returned by the CP lock | Yes. The ordinary map lock in the same product does not return one. |
Firestore and Cloud Spanner do not hand the application a token. A read-write transaction locks the cells (Spanner at the row-and-column level) until commit, so the database itself is the mutex.
Choosing the Right Tool
| Use case | Recommendation |
|---|---|
| Simple mutex inside one Postgres app | Postgres advisory locks |
| Cache invalidation, transient locks | Redis simple lock with TTL |
| Multi-DB coordination, ok with rare anomalies | Redlock (with caveats) |
| Correctness-critical (financial, leader election) | ZooKeeper or etcd, with fencing tokens |
| Already running K8s | etcd Lock API (you have it anyway) |
Recap
- Local locks coordinate one process. Distributed locks coordinate machines that don’t share memory.
- The hard parts: partial failures, clock skew, network partitions, GC pauses, atomicity, fencing.
- Common pitfalls: no TTL, no fencing tokens, single point of failure, non-atomic acquire.
- Tools: RDBMS advisory locks (simple), Redis (fast but unsafe under adversarial conditions), Redlock (controversial), ZooKeeper / etcd (strongly consistent, with fencing).
- Fencing tokens are the difference between “looks safe” and “actually safe” — but require storage cooperation.
- For correctness-critical use cases, prefer ZooKeeper or etcd. For convenience, Redis is fine.