Distributed systems do not have a reliable way to distinguish a slow process from a dead one. A worker can be paused by the scheduler, stuck in a garbage collection pause, partitioned from the network, or simply running behind. From the outside, all of these look like silence.

The useful question is not "is this worker alive?" It is "is this worker still allowed to act?"

That is the difference between a health check and a lease.

Timeline showing a worker claiming a lease, heartbeating, expiring, and a new worker claiming the same execution while the old worker's result is rejected.
Figure 1 — A lease is a time-bounded claim. When it expires, the execution can be claimed again; the old worker's late result is fenced off.

A process can be paused, not dead

The scheduler's first version treated worker liveness as a boolean. A worker registered, received work, and reported results. If it stopped reporting, the coordinator eventually assumed it was gone and reassigned the execution.

The problem appeared during a failure drill. A worker was paused long enough for its lease to expire and for a second worker to claim the same execution. Then the first worker resumed and published its result.

The coordinator had no way to tell which result was current. The first worker had not crashed. It had simply lost the argument about who owned the execution.

A boolean cannot represent that state. Ownership needs a deadline and a generation.

The lease is a deadline, not a status

A lease is a row that says: this execution is claimed by this worker until this timestamp. The claim is conditional on the lease being absent or expired.

In the scheduler, the claim is a single conditional update against PostgreSQL:

UPDATE executions
SET
  worker_id = $1,
  lease_token = $2,
  lease_expires_at = now() + interval '60 seconds',
  attempt = attempt + 1
WHERE id = $3
  AND status = 'pending'
  AND (lease_expires_at IS NULL OR lease_expires_at < now())
RETURNING attempt, lease_token;

If the update returns a row, the worker owns the execution for the next sixty seconds. If it returns nothing, someone else owns it, or the execution is no longer pending. The worker does not need a separate lock service, and it does not need to trust its local clock for the comparison. The database applies now().

The lease is not a status field that someone remembers to clear. It is a time-bounded permission that expires on its own.

Fencing: rejecting the stale write

A deadline alone is not enough. The old worker can still finish, publish a result, and overwrite state after a new worker has taken over.

The lease token is the fence. Every result write includes the token that was issued with the claim:

UPDATE executions
SET
  status = 'succeeded',
  result = $4,
  completed_at = now(),
  lease_token = NULL,
  lease_expires_at = NULL
WHERE id = $1
  AND lease_token = $2
  AND attempt = $3
RETURNING id;

If the old worker's token no longer matches, the update affects zero rows. The result is recorded as a late attempt, not applied to the execution. The new worker's result remains authoritative.

This is the same idea as a fencing token in a storage system, expressed as a conditional write. The old worker is not killed. It is simply not allowed to change the state it no longer owns.

Heartbeats and the clock you do not control

A sixty-second lease with no renewal is a sixty-second timeout. Long-running commands need a heartbeat that extends the lease while work is still progressing.

The worker sends a heartbeat every twenty seconds:

UPDATE executions
SET lease_expires_at = now() + interval '60 seconds'
WHERE id = $1
  AND lease_token = $2
  AND status = 'running';

Two details matter here.

First, the heartbeat is conditional on the token. A worker that has already been superseded cannot extend a lease it no longer holds.

Second, the lease duration must be longer than the heartbeat interval by enough margin to survive scheduling jitter. Twenty seconds between heartbeats and a sixty-second lease gives two missed beats before expiry. That margin is a design parameter, not a constant to copy.

Wall-clock skew is a real hazard, but it is not solved by trusting the worker's clock. The database is the clock for lease comparisons. Workers should treat expiry as an estimate and always let the conditional write decide ownership.

Reaping is reconciliation

An expired lease does not automatically run again. Something has to notice it.

The coordinator runs a reconciliation pass that looks for executions whose lease has expired and whose status is still running. It marks the attempt as abandoned and returns the execution to the pending pool. The next claim increments the attempt counter.

This is deliberately boring. Reconciliation is a query, not a distributed consensus protocol. It can run more than once. It can run late. The conditional claim prevents two reapers from handing the same execution to two workers.

The pass also records why the attempt ended: lease expired, worker reported failure, or result rejected. That history is what makes the system debuggable later.

Attempt history is the debugger

Every claim writes an attempt row. Every heartbeat can update its last-seen time. Every result, accepted or rejected, is recorded against the attempt.

When an execution behaves strangely, the history answers the questions that logs cannot:

QuestionWhere the answer lives
Who claimed the execution first?Attempt rows, ordered by started_at
Did the worker heartbeat?last_heartbeat_at on the attempt
Why was the result rejected?Attempt status and lease token comparison
How many times has this run?executions.attempt and the attempt table
Was the task duplicated?Idempotency key and attempt history

The history is also a reminder that retries are not free. Each attempt is a real side effect unless the command is idempotent. The scheduler can guarantee that only one result is accepted. It cannot guarantee that a shell command did not run twice.

What leases cannot promise

Leases solve a coordination problem, not an execution problem.

If a worker runs send_invoice.sh, expires mid-run, and a second worker runs the same command, the invoice may be sent twice. The database will accept only one result, but the external side effect already happened. No lease design can undo that without cooperation from the external system.

The scheduler's documentation says this plainly: delivery is at-least-once, result application is lease-guarded, and shell side effects are the operator's responsibility. Tasks that need stronger guarantees should use an idempotency key with the downstream service.

Leases also cannot make a slow worker fast. They can only make ownership explicit enough that a slow worker cannot silently corrupt state when it returns.

Rules that survived the drills

The design settled on a small set of rules:

  • A claim is a conditional database write, not a message on a queue.
  • A lease has a deadline and a token. Both are required.
  • Heartbeats extend the lease only if the token still matches.
  • Results are accepted only if the token and attempt still match.
  • Reaping is reconciliation. It can be retried.
  • Attempt history is part of the product, not debug logging.
  • At-least-once is stated in the interface, not hidden in a wiki.

The most valuable outcome of the failure drills was not a new component. It was a clearer definition of ownership. A worker is not trusted because it is running. It is trusted because it holds a lease that has not expired, and because the database still agrees.