Unique ID generator
medium 28 minGive every record its own number, from many servers at once.
When it comes up
The moments this idea belongs in a design interview, and the moments it does not.
Signals
When to bring it up
| You hear | Say | Because |
|---|---|---|
| Many servers create records | Time-ordered IDs made in each server | No central counter |
| Short public code | Leased range, scrambled, in base 62 | Short and unique |
| Newest first | Snowflake or UUIDv7 as the cursor | The ID is already in order |
| Private link | Random token with expiry | IDs can be guessed |
| Chat order | Sequence per conversation | Clocks do not give exact order |
Twenty services in three regions create orders. How do they get IDs?
BreaksA counter in each service
- How
- Every service counts from 1.
- Limit
- Duplicates on the first day.
- Choose when
- Never with more than one writer.
WorksOne ID service with a sequence
- How
- Each order asks it for a number.
- Limit
- Every order depends on it, and two regions wait for a far one.
- Choose when
- One region with modest volume, and blocks of numbers instead of one by one.
StrongIDs made in each instance
- How
- UUIDv7, or Snowflake with leased machine numbers and clock guards. Primary key as the last check.
- Limit
- Order across machines is only approximate.
- Choose when
- Several writers or regions.