Skip to content
Commitwise
Menu

How would you generate unique IDs in a distributed system?

System designSenior

In shortOptions are database sequences, UUIDs (v4 random, v7 time-ordered), and Snowflake-style 64-bit IDs (timestamp, machine id, sequence). The choice depends on whether IDs must be sortable by time, compact, unguessable and generated without coordination.

Model answer

Clarify requirements: globally unique? roughly sortable by creation time? 64-bit for compact indexes or 128-bit fine? generated offline on mobile clients? safe to expose in URLs?

Options:

ApproachProsCons
Database sequence / auto-incrementSimple, compact, orderedSingle coordinator; sharding needs ranges or step offsets; reveals volume
UUIDv4 (random)No coordination, unguessable, generated anywhere128-bit, random inserts fragment B-tree indexes
UUIDv7 (time-ordered)No coordination, sortable, good index locality128-bit; creation time is visible
Snowflake-style (41-bit ms timestamp, 10-bit worker id, 12-bit sequence)64-bit, sortable, ~4,096 IDs/ms per workerNeeds unique worker ids and sane clocks

Snowflake details: worker ids come from configuration or a coordinator (ZooKeeper, a lease table); if the clock moves backwards, the generator must wait or refuse rather than risk duplicates; the custom epoch decides how many years 41 bits cover (~69).

Practical defaults: UUIDv7 for most services (no coordination, good locality, works on clients); Snowflake-style when 64-bit IDs matter at very high volume; a separate random public identifier (or opaque token) when internal IDs shouldn’t be exposed or enumerable.

What they're looking for

Requirements first (uniqueness, ordering, size, coordination, exposure), the trade-offs of each approach, clock issues for time-based IDs, index locality in databases, and not exposing sequential IDs publicly.

Common mistakes

  • Random UUIDv4 as a clustered primary key on a write-heavy table without considering index locality.
  • Ignoring clock skew and clock going backwards for Snowflake IDs.
  • Exposing sequential IDs that leak business volume or enable enumeration.

More system design questions

Esc

Type to search the whole site.