Skip to main content
HS
HiSETSuccess
LeetCode Prep

System Design

Comprehensive interview guide: scalability, databases, distributed systems, cloud architecture, and full case-study walkthroughs — plus adaptive quizzes.

Case study

Design a Unique ID Generator

Generate globally unique, roughly sortable 64-bit IDs at 10K+/sec without coordination per ID.

SnowflakeUUIDdistributed

Requirements

  • Functional: generate unique ID; optionally expose batch generate
  • Properties: unique across all datacenters; roughly time-ordered (sortable)
  • Non-functional: 10K IDs/sec per machine; latency < 1ms local
  • No central DB round-trip per ID (bottleneck)
  • IDs numeric or fixed-length string (base62)

Compare to UUID v4

UUID v4 is random — not sortable and poor for B-tree indexes. Snowflake/truncated timestamp IDs give locality that improves DB insert performance.

Back-of-envelope Estimation

MetricValueNotes
Global ID rate1M/sec peakTwitter-scale order of magnitude
Per machine10K/sec100 machines suffice with headroom
ID size64 bits (8 bytes)Fits in BIGINT; 18 decimal digits max
Clock drift budget< 5 msNTP sync required per host

API Design

EndpointDescription
GET /v1/idReturns { id: 1234567890123456789 }
GET /v1/id/batch?count=100Returns array; max 1000 per request
Internal: allocate_worker_idZooKeeper/etcd lease assigns machine ID 0–1023

Snowflake Bit Layout

FieldBitsRange
Unused/sign10
Timestamp (ms since epoch)41~69 years
Datacenter + machine ID101024 machines
Sequence124096 IDs/ms per machine

Per millisecond: increment sequence. If sequence overflows, spin until next ms. On clock backward jump, wait or error — never reuse timestamp+sequence pairs.

High-level Design

ID service deployment
App servers ──► ID Generator lib (embedded) ──► local clock + worker_id
                    │
                    └── optional central service for worker_id lease only

ZooKeeper/etcd: /workers/{dc}/{host} → worker_id (ephemeral node)

Prefer embedded library (Twitter Snowflake style) over RPC per ID — eliminates network hop. Central service only coordinates worker_id assignment on startup.

Deep Dive: Alternatives

ApproachProsCons
DB auto-incrementSimple, strict orderSingle-writer bottleneck; hard to shard
UUID v4No coordinationRandom; 128 bits; index fragmentation
SnowflakeSortable; high throughputClock dependency; machine ID management
Redis INCREasySingle point of failure; network per ID

Bottlenecks & Edge Cases

  • Clock skew backward — refuse to generate until caught up; metric clock_rollback_events
  • Worker ID exhaustion (1024 machines) — extend bits or hierarchical IDs per service
  • Leap seconds / VM pause — sequence buffer absorbs brief stalls; alert on NTP drift > 1s
  • Hot partition in DB if IDs used as sole shard key — combine with secondary sharding key

Failure Modes & Monitoring

MetricAlert threshold
ids_generated/sec per hostApproaching 4096/ms sustained
clock_rollback_count> 0 per minute
worker_id lease renewals failedAny failure
duplicate_id_detectedCritical — should never happen

Testing uniqueness

Chaos-test clock jumps and worker restarts. Run collision detector in staging comparing ID sets across nodes.