Course outline

Redis / Advanced: persistence, distribution and operations / Lesson 21

Lease ownership and safe release

Reason about a time-limited single-instance lease and its failure boundaries.

About 4 min readContent reviewed
On this page

Lesson goals

  • Explain conditional acquisition and unique owner tokens.
  • Release only a matching lease atomically.
  • Recognize expiry, failover, and stale-worker limitations.

Define the lease and its limits

A time-limited lease can coordinate a short task through a key, but the application must define what happens after expiry, client pauses, network failures, and server failover. A single-instance example does not establish a distributed lock guarantee for every topology. For correctness-critical resources, evaluate a system with appropriate coordination and fencing semantics; a random ownership token alone does not stop an expired worker from writing elsewhere.

Acquire a disposable lease

The illustrative command below is for an isolated Redis 8.2 lab. NX requests creation only when the key does not exist, and PX sets a finite lifetime in milliseconds. A real client must generate a unique, unpredictable token for each acquisition attempt and check the actual response and elapsed time. The fixed token is synthetic and must not be reused as a real ownership token.

redis Request a short lab lease
SET lab:lease:report token-for-this-lab NX PX 30000

This attempts to create lab:lease:report for 30,000 milliseconds only if absent. Acquisition is successful only when the server reports success; no live result or timing guarantee is asserted.

Release only the current owner token

A client can pause beyond expiry, after which another client may acquire the same key. Blindly deleting the key could remove the new owner’s lease. The following single-instance Redis 8.2 example atomically compares the current value and deletes only on a match. It is illustrative, uses the same synthetic token, and must be evaluated with the permissions and failure behavior of the deployed client.

redis Compare and release atomically
EVAL "if redis.call('get', KEYS[1]) == ARGV[1] then return redis.call('del', KEYS[1]) else return 0 end" 1 lab:lease:report token-for-this-lab

The script returns the deletion result when the value matches and zero otherwise. It protects against deleting a different owner’s current key; it does not fence a stale client from an external resource.

Reason about expiry and failover

A successful acquisition is valid only for its limited interval under the stated assumptions. A paused client must not assume that it still owns the lease after that interval, and an uncertain response needs reconciliation. Replication and failover can introduce further failure modes; assess the exact deployment and chosen algorithm rather than applying a single-instance guarantee to a distributed service. Define how the protected resource rejects stale work when correctness requires it.

Exercise the failure timeline

In a disposable lab, simulate one client acquiring a lease, pausing until expiry, and another client acquiring a new token. Verify that the first client’s compare-and-release does not delete the second token. Separately test a lost acquisition response, task overrun, and failover under the documented application policy. Measure elapsed time and cleanup behavior; never rely on the illustrative token or fixed duration as a production setting.

Common errors and fixes

An expired client deletes another client’s lease.

Why it happens
It issues an unconditional DEL after its own lease expired.
How to fix it
Compare the owner token and delete atomically.
Prevent it next time
Test the expiry and reacquisition timeline.

Two workers affect the protected external resource.

Why it happens
A paused worker continues after lease expiry without fencing.
How to fix it
Use an appropriate coordination and fencing design for the resource.
Prevent it next time
Test stale workers and define the correctness requirement.

A client assumes it acquired a lease after a timeout.

Why it happens
The server response was lost and the outcome is uncertain.
How to fix it
Follow the client’s bounded reconciliation policy before doing protected work.
Prevent it next time
Exercise lost-response behavior in a disposable test.

2 practice exercises

1. Why is the fixed token in this lesson unsuitable for real acquisitions?

Show worked solution

Different requests must have unique owner tokens so a stale client cannot match a later owner by accident. A real client generates a suitable unpredictable value for each attempt.

2. What happens if the stored token differs when the release script runs?

Show worked solution

The comparison fails and the script returns zero without deleting the key. This protects the other token but does not prevent stale writes to an external resource.

Key takeaways

  • Acquire conditionally with a finite lifetime and unique token.
  • Compare and release atomically, never blindly delete another owner’s lease.
  • A lease alone does not provide external fencing or universal failover safety.

Official references