October DealsAmazon USOctober deal check: compare before you payAmazon US: current deals, useful picks and tech finds.Check DealsClean PCRecommendedOne scan can reveal what keeps slowing WindowsLook for cleanup and repair opportunities.Run ScanOctober DealsAmazon USDeal season is back - check today's better picksAmazon US: current deals, useful picks and tech finds.See Picks×
Skip to content
World desk7 min

Distributed Locking in Practice: Guarantees, Failure Scenarios, and Better Alternatives

A distributed lock coordinates clients but cannot alone stop an expired owner from writing. Learn how fencing tokens, resource-side checks, and alternative designs address that risk.
Special offer. See more information about Outbyte and uninstall instructions. Please review EULA and Privacy policy.

A distributed lock coordinates clients, but it does not automatically prevent an expired or paused client from writing to the protected resource. If correctness depends on exclusive access, the resource must reject stale owners—for example, by checking a monotonically increasing fencing token—or the operation must be serialized by a transaction that covers the actual shared state.

What a distributed lock does—and what it does not

A lock service records which client currently holds a lock, often for a limited lease period. That coordination can reduce overlapping work, but the service’s ownership record is not itself a barrier in front of another system’s storage. Unless the protected resource participates in the coordination or validates each operation, it may accept writes from a client whose lease has expired.

As an Amazon Associate I earn from qualifying purchases.

Redis frames lock design in terms of safety and liveness. Mutual exclusion is the safety property: owners should not act as though they hold the lock at the same time. Deadlock freedom and fault tolerance are liveness goals: the system should not remain stuck forever and should continue to make progress under its assumed failures. These are design criteria, not unconditional guarantees for every implementation or timing model. Redis’s live documentation describes a TTL-based algorithm; the client has only the remaining validity window in which to do its work.

Free tools Windows power users keep installed

One-click scans. No signup required.

Special offer. See more information about Outbyte and uninstall instructions. Please review EULA and Privacy policy.

How a lease expires while its former owner is still running

  1. Client A acquires a lock with a time-to-live.
  2. A is paused, or its requests are delayed by the network, long enough that the lease expires.
  3. Client B acquires the now-available lock and writes to the resource.
  4. A resumes and sends a write it prepared while it believed it owned the lock.

The lock service can have followed its configured expiry rules correctly, while the resource still receives operations from two successive owners. A TTL helps the system recover when a client crashes and cannot release its lock, but expiry creates a boundary: a paused process can continue from its own perspective after the resource should consider its lease over. Redis’s documentation and Martin Kleppmann’s 2016 analysis both address this distinction.

Fencing tokens: make the resource reject stale owners

A fencing token is an increasing value issued with each lock acquisition. The protected resource remembers the greatest token it has accepted and rejects an operation carrying an older one. If A holds token 41, its lease expires, and B acquires token 42, the resource can accept B’s operation and reject A’s delayed operation when it arrives.

As Kleppmann puts it, “The fix for this problem is actually pretty simple: you need to include a fencing token with every write request to the storage service.” The qualification is essential: the resource must actually validate the token. Every operation performed under the lock needs to carry it, and the resource must compare it against the greatest accepted value. Merely generating increasing values at the lock service does not fence stale writes if the target ignores them.

Kleppmann identifies ZooKeeper transaction IDs or znode versions as possible token sources in the setup he describes. Whatever source is used, the important property is that tokens increase across acquisitions and that the target enforces their order.

Special offer. See more information about Outbyte and uninstall instructions. Please review EULA and Privacy policy.

How to do distributed locking

  1. Define the protected operation. Identify the shared state that must not be corrupted by overlapping work, including any side effects outside the database or service that stores the lock.
  2. Decide what failure would cost. If duplicate execution is merely wasteful, coordination can be an efficiency measure. If a stale write can damage or corrupt state, assume a lease alone is insufficient.
  3. Put enforcement at the resource when stale writes matter. Use monotonically increasing fencing tokens and make the target reject older tokens, or use a transaction/serialization mechanism that covers the actual shared operation.
  4. Choose coordination with its failure boundary in mind. A Redis lock, a consensus-backed service, a database transaction, and a queue-based design make different trade-offs in availability, complexity, and what they can protect.
  5. Define recovery behavior. Decide what happens when a client pauses, loses its lease, cannot reach a quorum, or retries work whose outcome is uncertain. Make duplicate work harmless where feasible.

Use ownership-safe acquisition and release for a Redis lock so one client does not release another client’s lock. Treat such a lock as approximate under failures, not as sufficient protection for correctness-sensitive writes.

Redis Redlock and the disagreement about correctness

Redis documents Redlock as a multi-node design intended to be safer than a basic single-instance approach. Redis presents mutual exclusion, deadlock freedom, and majority-based fault tolerance among its goals. Those are Redis’s stated design properties and should be understood under the algorithm’s assumptions, including its time-limited validity window.

Kleppmann’s 2016 analysis reaches a different conclusion for work whose correctness depends on the lock. He argues that arbitrary process pauses, delayed packets, and clock behavior can violate the timing assumptions on which Redlock depends, and that Redlock does not provide monotonically increasing fencing tokens. On that analysis, a stale client can still send an operation after another client has acquired the lock. This is Kleppmann’s critique, not a conclusion endorsed by the Redis documentation.

The practical question is not simply whether a lock service reports one current owner. It is whether the system that accepts the protected write can detect and reject a former owner. For correctness-sensitive work, arrange that enforcement at the resource or use a transaction boundary that includes the resource.

Special offer. See more information about Outbyte and uninstall instructions. Please review EULA and Privacy policy.
Independent reader supportYour contribution helps us test, update, and keep practical guides available for everyone.Support on Ko-Fi

Which alternative fits the failure you need to handle?

Approach Pause or network delay Stale-owner enforcement Availability during quorum loss Complexity and fit Transaction boundary
Best-effort Redis lock A client can outlive its TTL from the resource’s perspective; overlap is possible under failures. Not provided by the lock alone. Pair with target-side token validation if stale writes must be rejected. Redis’s documentation describes majority-based fault tolerance for Redlock; availability under a particular failure scenario depends on the configured design. Useful when the lock is an efficiency optimization and occasional overlapping work is tolerable. Ownership-safe acquisition and release are important. The lock service and protected resource are not thereby placed in one transaction.
Consensus-backed coordination such as etcd Consensus-backed coordination provides documented consistency and coordination primitives, but lease ownership does not by itself stop a stale client from writing to an external resource. Use resource-side token validation where stale writes matter; etcd lease ownership alone is not external-resource ownership. etcd’s v3.5 failure-mode documentation says recovery from majority failure requires a majority of members to become available. Useful when coordination semantics are needed and the operational trade-off is acceptable; it adds a coordination system to operate. Coordination and an external resource are not automatically covered by a shared transaction.
Database transaction or resource-native serialization Can protect correctness when the transaction semantics cover the shared state and operation; behavior for external side effects is not established by a database lock alone. Enforcement is within the resource/transaction boundary. A separate external target still needs its own protection. Availability during quorum loss: not stated by Kleppmann’s article for this general alternative. Kleppmann recommends a database with reasonable transactional guarantees for correctness-dependent work. The transaction must cover the relevant operation. Best aligned when the protected state and operation fit inside the same transaction boundary.
Idempotent work or queue-based serialization Can reduce harm from retries or duplicate execution; it does not inherently prevent a stale operation from reaching an unprotected resource. Stale-owner token enforcement: not stated as an inherent property of this approach. Design the target-side checks if needed. Availability during quorum loss: not stated in the cited sources for this general alternative. Consider when duplicate work can be made harmless or a serialized work-claim pattern can replace a broad lock. No performance comparison is established here. Whether a queue, claim, and target operation share a transaction depends on the system design.

The etcd documentation describes operations as completing after consensus commit and documents leases and locks as coordination primitives. Its comparison documentation also cautions that owning a lease alone does not guarantee ownership of an external resource. Therefore, consensus-backed coordination can strengthen the coordination layer without removing the need to protect the target from stale writes.

Questions to answer before choosing

  • Can stale work cause damage? If not, a best-effort lock may be a reasonable efficiency tool. If yes, require enforcement at the resource or a transaction that covers the protected operation.
  • Can the target validate an increasing token? If it can, fencing directly addresses delayed writes from earlier owners. If not, determine whether resource-native transactions or serialization can cover the operation.
  • Must work continue when a quorum is unavailable? Consensus-based recovery can require a majority of members to be available, as etcd’s v3.5 failure guide states. Decide whether that trade-off is acceptable for the workload.
  • Can duplicate work be made harmless? Idempotency or serialized work claims may be simpler than a broad distributed lock when they fit the operation.
  • Do coordination and protected state share a transaction boundary? If not, the lock API cannot by itself make an external side effect atomic with lock ownership.

Sources and further reading

Redis’s live “Distributed Locks with Redis” documentation describes its algorithm and intended properties. Kleppmann’s “How to do distributed locking,” published February 8, 2016, explains the stale-client failure and fencing-token argument. etcd’s v3.4 “etcd API guarantees” and v3.5 “etcd versus other key-value stores” documentation describe its consistency and coordination primitives; its v3.5 “Failure modes” guide addresses recovery after majority failure. For a broader treatment of distributed systems, Kleppmann’s Designing Data-Intensive Applications is a relevant further read.

Product prices and availability are accurate as of the date/time indicated and are subject to change. Any price and availability information displayed on Amazon at the time of purchase will apply.

Leave a Reply

Your email address will not be published. Required fields are marked *

Special offer. See more information about Outbyte and uninstall instructions. Please review EULA and Privacy policy.

More from the Wire

  1. World desk4 min
    How to Spot an AI Voice Scam Before Sending MoneyDon’t rely on how a caller sounds. Pause, call back through a known number, and verify the emergency with another trusted person before sending money.
  2. Mountain View desk4 min
    Google’s SynthID Detector: How to Check AI-Generated Images, Video and AudioGoogle’s SynthID Detector looks for an embedded watermark in supported images, video and audio. Here is what its results do—and do not—show.
  3. Redmond desk20 min
    How to create a link to File or Folder in Windows 11Windows 11 gives you several ways to point to a file or folder without moving or duplicating it. You can create a desktop shortcut,…
Recommended PC Tool
Recommended PC Tool
Crashes, No Sound, or Screen Glitches?Free driver scan
Windows Errors? Fix Them Before They SpreadFree repair scan

Two free Windows tools

One Free Minute Could Fix That PC

Before you go - each of these free tools takes about a minute and tackles what quietly slows a Windows PC down.

Special offer. View Outbyte info, uninstall instructions, EULA, and Privacy Policy.