High Level Design

Designing a Unique ID Generator

Designing a distributed unique ID generator — comparing multi-master replication, UUID, ticket servers, and Twitter Snowflake, with a deep dive into the 64-bit Snowflake layout and follow-up considerations.

August 10, 2026

A unique ID generator creates globally unique identifiers across distributed systems. Properties vary by design: globally unique (no collisions), K-sortable (IDs roughly increase with time), compact, stateless or stateful.

A naive approach is auto-increment in a central database — but that creates a bottleneck and a single point of failure.


Step 1: Requirements#

QuestionAnswer
Globally unique?Yes
K-sortable (ordered by date)?Yes — ordered by timestamp, not by increment of 1
ID length64-bit integer
Numeric only?Yes
ScaleUp to 10,000 ID requests per second
Latency< 1ms per ID generation
AvailabilityHighly available and fault-tolerant

Challenges in distributed ID generation:

  • Collisions — multiple nodes generating IDs independently may overlap.
  • Scalability — must support billions of IDs efficiently.
  • Ordering — some systems require time-ordered IDs (e.g., Kafka offsets, Twitter posts).
  • Availability — must not depend on a single machine or DB auto-increment.

Step 2: Options Comparison#

OptionDescriptionProsCons
Multi-Master ReplicationMultiple masters generate IDs from guaranteed unique rangesScales with number of nodesNot time-ordered; hard to scale across data centers
UUID128-bit identifier, no central coordinationSimple; no SPOF; scales with serversNot K-sortable; 128 bits (large); not numeric
Ticket ServerCentralised server generates IDs; clients request themNumeric; K-sortable; works at small–medium scaleSingle point of failure; scalability issues under high load
Snowflake (Twitter)64-bit ID from timestamp + datacenter ID + machine ID + sequenceNumeric; K-sortable; highly scalable; low latencyRequires unique machine ID configuration; more complex

Visual comparison of approaches:

Multi-master replication: multi-master

UUID: UUID

Ticket server: ticket server

Snowflake: snowflake


Twitter Snowflake Design#

Snowflake is a 64-bit unique ID algorithm. It provides:

  • Global uniqueness
  • Time ordering
  • High throughput (millions of IDs/sec)

64-bit ID Layout#

BitsFieldNotes
1Sign bitAlways 0 — reserved for future use
41TimestampMilliseconds since custom epoch — gives ~69 years of IDs
5Datacenter IDSupports up to 32 datacenters
5Machine IDSupports up to 32 machines per datacenter
12Sequence Number4,096 IDs per machine per millisecond

How it works#

  • Datacenter ID and Machine ID are configured at startup (env vars / config files) and fixed for the lifetime of the instance.
  • Timestamp — the part that makes IDs time-sortable. Generated at runtime.
  • Sequence Number — incremented by 1 per ID; resets to 0 each millisecond. If the counter exceeds 4,095 within the same millisecond, the generator waits until the next millisecond.
  • If a machine restarts, it must use the same Machine ID to avoid collisions.

Throughput: up to 4,096 IDs per machine per millisecond = ~4M IDs/sec per machine. 32 machines per datacenter × 32 datacenters = 1,024 machines globally.


Follow-Ups#

1. Clock Synchronization#

Snowflake assumes all servers have the same clock. With multiple cores or machines, clock drift is real.

Solution: Use NTP for clock synchronisation. If a machine's clock drifts backward, it must wait until the clock catches up before generating new IDs.

2. Section Length Tuning#

Application typeTrade-offApproach
Low-concurrency, long-livedDon't need millions of IDs/msFewer sequence bits, more timestamp bits → IDs valid for centuries
High-concurrency (Twitter, TikTok)Need thousands of IDs/msMore sequence bits (12–14) → shorter lifespan (~50–70 years, acceptable)

3. Machine Failures#

  • On restart, the machine must use the same Machine ID.
  • If permanently decommissioned, the Machine ID can be reused after a safe period (after the maximum timestamp duration has elapsed).

4. High Availability#

Since ID generation is mission-critical, deploy multiple instances behind a load balancer.

5. Choosing the Right ID Structure#

CriteriaUUID v4Hash (MD5/SHA-1)SnowflakeULIDSharded DB Auto-Increment
Uniqueness✅ Very high (128-bit)✅ Deterministic✅ Guaranteed✅ Very high✅ Within shard
Time ordering❌ None❌ None✅ Strong✅ Strong✅ Weak (shard-based)
Size128-bit128–160-bit64-bit ✅128-bit64-bit ✅
Compact❌ Long string❌ Long string✅ int64❌ 26 chars✅ int64
CoordinationNoneNoneUnique machine IDsNoneShard offsets
Best Use CasesSession IDs, cache keys, security tokensFile deduplication, content IDsTweet IDs, orders, events, logsDB keys, distributed logsLegacy/small-scale sharding