Snowflake Style ID Generator With Clock Skew Handling
Last updated: November 29, 2026
Quick Overview
Implement a thread-safe generator of 64-bit, roughly time-ordered unique IDs made from a millisecond timestamp, a 10-bit worker id and a 12-bit sequence, and define its behaviour when the sequence overflows or the system clock moves backwards.
X
November 29, 20260
0
0 solved
Implement a thread-safe generator of 64-bit, roughly time-ordered unique IDs made from a millisecond timestamp, a 10-bit worker id and a 12-bit sequence, and define its behaviour when the sequence overflows or the system clock moves backwards.
Every post ID on X is a Snowflake ID, which is why IDs sort by creation time and can be generated without a central database. The interviewer is checking bit packing, concurrency, and whether you think about the operational failure (NTP stepping the clock back) that can create duplicate IDs.
What the Interviewer Expects
- Lays out the bit budget and its limits (years of range, IDs per ms per worker)
- Uses a lock or atomic for the sequence
- Waits for the next millisecond on sequence overflow
- Refuses or waits when the clock goes backwards
- Makes the clock injectable for tests
- Provides a decode function
Key Topics to Cover
How to Approach This
- Apply SOLID principles. Single Responsibility makes code testable, Open/Closed makes it extensible.
- Choose data structures based on access patterns, not familiarity.
- Prefer immutable data and message passing over shared mutable state for concurrency.
- Design APIs with RESTful conventions, versioning, meaningful errors, and pagination from day one.
Possible Follow-up Questions
- How are worker ids assigned and kept unique across a fleet that autoscales?
- What happens to sort order between two workers whose clocks differ by 20 ms?
- Why do some systems leave the top bit unused?
- How would you generate IDs in a client that is offline?
Sharpen Your Skills on Codemia
Practice similar problems with our interactive workspace, get AI feedback, and track your progress.
Practice System Design ProblemsSample Answer
Requirements
64-bit IDs, unique across the fleet, k-sorted by time, generated locally in microseconds. Layout: 1 unused sign bit, 41 bits of milliseconds since a custom epoch (about 69 years), 10 bits worker id (1...
High-Level Design
Each process holds last_ms and seq under a lock. Same millisecond: increment seq; if it wraps to 0, spin until the next millisecond. New millisecond: reset seq to 0. If the clock reads earlier...