Full answer
Requirements to clarify
Unique across all servers, generated with no shared bottleneck, and often: fits in 64 bits, roughly sortable by creation time, not guessable.
Options
Database auto-increment. Simple and compact. With one database it is a single point of failure and a write bottleneck. Across shards you can use offsets (shard 1 uses 1, 11, 21...; shard 2 uses 2, 12, 22...), but adding shards is awkward. Sequential IDs also reveal business volume and are easy to guess (/invoice/1001, /invoice/1002).
Ticket server or ID blocks. A central service hands each app server a block of 1,000 IDs at a time. Few round trips, still mostly sequential.
UUIDv4. 122 random bits. Collisions are so unlikely they are ignored in practice. No coordination. But 16 bytes per key and every index, and random values insert all over the B-tree, causing page splits and poor cache locality.
Time-ordered IDs. Snowflake-style layout in 64 bits:
| Bits | Field | Meaning |
|---|---|---|
| 1 | sign | always 0 |
| 41 | timestamp | milliseconds since a custom epoch |
| 10 | machine ID | up to 1,024 generators |
| 12 | sequence | up to 4,096 IDs per millisecond per generator |
41 bits of milliseconds is 2^41 = 2,199,023,255,552 ms, about 69.7 years from the chosen epoch. One generator can create 4,096 x 1,000 = 4,096,000 IDs per second. IDs sort roughly by time, which is ideal for feeds and chat messages.
ULID and UUIDv7 give the same time-ordering in a 128-bit, UUID-compatible format.
Pitfalls
- Clock going backwards (NTP correction): a Snowflake generator must detect it and wait or refuse, or it may reuse IDs.
- Machine ID assignment: two generators with the same machine ID create duplicates. Assign from a coordination service or a config with leases.
- Ordering is only approximate across machines, because clocks differ slightly.
- Exposure: time-based IDs reveal creation time. Use a separate random public token where that matters.
Choosing
Use Snowflake-style or UUIDv7 for high-volume, time-ordered entities in sharded systems. Use UUIDv4 when you need unguessable IDs and volume is moderate. Plain auto-increment is fine for a single database.
By level
Fresher: Explain why auto-increment is a problem across many servers and what a UUID is. Mentioning UUIDs as a no-coordination option is enough.
Mid: Explain the Snowflake layout with arithmetic, sorting benefits and index locality, and compare with UUIDv4 and UUIDv7. The 41-bit and 12-bit arithmetic should be correct.
Senior: Discuss clock skew handling, generator identity leasing, multi-region generation, ID exposure and migration of ID schemes in an existing system. Expect a plan for clock rollback and duplicate machine IDs.
Key points
- Auto-increment is simple but centralised and guessable
- UUIDv4 needs no coordination but is large and random for B-trees
- Snowflake: 41-bit ms timestamp, 10-bit machine, 12-bit sequence in 64 bits
- Time-ordered IDs sort roughly by creation time
- Handle clock skew and unique machine ID assignment
Common mistakes
- Ignoring clock rollback in time-based generators
- Assuming time-ordered IDs give exact global ordering
- Using sequential IDs in public URLs for private resources
- Forgetting the index cost of random 128-bit keys
Follow-ups
- What happens if two Snowflake generators get the same machine ID?
- How would you generate short codes for a URL shortener?
- Why do random UUID primary keys slow inserts in some databases?
Tips
- List requirements first, then options
- Draw the Snowflake bit layout
- Do the 41-bit and 12-bit arithmetic out loud
Evaluation rubric
- Compares options on coordination, size, ordering and guessability
- Correct Snowflake layout and arithmetic
- Identifies clock and machine ID pitfalls
- Recommends an option for a stated use case