Lease (Leased Lock)

A time-bounded exclusive ownership right over a resource or role. A holder retains the lease as long as it periodically renews it before expiration; if renewals stop, the ownership expires automatically.

General Meaning

In distributed systems where a critical role or task must be executed by exactly one worker at any given time, traditional indefinite locks introduce single points of failure. If a node crashes or loses power while holding an unexpired mutual exclusion lock, the entire cluster can hang indefinitely waiting for a manual release.

A lease solves this dilemma by attaching a time-to-live (TTL) expiration to the lock. The active holder must actively renew the lease at regular intervals before it elapses. If the holder crashes or partitions, the lease naturally expires, permitting standby nodes to safely acquire the lock and resume processing without deadlock.

Why It Matters

  • Preventing abandoned lock deadlocks: When a node suffers a hardware crash, power outage, or OS freeze, its lock naturally lapses, enabling surviving nodes to take over.
  • Enforcing single-leader execution: During valid lease terms, only the recognized leaseholder may run coordinated workflows, eliminating duplicate patrol runs or race conditions.
  • Simplifying central coordination: Workers do not need complex peer-to-peer gossip protocols; they simply inspect or renew the lease record in a centralized datastore.

How the Site Factory Uses It

Across the Site Factory's home cluster (Mac mini, Studio 1, and Studio 2), identical daemon services (patrol, macgakari, unkou, and site patrol) are installed on all three machines. Active execution is governed by Cloudflare D1 service leases, such as /v2/services/patrol. Each launchd iteration triggers service_lease.py to acquire the designated lease in D1; only the winning node proceeds, while standby nodes perform no work. Leases have a 40-minute TTL, with long-running tasks renewing every 5 minutes. If renewal fails, a stop-file flag (HONBU_SERVICE_LEASE_STOP) terminates the loop at the next clean boundary, and nodes abort after three consecutive failures or upon receiving HTTP 409.

The Site Factory enforces strict defensive verification on lease renewals rather than trusting raw HTTP 200 responses. The client validates ok:true, node identity matching, integer generations, proper ISO timestamps, and monotonic expiration advancement before extending local runtimes. The safe local deadline is calculated by adding the server-granted TTL to the client monotonic clock recorded when the request was sent, subtracting a safety margin to prevent artificial extension during network delays. Execution jobs in D1 (execution_jobs) also feature lease_until and lease_token; expired A2A jobs transition cleanly to uncertain rather than being marked as failures.

Caveats & Operational Rules

  • A lease alone cannot prevent stale writes: If a holder pauses due to garbage collection or network lag, its lease may expire while it still believes it is active; the Site Factory couples every lease with an incrementing generation number so the storage layer rejects stale writes.
  • Do not rely on synchronized wall-clock times: Machine wall clocks frequently drift across hardware; deadline evaluations must be computed using server-granted duration added to local monotonic timers.
  • A lease is not proof of forward progress: Holding a valid lease merely guarantees exclusive access, not that tasks are completing; the Site Factory verifies definitive task completion through signed receipts and deliverables.

Related Terms

References

  • Book: Martin Kleppmann, *Designing Data-Intensive Applications*, Chapter 8 (The Trouble with Distributed Systems: Leases and Fencing)

Verified: 2026-11-08