advanced25 min studyLesson 358 in Engineering

Controller leadership: a lease does not fence old work

Trace cooperative leadership changes and require downstream fencing or idempotency for effects that must exclude a stale actor.

On this page

What you’ll understand

  • Explain lease renewal and takeover as a cooperative coordination mechanism.
  • Construct an overlap involving a paused old leader.
  • Distinguish a lease record, a fencing token, and an idempotency key.

Several controller replicas may run for availability while one normally performs a particular active role. A Kubernetes Lease can coordinate that role, but it does not physically stop a paused or partitioned old process from acting on an external system. The client-go v0.34.0 leader-election documentation explicitly does not guarantee fencing. Treat leadership as one layer of the design, then decide how the resource receiving side effects rejects stale actors or absorbs repeated operations.

Renewal is an observation, not remote process control

An acting leader renews a shared record, while other candidates observe whether progress continues under the election algorithm’s timing rules. A candidate may take over when the old leader is judged unable to renew within the configured bounds. Network delay, process pauses, API availability, and clock-rate behavior affect that judgment. A Lease’s timestamps should not be naively compared using arbitrary machines’ unsynchronized wall clocks; use the library’s specified local observation and timing model rather than inventing a second election protocol around the same object.

After losing leadership, controller code should cancel and stop guarded work promptly, but cancellation is cooperative. An external call already in flight may complete later, and a stopped process can resume after another candidate starts. Releasing a lease before guarded work is actually stopped can also permit overlap. Do not infer that only one actor can affect a database merely because one current Lease holder is visible in the API. The downstream service has not necessarily checked that Lease at all.

Ttakeover≈Tdetection+Tacquisition+TstartupT_{\rm takeover}\approx T_{\rm detection}+T_{\rm acquisition}+T_{\rm startup}
The terms are elapsed seconds in a declared failover scenario. This budget decomposition is not a Kubernetes timing guarantee: retries, clock-rate assumptions, cache readiness, and outstanding external work can change observed failover and overlap.↔ Scroll wide equations horizontally.

Fencing belongs at the side-effect boundary

A fencing design gives each authority epoch a monotonically increasing token and makes the downstream resource atomically reject operations from older epochs after accepting a newer one. Merely sending a token is insufficient if the receiver does not persist and enforce the check together with the effect. A Kubernetes object resourceVersion is not automatically such a token for another system, and an arbitrary increment in process memory is not a safe authority source. The token allocation and resource enforcement need their own documented correctness argument.

An idempotency key solves a different problem: repeated delivery of one logical operation should not produce duplicate effects under the receiving API’s contract. It does not necessarily stop a stale leader from issuing a different unwanted operation. Many controllers combine repeatable reconciliation, stable identities, cancellation, and leader election, adding fencing for external effects that require it. State the required safety property first. Strong exclusion at an external target is more demanding than reducing duplicate reconciliations during healthy operation.

accept⁡(e)  ⟺  e≥emax⁡,emax⁡←max⁡(emax⁡,e)\operatorname{accept}(e)\iff e\geq e_{\max},\qquad e_{\max}\leftarrow\max(e_{\max},e)
This sketches an atomic downstream fencing rule for nondecreasing authority epochs. Equal-epoch retries still need appropriate operation semantics. The epoch source and the comparison/update/effect atomicity are assumed; this is not supplied automatically by the Kubernetes Lease API.↔ Scroll wide equations horizontally.

Design failover tests around stale actors

Pause the old leader after it passes a local leadership check but before its external request completes. Let a new replica acquire the role, then resume the old request. Observe what the downstream system actually accepts. Also test cancellation during long cleanup, lease release while goroutines remain active, API request timeouts, and delayed responses. Measure time to useful reconciliation separately from time to election acquisition, because a new leader may still need cache synchronization or dependency recovery before it can make progress.

The reliability lab counts reachable participants only. Reset, set n=3 and k=1, predict two up and a majority threshold of two, then Step. That count does not run a Lease election, provide a fencing token, or stop a stale process. It helps separate the availability question from the side-effect safety question. A reachable majority can support some coordination protocols, but this diagram proves none of their behavior. Use the explicit epoch trace for the actual stale-writer argument.

Reject the resumed old leader downstream

Assume a separate correct authority mechanism grants old leader A fencing epoch 7 and new leader B epoch 8. The external resource atomically enforces the stated maximum-epoch rule and has already accepted B’s epoch 8.

  1. The resource stores emax=8 after B’s accepted operation. A later resumes and submits an operation tagged 7. Because 7<8, the downstream resource rejects it even if A still believes it leads.

  2. A retry from B with epoch 8 passes the authority comparison, but repeat delivery of the same logical action still needs the resource’s idempotency or transaction semantics. Equality is not permission to duplicate a payment or append blindly.

  3. If A’s operation arrived before the resource ever accepted epoch 8, this particular rule alone would not prove it must be rejected. The safety claim concerns rejection of older epochs after the newer epoch is installed, not omniscient knowledge of an external election.

The result

Downstream atomic enforcement provides the stated fencing boundary. The Lease record alone would not make the old external call disappear.

Explore the model

Interactive lab

Interactive lab · reliability

Availability is not stale-leader exclusion

Reset, set n=3 and k=1, predict two up and majority two, then Step. This availability diagram does not elect a controller, enforce leases, generate epochs, or fence external writes.

Availability is not stale-leader exclusionA replica count and a majority threshold, without a consensus protocol. Playback highlights fixed replica states. Having a majority alone does not establish linearizability, durability or failure independence. Current readings: Available replicas: 4 of 5; Majority needed: 3; Majority available?: Yes. All parameters are adjustable with the labeled controls below.Majority available · need 3 of 5Playback highlights fixed statesR1 · upR2 · upR3 · upR4 · upR5 · downCyan mark: majority threshold
AvailableUnavailableMajority threshold
Available replicas
4 of 5
Majority needed
3
Majority available?
Yes
t = 0 s

A replica count and a majority threshold, without a consensus protocol. Playback highlights fixed replica states. Having a majority alone does not establish linearizability, durability or failure independence. Parameter changes pause playback and recompute the model at the current elapsed time. Reset restores both parameters and returns time to zero. Playback starts paused. Adjust either parameter, then choose Play or Step.

Look forA participant count cannot substitute for a side-effect-boundary safety mechanism.

Check your understanding

Which statement correctly describes the safety boundary of client-go leader election?

Try it yourself

A downstream target has atomically recorded fencing epoch 12. Requests tagged 11,12,13 arrive in that order. Which pass the stated comparison rule, and what is the final maximum?

Take a moment to make a prediction or work it out on paper. Open a hint when you need one.

Give me a hint

Apply the epoch check and update in order, while keeping duplicate-operation semantics separate.

Compare with the solution

Epoch 11 is rejected. Epoch 12 passes the authority check and leaves the maximum at 12. Epoch 13 passes and advances it to 13. Equal-epoch operation retries still require suitable idempotency; the comparison alone does not guarantee a repeated action is harmless.

Take it with you

  • A lease coordinates cooperative leadership rather than stopping remote processes.
  • Fencing requires an authority token enforced atomically by the effect receiver.
  • Idempotency, cancellation, election, and fencing protect different boundaries.

Read further

Original sources and deeper explanations to follow at your own pace.

Optional progress, saved in this browser when available. Revisit any lesson at any time.

Lesson not marked complete.