Leader Election
Let a group of identical instances pick one leader to do work that must happen exactly once, and automatically pick a new one if it fails.
Description
When you scale a service to several identical instances, some jobs must still run on only one of them: the nightly billing run, a scheduled cleanup, coordinating partitions. Running them on every instance would bill customers three times.
With leader election, instances compete for a lease, a lock with an expiry, in a shared coordination store. The winner is the leader and renews the lease regularly; the others stay on standby. If the leader crashes, its lease expires and another instance takes over within seconds.
System design
Leader Election system design diagram
- Instance A (service): leader.
- Instance B (service): standby.
- Instance C (service): standby.
- Lease store (database): etcd / DB lock, TTL 15 s.
- Nightly billing (function).
- Step 1: Instance A → Lease store: acquire / renew.
- Step 2: Instance B → Lease store: try: taken (failure path).
- Step 3: Instance C → Lease store: try: taken (failure path).
- Step 4: Instance A → Nightly billing: only the leader runs it.
- Instance A holds the lease and renews it every few seconds, so it is the leader.
- Instance B tries to take the lease, finds it held and stays on standby.
- So does instance C. If A stops renewing, the lease expires after 15 s and one of them wins it.
- Only the leader runs the nightly billing job, so customers are billed exactly once.
- AWS
- DynamoDB conditional writes as a lease (DynamoDB Lock Client); EKS leases; Aurora advisory locks.
- Azure
- Blob Storage leases; AKS leases; Azure SQL application locks.
- Google Cloud
- GKE leases; Firestore/Spanner transactions as a lease; Cloud Storage object preconditions.
When to use it
Use it when
- Several instances run for availability, but a task must be performed by only one at a time.
- You need a single coordinator, such as assigning work partitions or running a scheduler.
- Failover must be automatic, without someone picking a new primary by hand.
Avoid it when
- The work can be split into independent jobs. Use a queue with competing consumers; no leader needed.
- A managed scheduler can run the job once (cron in Kubernetes, EventBridge Scheduler, Cloud Scheduler).
- Your data store already handles primaries and failover (managed databases do).
Pitfalls
- Split brain: a paused old leader wakes up and keeps working after a new one is elected. Use fencing tokens so stale leaders' writes are rejected.
- Leases too long (slow failover) or too short (leadership flapping on small hiccups).
- Writing your own consensus. Use a proven store: etcd, ZooKeeper, a database row lock or a cloud lease API.
- The leader doing so much work that it becomes the bottleneck.
Alternatives
Competing Consumers
Choose it when the work is a stream of independent jobs; let any worker take each one.
Managed scheduler
Choose it when the task is simply "run this once every night".
Partitioning
Choose it when you can give each instance its own slice of the work instead of one leader for everything.