Distributed coordination
On this page 8 topics
Overview
Topics
- Leases
- leader election concepts
- fencing tokens
Learn first
2 h of practice (estimate)
Learning outcomes
- Explain leases and leader election and when a system needs a single active worker.
- Explain why a lock held via an expired lease is unsafe and how fencing tokens prevent damage.
- Implement a single-runner scheduled job across instances (database-based lock, e.g., ShedLock).
Lessons
Lab and exercises
Lab
MID-032-LAB1 · Single runner
Outcomes: 1–3 · Time: ~2 h Make the nightly reconciliation job in orders run once across three replicas using a database lock (ShedLock or an equivalent lease table). Add a fencing token: each lock grant increments a number that the job writes with its results, and the update rejects lower tokens.
Acceptance: a test that pauses the lock holder past its lease, lets another instance run, then resumes the first does not double-apply any change.
Exercises (assessment: quiz)
| ID | Outcome | Task |
|---|---|---|
| MID-032-EX01 | 1 | List three Shop jobs and say which truly need a single active worker and which can use another design. |
| MID-032-EX02 | 1 | Explain leader election and what must happen when the leader dies. |
| MID-032-EX03 | 2 | Draw the timeline of an unsafe lease: holder pause, expiry, takeover, double write. |
| MID-032-EX04 | 2 | Write the SQL update that applies a fencing token. |
| MID-032-EX05 | 3 | Configure lockAtMostFor and lockAtLeastFor for a job that normally takes 3 minutes and at worst 20. |
| MID-032-EX06 | 2 | Review a design that "uses Redis SET NX for exactly-once payouts" and recommend changes. |
Show solutions
- EX01 Reconciliation: one runner (lock + fencing). Outbox relay:
SKIP LOCKED. Email digest: lock or unique "sent for date" record. Cleanup of old rows: idempotent, can run anywhere. - EX02 Instances compete for a lease; the winner acts and renews it; if it dies the lease expires and another wins. The old leader must stop on failed renewal.
- EX03 A acquires; A stalls; lease expires; B acquires and writes; A resumes and writes: both applied.
- EX04
UPDATE state SET last_token = :t, ... WHERE id = :id AND last_token <= :tand treat 0 rows as stale. - EX05
lockAtMostForof about 30 minutes (above worst case, still releases after a crash);lockAtLeastForof 1 to 5 minutes to prevent immediate re-run through clock skew. - EX06 Redis locks cannot guarantee exclusion under pauses or failover. Make payouts idempotent (unique payout key), add fencing in the payout table, and use the lock only to reduce duplicate work.
Outcome 3 is mainly assessed by the lab and quiz Q09 and Q10.
Quiz
Outcomes: O1 leases and leaders · O2 lock safety and fencing · O3 single-runner jobs.
A self-check for you: nothing is graded, saved or sent anywhere. Choose an answer, then check it. You can try again as often as you like.
Practical part
Six exercises MID-032-EX01 to EX06 plus MID-032-LAB1. Pass: at least five exercises, 8 of 10 quiz answers, and a paused-instance test with no double-applied change.
Common mistakes
Trusting a Redis lock for correctness; no fencing.
Assessment
Quiz.
Career relevance
Consensus concepts (SRB-003).