Saltar al contenido principal

Hold the Lease, or Stop Serving: Ownership on a Timer

· 11 min de lectura
Clustron Team
Ingeniería de Sistemas Distribuidos

A distributed store has to know, at every moment, which node owns a given partition — which one accepts the writes, orders them, and is the source of truth the replicas follow. The tempting way to record that is a lock: the owner grabs an exclusive lock on the partition and holds it. It works right up until the owner crashes while holding the lock, at which point the lock is stuck, the partition has no live owner and no way to get one, and the only thing that clears it is a human noticing. A store whose availability depends on someone noticing is not highly available.

The fix is to stop granting ownership once and start renting it. A lease is a lock with an expiry date: the owner is granted the partition for a fixed term, and that grant lapses automatically unless the owner keeps renewing it before it runs out. A healthy owner renews comfortably ahead of each deadline and keeps serving. An owner that crashes, freezes in a long garbage-collection pause, or is severed from the network simply stops renewing — and its lease expires on schedule, all by itself. The partition becomes available for a new owner to claim, with no operator in the loop and no stuck state to clear. Liveness is restored by the clock, which is the one actor in a distributed system that never stops working.

That is the whole appeal: a lease converts "the owner is gone and nothing will ever notice" into "the owner is gone and the problem resolves itself in at most one lease term." But it does not come free. It replaces a correctness problem that needed a human with a timing problem that needs careful arithmetic, and getting that arithmetic wrong is how two nodes come to believe, simultaneously, that each of them owns the same partition.

The renewal contract​

A lease is an agreement between two parties: the grantor (a coordination service, or the cluster's elected leader) and the holder (the node that owns the partition). The contract has two numbers. The term T is how long a grant is good for. The renewal interval is how often the holder refreshes it, and it must be comfortably shorter than T — a common choice is T/3, so that a holder can miss two consecutive renewals (to a dropped packet, a brief hiccup) and still refresh before the lease lapses.

The grantor's promise is the strict half: once it has granted a lease with term T, it will not grant that partition to anyone else until T has elapsed with no renewal. The holder's obligation is the mirror image: it may serve as owner only while it believes its lease is still live, and the instant it can no longer be sure, it must stop — shed its ownership, reject the writes it can no longer safely accept, and step down. A failure to renew is therefore not an error to retry forever; past a point it is a signal to give up the role. This is the same "dead or just slow?" question a failure detector asks, answered by a deadline instead of a guess.

lease term T (epoch N)time →granted · epoch Nrenew every T/3renewal missedcrash / GC pause / partitionowner stopsexpiry − guardnominal expiry (t + T)grantor reassignsexpiry + guardnew owner · epoch N+1guard gap: no one serves — absorbs bounded clock skewlate write stamped epoch N → fenced (stale)

The lease is shorter than it looks​

Here is where the arithmetic bites. The holder and the grantor are both reasoning about the same deadline — "expires at t + T" — but each reads it on its own clock, and physical clocks drift. If the holder's clock runs slow, it believes its lease is still live for a few hundred milliseconds after the grantor has already decided it expired and handed the partition to someone else. For that window, two nodes both think they own the partition and both accept writes. That is split ownership, and it is exactly the corruption a lease was supposed to prevent.

The defense is a guard margin on both ends. The holder does not serve right up to its computed expiry; it stops a guard interval early, at expiry − guard. The grantor does not reassign the instant its own clock hits expiry; it waits a guard interval late, until expiry + guard. Between the two lies a dead band in which the old owner has already stepped down and the new one has not yet started — nobody serves. As long as the real clock skew between the two parties stays smaller than the guard, their serving intervals cannot overlap, and split ownership is impossible. The price is that the partition is briefly unavailable during handoff, which is the right trade: a short gap in availability beats a silent divergence in the data. The practical rule falls out of this directly — the usable lease is the term minus the guard, so a lease is always a little shorter than its nominal term, and a store that forgets the guard has a lease that is secretly too long.

The fence behind the lease​

A guard margin is only safe while skew stays bounded, and the real world violates that occasionally: a virtual machine is paused and resumed minutes later, an NTP correction steps the clock, a process freezes in a stop-the-world GC far longer than anyone budgeted. In those moments a holder can wake up still believing its lease is live when it expired long ago — a zombie owner — and a lease alone cannot stop it from issuing a write.

So correctness does not rest on the lease. It rests on a fencing token: a number that increases every time the lease changes hands, stamped onto the lease and carried on every write the owner sends downstream. The replicas and the storage layer remember the highest token they have ever accepted and reject anything older. When the lease moves to a new owner, its token jumps; the new owner's writes carry the higher number, and the moment they land, every later write from the zombie — still stamped with the old, now-stale token — is refused. The lease delivers liveness, recovering ownership automatically when a node vanishes; the fence delivers safety, guaranteeing that a node which lost ownership without realizing it cannot corrupt anything. Neither is sufficient alone, and the system needs both.

// The owner serves only while its lease is live — and stops early, by the guard.
bool CanServe(Lease lease, Clock clock) =>
clock.Now() + guard < lease.ExpiresAt; // false => shed ownership, step down

// Every mutation carries the lease's fencing token; the replica rejects a stale one.
void Apply(Write w, Replica r) {
if (w.Token < r.HighestTokenSeen) // a zombie from a lapsed lease
throw new FencedOut(w.Token); // refuse — safety never trusts the clock
r.HighestTokenSeen = w.Token;
r.Commit(w);
}

How it composes​

In the store, every partition's owner holds a lease, and the lease's fencing token is the ownership epoch. A healthy owner renews and serves; when it fails to renew, the lease lapses and the cluster promotes a new owner at the next epoch — the mechanics of anatomy of a failover. Because the epoch advances on every handoff, the old owner's in-flight writes are fenced the instant the new one commits, which is why a planned partition cutover can hand ownership across cleanly and why clients retrying under ownership churn never land a write on a stale owner. It is the same shift that separates a distributed lock from a lease: a lock asks "who holds it?", while a lease asks "who holds it right now, and for how much longer?" — and in a system where nodes crash, pause, and partition without warning, the second question is the only one with a safe answer.

Wrap-up​

A lock is ownership with no expiry, and in a distributed system that makes it a liveness hazard: a crashed holder leaves it stuck forever. A lease is ownership on a timer — rented for a term, renewed ahead of each deadline, and reclaimed automatically the moment an owner stops renewing, so a dead node heals itself in at most one lease term with no human in the loop. The catch is that the holder and the grantor judge the same deadline on different clocks, so the lease is served with a guard margin that makes the owner step down early and the grantor reassign late, and a bounded skew can never produce an overlap. Behind the guard sits a fencing token that makes correctness independent of the clock entirely, so that even an unbounded pause cannot let a zombie owner corrupt the data. Liveness from the timer, safety from the fence — hold the lease, or stop serving.