Quick revision
The short answer
Two separate faults produced the duplicates. After NTP stepped one node's clock back by 2 seconds, its generator reissued timestamps it had already used, with the same worker id and a restarting sequence, so it repeated ids; and hashing pod names into 10 bits gave two pods the same worker id, which with 24 pods happens about 24 percent of the time. We make generation safe by guarding the clock (refuse or wait when time goes backwards, fail fast on larger gaps, and configure time sync to slew on running hosts), assigning worker ids from leases in a database or coordination store, keeping the unique constraint as the last defence, and alerting on clock-back events.
What to remember
In this answer, we saw that the duplicates had two causes: a backward NTP step made one generator reuse timestamps with the same worker id and a restarting sequence, and hashing 24 pod names into 10 bits gave a collision with about 24 percent probability. We made generation safe by guarding the clock in the generator, configuring time sync to slew on running hosts, assigning worker ids from leases measured by the database clock, keeping the unique constraint, and alerting on clock-back and lease failures.
Your turn
Answer it aloud as you would in the interview, or type your answer below. Then show one part at a time and compare.
Two separate faults produced the duplicates. After NTP stepped one node's clock back by 2 seconds, its generator reissued timestamps it had already used, with the same worker id and a restarting sequence, so it repeated ids; and hashing pod names into 10 bits gave two pods the same worker id, which with 24 pods happens about 24 percent of the time. We make generation safe by guarding the clock (refuse or wait when time goes backwards, fail fast on larger gaps, and configure time sync to slew on running hosts), assigning worker ids from leases in a database or coordination store, keeping the unique constraint as the last defence, and alerting on clock-back events.
Why this question is asked
Snowflake-style ids promise uniqueness without coordination, but that promise rests on two assumptions: the clock never goes backwards, and no two generators share a worker id. This incident broke both on the same night. Interviewers like it because it is a real failure mode that tests our understanding of time on servers and of probability, not just of bit layouts. In this answer, we will recap why the generator is unique in theory, explain each cause with numbers, fix the clock handling, replace hashed worker ids with leases, and add the last lines of defence.
1A Quick Recap of What Keeps Ids Unique
A Snowflake-style id combines a millisecond timestamp, a worker id and a per-millisecond sequence. Two ids can only be equal if all three parts are equal. Different workers differ in the worker id. The same worker in different milliseconds differs in the timestamp. The same worker in the same millisecond differs in the sequence.
Notice what this silently assumes. Each worker id must belong to exactly one running generator, and each generator must never live through the same millisecond twice. If either fails, the guarantee is gone, and the database's unique constraint is all that stands between us and corrupted data.
2Cause 1: The Clock Stepped Back
NTP keeps server clocks in sync. When a clock is far off, NTP can correct it in two ways: slewing, which speeds up or slows down the clock slightly until it is right, or stepping, which jumps it to the right time at once. A step backwards by 2 seconds means the node lives through the same 2,000 milliseconds again.
| Wall clock | Timestamp used | Sequence | Result |
|---|---|---|---|
| 02:14:07.120 | ...07.120 | 0, 1, 2 | ids issued |
| NTP steps clock back 2 s | |||
| 02:14:05.120 | ...05.120 | 0 | new, but older than issued ids |
| 02:14:07.120 again | ...07.120 | 0, 1, 2 | same ids as before: duplicate key |
A generator that only compares the current time with the previous call and resets the sequence whenever the time differs will happily issue ids again for timestamps it already used. With the same worker id and a sequence that restarts at 0, those ids are identical to ones issued 2 seconds earlier, so inserts fail with duplicate key errors until the clock passes the old high point. Two seconds of replay explains part of the 40 seconds of errors; the colliding worker ids explain the rest.
3Guarding the Clock and Configuring Time Sync
The generator must remember the last timestamp it used and never go below it. If the current time is lower by a few milliseconds, it waits until the clock catches up; if it is lower by more, it refuses to issue ids and fails fast, so the pod is taken out of service instead of producing duplicates or stalling every request for seconds.
On the operations side, time sync should slew rather than step on running hosts. With chrony, makestep 1.0 3 allows steps only during the first three updates after start, when nothing is generating ids yet, and slews afterwards. With ntpd, the slew-only option has the same effect (check your distribution). Leap seconds should be smeared rather than inserted as a repeated second. These settings make backward steps rare; the generator guard makes them harmless.
4Cause 2: Hashing Pod Names Into 10 Bits
Deriving the worker id from a hash of the pod name looks clever, because it needs no coordination. But it is the birthday problem: with 24 pods choosing among 1,024 values, the chance that at least two pick the same one is 1 minus the product of (1 - i/1024) for i from 0 to 23, which is about 24 percent. So roughly one deployment in four has a collision somewhere. Restarts make it worse, because a restarted pod usually gets a new name and a new hash, giving fresh chances to collide.
Two pods with the same worker id generate identical ids whenever they happen to issue ids in the same millisecond with the same sequence, which at low traffic is mostly sequence 0. Under load that happens constantly. It probably stayed hidden until the clock step increased the overlap, but it was always a latent bug.
5Assigning Worker Ids From Leases
The robust fix is to hand out worker ids from a central place that guarantees exclusivity, with leases so that crashed pods eventually give theirs back. A database table with one row per worker id works well. At startup, a pod claims a free or expired row; while running, it renews its lease every few seconds; at shutdown it releases it.
7 messages in order. Play them, or step through them one at a time.
Notice that expiry is measured with the database clock, now(), so pods with skewed clocks cannot disagree about who owns a lease. A worker id is never reused before its lease has expired, and a pod stops issuing ids before its own lease could expire, so two live owners cannot overlap. Kubernetes StatefulSet ordinals are another way to get stable, unique numbers, as long as the ordinal is unique across all generators that share the id space.
6Keeping the Last Line of Defence
Even with both fixes, the unique constraint on the id column stays. It turned a silent data corruption into 40 seconds of visible errors, which is exactly what we want from a last line of defence. We also alert on any clock-back event from the generator and on lease renewal failures, and we test the guard by faking a backward clock in a unit test with an injected clock, so the protection does not decay unnoticed.
7Follow-up questions
What do you do with a pod that cannot renew its worker id lease?
It must stop generating ids before the lease could expire, for example after 20 of its 30 seconds, because after expiry another pod may claim the same worker id. Requests that need ids then fail fast, and the pod marks itself not ready so traffic moves elsewhere. It keeps trying to renew or claim a new lease and resumes only once it holds one. Losing a little availability on one pod is far better than issuing ids that may collide.
Why not just retry the insert with a new id on a duplicate key error?
For ids generated in our own service a retry would hide the symptom, but duplicates may also land in other systems that use the id as a key, such as Kafka messages or caches, where no constraint catches them. Fixing the generator is the only reliable answer.
Could the pods use random worker ids instead?
Random choice is the same birthday problem as hashing. Only a mechanism that knows which ids are taken, such as a lease table or a coordination service, can guarantee uniqueness.
8Conclusion
In this answer, we saw that the duplicates had two causes: a backward NTP step made one generator reuse timestamps with the same worker id and a restarting sequence, and hashing 24 pod names into 10 bits gave a collision with about 24 percent probability. We made generation safe by guarding the clock in the generator, configuring time sync to slew on running hosts, assigning worker ids from leases measured by the database clock, keeping the unique constraint, and alerting on clock-back and lease failures.
Open with
There are two causes. First, the NTP correction stepped the clock back by 2 seconds, so that node relived 2,000 milliseconds. Its generator reset the sequence whenever the time changed and did not compare against the last timestamp it had used, so it issued the same timestamp, worker id and sequence again, which are exactly the same ids.
Then, if they want more
Second, deriving the worker id by hashing the pod name into 10 bits is a birthday problem: with 24 pods in 1,024 slots, the chance of at least one collision is about 24 percent, and every restart rolls the dice again. Two pods with the same worker id produce identical ids whenever they issue in the same millisecond.
To fix it, the generator keeps its last timestamp and never goes below it: it waits if the clock is a few milliseconds behind and fails fast if it is more, and we count those events and alert. Time sync should slew rather than step on running hosts, for example chrony stepping only at boot.
Worker ids come from leases in a database table, claimed with SKIP LOCKED, renewed every 10 seconds, expiring after 30, and never reused before expiry; a pod that cannot renew stops issuing ids. And the unique constraint stays as the last line of defence.
About this answer
- Last updated
- 13 November 2026
- Checked
- Its structure and format are checked automatically. Use it as a carefully prepared study note: where a detail matters, confirm it against the official documentation for the version you use. What the checks cover
- Where the question comes from
- Written by Scrivio for practice. It is not taken from an interview.
Report an issue with this answer
Copy the line below and send it to the author with what you found. It carries the address of this page.
Issue with "Explain both causes and how you would make id generation safe.": https://scrivio-interview-library.netlify.app/architecture/storage-and-pipelines/f4d9c161e2/