How it works
Assign each generator a unique data-center and machine ID from a registry: a strongly consistent store such as etcd or ZooKeeper that records which process holds which ID. The claim is a lease, which lasts a fixed time and must be renewed. On startup, obtain the lease before issuing IDs. A duplicate configuration lets two workers generate the same timestamp and sequence combination.
Follow a partition with example timings. Worker A holds ID 17 on a 30-second lease and last renewed at t = 0, when the network cuts it off. At t = 30 the registry considers the lease expired and could hand 17 to Worker B. If A keeps issuing, both can produce identical IDs. Fencing means stopping an expired holder from acting. A classic fencing token, a rising number that a storage server uses to reject old holders, does not fit here because generating an ID touches no such server. So the worker fences itself: it checks its lease timer before each ID and stops when the lease runs out. The registry waits an extra margin, perhaps 10 seconds, before reassigning, to cover clock drift and process pauses.
Do not reuse a worker ID immediately after a crash unless the new process can prove its clock and sequence have moved past the old process’s last issued value, or it has waited out a safe interval. Leave headroom in the 10-bit worker namespace: a rolling deploy that briefly runs 1,200 pods cannot fit in 1,024 IDs. A small fixed fleet may manage with static configuration instead.
An autoscaler starts two pods with the same machine ID. Why can their IDs collide?
If both issue in the same millisecond and use the same sequence, their encoded fields are identical. Allocate distinct live identities and fence stale holders.
Use this as a guide for self review; written answers are not graded.Keep in mind
- Worker-ID uniqueness is a live operational invariant.
- Lease expiry and restart need fencing or a safe reuse delay.