Leases
Some background work should run on one node at a time: failover, garbage collection, and the TTL and token sweeps. Running them on every node at once would be wasteful, not incorrect. PicoMQ elects that one node with a lease in the SQL database, the same database that already holds the metadata log, so the election needs no extra infrastructure.
The lease row
The lease is a single row holding the current holder, an epoch, and an expiry time. Acquiring it is a conditional update: take the row if it is free or its TTL has lapsed, bumping the epoch. Renewing is a conditional update at the holder's own epoch. The database's transaction guarantees make both atomic, which is all the election needs.
The TTL is 10 seconds and each node tries to acquire or renew every quarter of that. The epoch is a fencing token. A holder that stalls, loses connectivity, or gets paused past the TTL finds its renewal rejected because a successor bumped the epoch, and it demotes itself instead of acting on stale leadership.
What leadership gates
The leadership state feeds a watch channel, and the gated loops follow it: they start when the node becomes the holder and stop when it is not. Each ticks once a second. The list of loops is in Overview.
Losing the lease is not an event worth reacting to beyond stopping the loops. The work is queued in the replicated state, not in the holder's memory, so the next holder picks up exactly where the last one stopped.
Handover
A node that shuts down cleanly releases the lease, so a successor takes over on its next attempt rather than waiting out the TTL. A node that crashes leaves the row to expire, and the gap until takeover is at most the TTL plus one check interval. A holder that cannot reach the database keeps its leadership only for as long as the lease cannot have expired underneath it, then self-demotes.
Why correctness never depends on it
The lease is an optimization, not a safety mechanism. If a handover briefly overlaps two holders:
- Cleanup and sweeps go through the metadata log. One proposal applies and the duplicate is answered as redundant.
- Failover fences only a node that is exactly as the proposer saw it, and taking over a WAL fences its writer, see Failover.
Nothing diverges, some work is done twice.