Bloqueo Distribuido
Lease · Token de fencing
Escrito por Darjus Kazakevic · revisado el
A lease plus a fencing token. A lock alone is not enough across a network.
A distributed lock lets one process at a time do a task across many machines: run the nightly export, or write one customer's ledger. The lock lives in a shared store such as etcd, ZooKeeper, Consul, Redis or Postgres. Whoever creates the key first holds it. Every lock must also expire, or a crashed holder would keep it forever.
Expiry creates the real problem. A holder can pause for longer than its lease: a garbage-collection stop, a slow disk, a network partition. The lease ends, another process takes the lock, and the first one wakes up and writes. The fix is a fencing token: a number that rises with every grant, checked by the resource itself.
En esencia — Grant the lock as a lease with a rising token, and make the protected resource reject any token older than the newest it has seen.
Cuándo recurrir a él
Recurre a él cuando
- One node at a time may write a resource, such as a customer's ledger
- A scheduled job runs on every node but must run only once
- Expensive work, such as a large report, should not be done twice
Reconsidéralo cuando
- Correctness depends on it, but the resource cannot check a token
- The data lives in one database: a row lock or version check is enough
- The work is idempotent, so running it twice is harmless
Conceptos clave
- Lease
- A lock with an expiry, or TTL (time to live). The holder renews it while it works. If the holder crashes, the lease ends and the lock frees itself.
- Fencing token
- A number that rises with every grant. The holder sends it with each write, and the resource rejects any token lower than the highest it has accepted. In etcd the lock key's revision is a good token.
- Efficiency or correctness
- An efficiency lock stops duplicate work; if it fails now and then, you pay twice. A correctness lock protects data; if it fails, data is corrupted. Only a correctness lock needs fencing and a consensus store.
- Redlock and its critique
- Redlock takes the same key on a majority of 5 independent Redis nodes. Martin Kleppmann showed it has no fencing token and relies on timing, so a pause or clock jump lets two clients hold it. Redis's author disputes this.
- Postgres advisory locks
- When the data is in one Postgres database, pg_try_advisory_xact_lock(key) gives a lock with no extra system. It belongs to the transaction, so a commit, a rollback or a dropped connection frees it.
Reglas de diseño
- Fence at the resource, not the lock
- A lease can expire while its holder is paused, so two processes will believe they hold it. Send a rising token with each write, and have the resource refuse any token older than the highest it has seen.
- Make every lock a lease
- Give each lock an expiry and renew it while the work runs. Expiry is the only way a crashed holder ever lets go, and it is the reason you need the fencing token.
- Use a consensus store for correctness
- etcd, ZooKeeper and Consul keep the lock on a majority of nodes. A single Redis node can lose a lock on failover, because it copies data to its replica asynchronously.
- Prefer the database's own lock
- If the protected data is in one database, use a row lock, a version check or a Postgres advisory lock. One system is easier to reason about than two.
Errores habituales
- No fencing token. Two holders write after a GC pause, and the bug appears only under load.
- SETNX on a single Redis node for correctness. After a failover the replica may not have the key, so a second client gets the lock.
- Deleting the key without checking the owner. A slow holder releases a lock that another client now holds. Delete only if the value is still yours.
- A TTL shorter than the task. The lock expires halfway through, and a second worker starts the same job.