The interview problem
Design identifier allocation for an event platform operating in eight regions. Peak global creation rate is one million events per second. Clients want compact IDs that sort roughly by creation time, and each region should survive a temporary inter-region partition. Explain uniqueness, worker restarts, clock rollback and the difference between sorting IDs and ordering committed events.
Original system-design practice, not a reported company interview. Rates and bit allocations are hypothetical. Official technical reference retrieved September 27, 2026.
1. Separate four different promises
Uniqueness means two distinct allocations never return the same identifier within the defined namespace. Local monotonicity means one allocator returns increasing values. Approximate temporal order means timestamps usually place nearby events near each other. A globally linearizable sequence is stronger: it orders allocations consistently with real-time precedence. None of these promises alone makes IDs gapless or orders subsequent database commits.
For the base design, require uniqueness under the stated allocation protocol, approximate time sorting and regional availability while previously reserved allocation space remains. Allow gaps, arbitrary order among concurrent events and backpressure if safe allocation space is exhausted. Do not promise indefinite isolated operation with finite preallocated space. Use separate event-time and commit metadata for consumers needing those meanings.
2. Calculate a compact layout
An illustrative nonnegative 64-bit representation uses one zero sign bit, 41 timestamp bits measured in milliseconds since a fixed custom epoch, 10 worker bits and 12 sequence bits. Reserve three worker bits for eight regions and seven for 128 worker slots in each region. Treat the epoch and layout as permanent namespace metadata; changing them without a versioned namespace can create collisions.
| Field | Capacity | Consequence |
|---|---|---|
| 41-bit timestamp | 2,199,023,255,552 milliseconds | About 69.68 years using 365.25 days/year; reject overflow |
| 10-bit worker | 1,024 slots globally | 8 regions × 128 slots; identity allocation is part of correctness |
| 12-bit sequence | 4,096 values per logical millisecond | Never wrap at the same timestamp and worker |
The nominal field capacity per worker is 4.096 million IDs/second if it can use every sequence value in each millisecond. That is an encoding ceiling, not measured hardware throughput. At one million global events/second spread evenly across eight regions, each region sees 125,000/second. Average capacity says little about a hot worker receiving more than 4,096 requests in one millisecond; it must wait or use a later allowed logical millisecond.
Compose the value as (timestamp << 22) | (worker << 12) | sequence, after checking every field range. Serialize it as a decimal string or a true 64-bit integer format. JavaScript numbers cannot represent all integers above 2^53 exactly; silently rounding an ID can destroy uniqueness at the API boundary.
3. Make allocation space disjoint
global configuration → unique region namespaces regional quorum registry → durable, non-overlapping timestamp ranges per worker worker process → one reserved range + atomic timestamp/sequence state → ID storage → unique-key constraint + allocation diagnostics
A lease that says “worker 17 belongs to process B now” is insufficient by itself. Paused process A may resume with its old state, and an ID already returned to a client cannot be recalled by fencing a later database write. The ID contains no lease epoch, so A and B using the same timestamp and sequence would collide.
One concrete solution is to reserve disjoint timestamp ranges for each worker slot in a durable quorum registry. For example, A receives worker 17 with timestamp interval [1,000,000, 1,001,000); B later receives [1,001,000, 1,002,000). A cannot emit outside its interval. B never uses A's interval, even after A's lease expires. The registry commits the upper reservation bound before returning a grant. Lost responses can waste ranges, but cannot make the registry reuse them. A retry may recover the same grant only for the same live incarnation without resetting its cursor; otherwise allocate fresh space. Ignore delayed responses for superseded grant requests.
A new process incarnation always obtains a new range. It must not restore a partially consumed range from a VM snapshot or copy its allocation state into another process. These are trusted allocator assumptions, not protection against a malicious worker. Inside one incarnation, an atomic state update serializes allocation across threads. Retiring a worker does not erase its reservation high-water mark.
Region namespaces must also remain disjoint during disaster recovery. Failover under a different region number is straightforward; reusing a lost region's number requires its durable reservation history and assurance that no authority can grant overlapping ranges. Restoring an old registry backup and reopening allocations is unsafe.
4. Handle clock movement without repeating IDs
Within an assigned range, choose a logical timestamp at least as large as wall time, the range start and the previous emitted timestamp. If this falls beyond the range end, obtain another grant or stop. Advance the sequence atomically for repeated timestamps. On sequence exhaustion, advance to a later permitted timestamp or wait; never wrap the counter at the same timestamp. A backward wall-clock jump therefore cannot directly reset the sequence.
Range boundaries can force timestamps ahead of wall time, especially after repeated crashes. Set an explicit maximum permitted lead over a trusted time estimate and block when the next safe timestamp would exceed it. This trades availability for a stated approximation to chronological sorting. If no such bound is enforced, uniqueness can still hold, but calling the timestamps real creation times is misleading.
An inter-region partition does not stop new grants if each regional quorum remains healthy and its namespace is exclusive. During a regional registry outage, a worker may continue within its already reserved interval. Once it reaches the range end, it stops. Reserving larger intervals extends this autonomy but increases wasted timestamp space after a crash and can delay successor activation under the future-time bound. For a one-second interval, nominal sequence space is 1,000 × 4,096 = 4,096,000 IDs; time progression may exhaust the interval well before every sequence value is used.
5. Trace the duplicate-worker failure
In an unsafe lease-only design, A pauses after checking its lease but before selecting a timestamp. Its lease expires and B acquires slot 17. A resumes without rechecking and both processes select the current millisecond 1,000,042 with sequence 0. Both emit the same bit pattern. Perfect clock synchronization would not repair this ownership failure.
Under disjoint ranges, B starts at timestamp 1,001,000 or later, while A is bounded below that value. A's resumption cannot duplicate B's ID as long as each process obeys its grant and does not repeat its own state. If A was cloned, two copies could still collide inside A's range; the deployment must prevent state reuse, or use a design with separately allocated incarnation bits. Such bits are finite too: never recycle them while an old incarnation can still emit conflicting tuples.
A database uniqueness constraint provides a final detection layer. It does not prove IDs were globally unique before insertion, especially across independently accepting databases. Log worker, range and incarnation provenance so a collision is diagnosable; do not quietly regenerate and hide an allocator invariant violation.
6. Choose alternatives by the actual requirement
UUIDv7 offers a standardized 128-bit format with a 48-bit Unix millisecond timestamp and remaining space for randomness or permitted monotonicity constructions, excluding version and variant bits. It can avoid worker-slot coordination when using a correctly implemented random construction, at the cost of probabilistic uniqueness and wider keys. The exact implementation matters: do not assume every UUIDv7 library provides identical monotonicity or entropy.
For an illustrative pure 74-random-bit construction with independent uniform suffixes, one million IDs sharing the same timestamp have approximate collision probability n(n − 1)/(2 × 2^74) ≈ 2.65 × 10^-11. That is a per-timestamp-bucket calculation, not a lifetime guarantee. Accumulate risk across buckets and verify random-generator behavior, including process cloning. Counter-based variants need their own analysis.
If strict allocation order is essential, use a coordinated sequencer and accept its availability and latency constraints. Allocating blocks from that sequencer preserves uniqueness but permits a later block's values to be returned before an earlier block is consumed. Gapless committed invoice numbers, causal event order and compact storage keys are different problems; do not force one ID service to promise all three.
7. Operate and test the invariants
Test simultaneous requests at counter exhaustion, backward and forward clock jumps, process restart after a lost grant response, registry leader failover, exhausted reserved ranges, region recovery and accidental process-state cloning. Assert disjoint grants and unique emitted tuples, then verify lossless serialization through actual clients and databases.
Monitor range exhaustion, logical-clock lead, grant latency, rejected duplicate grants, clock anomalies and uniqueness violations. Plan the timestamp horizon migration before field exhaustion; never mask overflowing bits. Time-leading keys can also concentrate writes at an index edge, so evaluate storage partitioning separately. IDs reveal approximate time and possibly topology; they are identifiers, not authorization tokens or secrets.
8. Follow-ups and self-review
Follow-ups: Can two IDs prove which event committed first? No: allocation can precede a delayed or aborted write. Can a lease alone make worker reuse safe? No: stale emitters and missing incarnation information remain. What if the business demands uninterrupted operation through an unbounded partition? Finite reserved space cannot guarantee that; revise the availability contract or identifier design. Does a larger counter fix duplicate worker assignment? No: it changes capacity, not ownership.
Score 0–2 each for separating ordering promises, correct bit arithmetic, preventing overlap across worker incarnations, handling clock and counter limits, and matching failure behavior to the availability contract. A strong answer gives a concrete uniqueness argument rather than relying on synchronized clocks.
Sources and next steps
- RFC 9562: Universally Unique IDentifiers — May 2024 standard; UUIDv7 format and monotonicity considerations.
The 64-bit layout exercise, disjoint-range allocator, failure traces and capacity calculations are original practice, not a claim that RFC 9562 specifies this custom allocator. Continue with distributed locks for resource-side fencing.
Continue your preparation
Choose a connected learning path or browse more System design questions. Read our editorial method and attribution policy.