LeetBuild

Leases, claims and fencing

Work owned for a while: a claim that carries a deadline, a renewal only the holder can make, an expired claim handed back once, and a completion from a replaced worker refused.

The failure mode

A queue hands a job to a worker. The worker runs it, then says it is done. Between those two moments the job belongs to that worker, and everything that can go wrong with ownership goes wrong there:

  • The worker dies holding the job. If the claim has no deadline, the job is held forever.
  • The worker stalls (a garbage-collection pause, a swap storm, a partition) past its deadline. The job is handed to a second worker, and the first one wakes up still believing it owns it.
  • A second party moves the claim: another worker extends it, another reaper returns it, another caller releases it.
  • The worker fails halfway and never gives the claim back, so the slot it held stays taken until someone notices.

A claim that is meant to survive a crash is a lease: ownership with a deadline, renewable only by the holder, returned exactly once when the deadline passes, and checked on the way out so that a holder who was replaced cannot finish the work anyway.

The smallest instance is a job queue in Redis:

def claim(worker):
    job = r.lpop("jobs:queue")                                   # 1. take it off the queue
    if job is None:
        return None
    r.zadd("jobs:inflight", {job: now() + VISIBILITY_TIMEOUT_S})  # 2. remember the deadline
    r.hset(f"job:{job}", mapping={"state": "running", "worker": worker})
    return job

def ack(job, worker):
    r.hset(f"job:{job}", "state", "completed")                    # whoever asks

It works in every demo. It has three holes: a crash between lines 1 and 2 loses the job with no trace; a worker whose deadline passed and whose job went to someone else can still ack it; and the acknowledged job stays in jobs:inflight, so when its deadline passes the reaper runs it again.

How it shows up

The debugging courses that hold work for a while all start from a version of this:

  • Job Queue, step 2: the visibility timeout. The starter pops and records the deadline in separate round trips, two reapers that see one expired claim both requeue it, and an acknowledged job stays in jobs:inflight until it is run a second time.
  • Batch Inference Service, step 2: shard leases with claim, heartbeat, complete and reap. The heartbeat does not look at the owner, the reaper marks a dead worker's shard failed instead of handing it back, and the worker the reaper replaced can still complete.
  • Moderation Service, step 2: a case claimed by a moderator whose browser crashed is held forever, one moderator extends a case another holds, and a swept case is swept again on every pass.
  • Vision Inference, step 2: GPU slots as a sorted set. Members are scored by admission time instead of by lease expiry, so a crashed holder's slot never ages out, and release removes the lowest-ranked member rather than the caller's ticket.
  • Sandbox Runner, step 2: the runner trusts its own memory of when the lease started, so an operator who shortens it is ignored, and a result that arrives after the lease expired is published anyway.
  • Ticketing System, step 2: one agent puts down a ticket another one holds, because release deletes the lease without asking whose it is.
  • Media Transcoder, step 2: a segment claim without an expiry is held forever by a crashed worker, and a claim is not released when the encode raises.
  • Recommendation Ranking, step 2: a single-flight refresh under a lock, where the lock has to be released on the failure path too.

The symptoms rhyme across all of them: work that disappears (popped, never leased), work done twice (two holders, or a done job that comes back), work stuck (a claim nobody will ever release), and a result from a worker that no longer owned the job.

Pop quiz

A job's visibility timeout is 60 s. The worker that claimed it is paused for 90 s, the job is handed to a second worker, and the first worker wakes up and acknowledges it. What should the acknowledgement do?

The mechanism

A claim is a deadline, written in the same step as the take

A lease is two facts that must appear together: who holds the work and until when. In Redis the deadline usually lives in a sorted set scored by it (ZADD jobs:inflight <deadline> <id>), so "what expired" is one range query, and the owner lives beside it in a hash. The take and both facts are one Lua script, so no crash can land between them:

-- claim: KEYS = jobs:queue, jobs:inflight, jobs:owner, jobs:epoch
-- ARGV = now_ms, lease_ms, worker
local id = redis.call('LPOP', KEYS[1])
if not id then return false end
local token = redis.call('HINCRBY', KEYS[4], id, 1)          -- the fencing token
redis.call('ZADD', KEYS[2], tonumber(ARGV[1]) + tonumber(ARGV[2]), id)
redis.call('HSET', KEYS[3], id, ARGV[3] .. ':' .. token)      -- owner = worker:token
return {id, token}

A claim on a single key is the same idea in one command: SET lease:{id} <owner> NX PX 90000 takes the key only if it is free and gives it its expiry in the same write. SET NX followed by EXPIRE is the bug the Sandbox Runner starter ships: a runner that dies between them leaves a lease that never expires.

Renewal only by the holder, checked in the same script

A long job heartbeats: it pushes its deadline forward while it is still alive. The renewal compares the owner and moves the deadline in one script; a comparison in the client followed by a ZADD is a check-then-act race, and a renewal that does not compare at all lets anyone keep anyone's job.

-- renew: KEYS = jobs:inflight, jobs:owner   ARGV = id, worker:token, now_ms, lease_ms
if redis.call('HGET', KEYS[2], ARGV[1]) ~= ARGV[2] then return 0 end
local deadline = tonumber(redis.call('ZSCORE', KEYS[1], ARGV[1]) or 0)
if deadline <= tonumber(ARGV[3]) then return 0 end            -- already expired: too late
redis.call('ZADD', KEYS[1], 'XX', tonumber(ARGV[3]) + tonumber(ARGV[4]), ARGV[1])
return 1

The second test matters as much as the first. A lease whose deadline has passed is already gone, even if the reaper has not run yet; renewing it would let a worker resurrect a claim the reaper may hand out a millisecond later. A worker whose renewal is refused stops working.

Reaping once, guarded by the removal

The reaper finds the claims whose deadline passed and puts them back. Two reapers (or a reaper and a late acknowledgement) will look at the same expired claim. The rule that keeps the hand-back single is: whoever removes the claim hands it back, and the removal answers whether you were that one.

-- reap: KEYS = jobs:inflight, jobs:owner, jobs:queue   ARGV = now_ms
local due = redis.call('ZRANGEBYSCORE', KEYS[1], '-inf', ARGV[1], 'LIMIT', 0, 100)
local n = 0
for _, id in ipairs(due) do
  if redis.call('ZREM', KEYS[1], id) == 1 then                -- I removed it, so I return it
    redis.call('HDEL', KEYS[2], id)
    redis.call('RPUSH', KEYS[3], id)
    n = n + 1
  end
end
return n

Inside one script nothing else runs between the range and the removal, so every ZREM returns 1. The guard is what keeps a reaper that runs in the client correct: ZRANGEBYSCORE, then one ZREM per id, then RPUSH only when that ZREM returned 1. The losing reaper sees 0 and moves on. The same guard sits in the acknowledgement: an ack that ZREMs the job from jobs:inflight and gets 0 knows the claim is gone, and answers false.

An expired claim goes back to the queue, not to failed: the worker died, the job did not. A dead-letter decision belongs to an attempt counter, not to one missed deadline.

Fencing: the holder proves it is still the holder

A deadline guarantees that a dead worker's job comes back. It cannot guarantee that the old worker is dead. Nothing stops a paused process from resuming after its lease expired and finishing the job it no longer owns:

Sequence of calls for this stepWorker ARedisWorker BOutput storeclaim shard 7 · lease 30 sshard 7, token 33,deadline t+30GC pause 40 s: noheartbeat, A stillthinks it owns 7reap at t+31 · ZREM shard7 → 1 → RPUSHclaimshard 7, token 34write shard 7 · token 34ok, highest token is now34write shard 7 · token 33(A wakes up)refused: 33 < 34complete shard 7 as A:330: the owner is B:34
— request- - reply· · fire-and-forget

The fencing token is a number that grows with every claim of the same work (HINCRBY jobs:epoch above, a ZooKeeper znode version, a database row version). The holder passes it with every effect, and whatever receives the effect refuses a token lower than the highest it has seen:

UPDATE shard_output
   SET body = $1, token = $2
 WHERE shard_id = $3 AND token < $2;    -- 0 rows: a newer holder already wrote

For effects that stay inside Redis, the owner check in complete is the fence: the stored owner is worker:token, and a completion from A:33 when the owner is B:34 changes nothing.

-- complete: KEYS = jobs:inflight, jobs:owner, jobs:outcome   ARGV = id, worker:token, outcome
if redis.call('HGET', KEYS[2], ARGV[1]) ~= ARGV[2] then return 0 end
redis.call('ZREM', KEYS[1], ARGV[1])                          -- no deadline left behind
redis.call('HDEL', KEYS[2], ARGV[1])
redis.call('HSET', KEYS[3], ARGV[1], ARGV[3])
return 1

Pop quiz

The reaper runs in the client: ZRANGEBYSCORE jobs:inflight -inf now, then for each id RPUSH jobs:queue id and ZREM jobs:inflight id. Two reaper processes run at once. What goes wrong, and what is the smallest fix?

Releasing on every path

A lease that is released by the normal path only is released by the normal path only. The failure path is the one that runs when something has already gone wrong, so it is the one most likely to leak a slot:

token = secrets.token_hex(16)
if not r.set(lock_key, token, nx=True, ex=LOCK_TTL_S):
    return stale_copy_or_none()              # someone else is refreshing
try:
    values = feature_service.user_features(tenant, user_id)
    write_back(values)
    return values
finally:
    release_if_mine(keys=[lock_key], args=[token])   # compare-and-delete, success or not

release_if_mine is the four-line script from the Redis docs (GET, compare, DEL), or DELEX key IFEQ token on Redis 8.4 and later. A plain DEL in the finally would delete the lock of whoever took it after yours expired. The expiry is still there as the backstop for the path finally cannot cover: the process killed outright.

The same goes for counted slots. A GPU slot taken with a ticket must be given back by that ticket (ZREM gpu:{tenant} <ticket>), from a finally, and the set must be scored by the lease's end so that ZREMRANGEBYSCORE gpu:{tenant} -inf now returns a crashed holder's slot.

Leases versus locks

A lock says "only one at a time". A lease says "only one at a time, until a deadline". In a single process a lock is enough, because the holder cannot vanish while the lock outlives it. Across machines the holder can vanish, so every useful distributed lock is a lease, and inherits a lease's problem: expiry is decided by time, and time cannot tell a dead holder from a slow one.

Lock (in one process)Lease (across machines)
Holder crashesthe lock dies with the processthe deadline returns the work
Holder stallseveryone waitsthe work moves on; the stalled holder must be fenced
Releaseby the holderby the holder, compared by token, or by expiry
Renewalnot neededa heartbeat, only by the holder, refused once expired
What it guaranteesmutual exclusionmutual exclusion while clocks and pauses behave, plus fencing for when they do not

Kleppmann's distinction decides how much of this you need. A lock for efficiency (one refresh instead of fifty, as in a single-flight cache fill) can fail now and then: the cost is a duplicate call. A single Redis key with SET NX PX and a compare-and-delete is enough. A lock for correctness (one charge, one file write, one encoded segment in a manifest) cannot: it needs the fencing token checked by the resource, because no lease alone is safe against a pause.

Pop quiz

A cache refresh takes SET lock:features:t1:u9 1 NX EX 5 so that one caller refreshes a cold key while the others serve the stale copy. Which of these is a real flaw?

The fix and its trade-offs

The pattern, whatever stores it:

  1. Claim with a deadline, take and record in one step: one script, or SET … NX PX.
  2. Renew only as the holder, compared in the same script as the move, and never after the deadline.
  3. Reap by removal: whoever removes the expired claim hands it back, to the queue, once.
  4. Fence the exit: completion and any external effect carry the token; the receiver refuses an older one.
  5. Release on every path, compared by token, from a finally; leave the expiry as the backstop.
  6. Leave nothing behind: completion removes the deadline, so the reaper cannot find a finished job.

What it costs:

  • Lease length is a trade. Short leases return a dead worker's work quickly and turn every slow worker into a duplicate; long leases do the opposite. A heartbeat lets the lease be short and the job long, at the price of one write per interval per job. SQS's guidance is the same: start with a timeout close to the typical processing time and extend it while processing.
  • Fencing needs the receiver's help. A store that cannot compare a token (an email provider, a payment API) cannot be fenced; there the fallback is an idempotency key the provider honours, so the duplicate effect is at least collapsed.
  • At-least-once is the honest promise. A lease turns "lost" into "maybe done twice". The work behind it has to tolerate a second run (idempotent writes, a dedupe key), because a worker can finish its effect and die before it completes the lease.
  • One Redis is one point of truth. A single instance with async replication can lose a lease on failover and grant it twice. For efficiency locks that is acceptable; for correctness, the fence is what makes it acceptable.

Proving it: a paused worker on a virtual clock

A lease bug only shows when time passes at the wrong moment, so the test must own time. Every operation takes now from a clock the test advances; nothing sleeps. Then the paused worker is not a thread you hope will be slow enough: it is a worker that simply makes no call while the test moves the clock past its deadline.

def test_a_paused_worker_is_replaced_and_fenced(r):
    clock = FakeClock(start_ms=1_000_000)
    q = ShardQueue(r, now=clock.now, lease_ms=30_000)
    q.enqueue("shard-7")

    a = q.claim("A")                          # token 1, deadline 1_030_000
    clock.advance(31_000)                     # A is paused: no heartbeat, no call
    assert q.reap() == 1                      # handed back once
    assert q.reap() == 0                      # and not twice
    b = q.claim("B")
    assert (b.id, b.token) == ("shard-7", 2)

    assert q.renew(a) is False                # A wakes up: no renewal
    assert q.complete(a, "done") is False     # and no completion
    assert q.complete(b, "done") is True

    assert q.outcome("shard-7") == "done"
    assert r.zcard("jobs:inflight") == 0      # no deadline left behind
    assert r.llen("jobs:queue") == 0          # nothing will run it again
    clock.advance(60_000)
    assert q.reap() == 0

Then the paths around it:

  • Renew before and after the deadline. At deadline - 1 the holder's renewal succeeds and the deadline moves; at deadline it is refused, even though the reaper has not run.
  • Renew by someone else. B's renewal of A's live lease answers false and leaves A's deadline where it was.
  • Two reapers. Run the client-side reaper's steps interleaved by hand: both read the range, both try to remove; assert exactly one push.
  • Crash inside the claim. Fail the connection between the take and the deadline (a fake that raises after LPOP) and assert the job is in the queue or in flight, never in neither. With one script this passes trivially, which is the point.
  • Release on the error path. Make the dependency raise inside the try, then assert the lock key is gone, and that a second caller can take it without waiting for the expiry.
  • Release by a stranger. Take the lock as A, let it expire, take it as B, release as A; assert B still holds it.

Assert outcomes: who owns the work, what the store holds, how many times the work ran. A test that greps the script for ZREM, or that sleeps 31 real seconds and passes because the machine was fast, proves nothing about the next pause.

Pop quiz

Which test catches a heartbeat that renews without comparing the owner?

Practise it

21 steps start from a starter that hides this failure mode, in 19 courses.

Sources