| Scale | Requests per Second (RPS) | ID Generation Method | Storage Needs | Latency | Notes |
|---|---|---|---|---|---|
| 100 users | ~10 RPS | Simple timestamp + counter | Minimal (few KB) | <1 ms | Single server, no concurrency issues |
| 10,000 users | ~1,000 RPS | Timestamp + machine ID + sequence number | Small (MB) | <5 ms | Single server with concurrency control or small cluster |
| 1,000,000 users | ~100,000 RPS | Distributed ID generator (e.g., Snowflake) | Moderate (GB logs) | <10 ms | Multiple servers, coordination needed |
| 100,000,000 users | ~10,000,000 RPS | Highly distributed, sharded generators + caching | Large (TB logs) | <20 ms | Global distribution, fault tolerance critical |
Design a unique ID generator in HLD - Scalability & System Analysis
Start learning this pattern below
Jump into concepts and practice - no test required
The first bottleneck is the central coordination or state management that ensures uniqueness. At low scale, a single server can handle ID generation easily. As traffic grows, the server's CPU and memory limits are reached due to concurrency and synchronization overhead. Also, network latency and clock synchronization issues arise in distributed setups.
- Horizontal Scaling: Add more ID generator nodes with unique machine IDs to distribute load.
- Sharding: Partition ID space by machine or region to avoid collisions.
- Caching: Pre-generate ID blocks to reduce coordination calls.
- Use of Time-based IDs: Incorporate timestamps to reduce coordination.
- Coordination Services: Use lightweight consensus or coordination (e.g., ZooKeeper) carefully to avoid bottlenecks.
- Fault Tolerance: Design for node failures without ID collisions.
- At 1M users generating 100K IDs/sec, each ID ~8 bytes -> 800 KB/sec storage if logged.
- Network bandwidth for 100K RPS with 8-byte IDs ≈ 0.8 MB/sec, easily handled by 1 Gbps network.
- CPU: Each server can handle ~5K concurrent ID requests; need ~20 servers for 100K RPS.
- Storage: Logs and backups grow ~70 GB/day at 100K RPS.
Start by clarifying requirements: ID length, uniqueness scope (global or per service), latency needs, and failure tolerance. Then discuss simple solutions for low scale and identify bottlenecks as scale grows. Propose incremental scaling strategies and justify choices with trade-offs. Always mention fault tolerance and collision avoidance.
Your database handles 1000 QPS. Traffic grows 10x to 10,000 QPS. What do you do first?
Answer: Since the database is the bottleneck, first add read replicas or caching to reduce load. For ID generation, move from a single centralized generator to a distributed approach with machine IDs and sequence numbers to avoid database contention.
Practice
Solution
Step 1: Understand the role of unique IDs
Unique IDs ensure that each identifier is different from others, avoiding conflicts.Step 2: Recognize distributed system needs
In distributed systems, IDs must be unique across machines and time to prevent collisions.Final Answer:
To create identifiers that are distinct across all machines and time -> Option AQuick Check:
Unique ID purpose = distinct identifiers [OK]
- Confusing unique ID with encryption
- Thinking unique ID compresses data
- Mixing load balancing with ID generation
Solution
Step 1: Identify components of unique ID generators
Common components include timestamp, machine identifier, and sequence number.Step 2: Understand sequence number role
Sequence numbers help generate multiple unique IDs within the same timestamp to avoid collisions.Final Answer:
Sequence number to avoid collisions within the same timestamp -> Option DQuick Check:
Sequence number = collision avoidance [OK]
- Confusing encryption with ID generation
- Thinking compression is part of ID design
- Mixing load balancing with ID components
Solution
Step 1: Understand bit allocation for sequence number
The sequence number uses 12 bits, so max IDs per millisecond = 2^12.Step 2: Calculate 2^12
2^12 = 4096 unique IDs per millisecond per machine.Final Answer:
4096 -> Option CQuick Check:
2^12 = 4096 [OK]
- Using machine ID bits instead of sequence bits
- Calculating 2^10 or 2^11 instead of 2^12
- Confusing total bits with sequence bits
Solution
Step 1: Analyze ID components for uniqueness
Machine ID differentiates IDs from different machines at the same time.Step 2: Identify cause of duplicates
If machine IDs are missing or not unique, IDs from different machines can collide.Final Answer:
Machine IDs are not unique or not included in the ID -> Option AQuick Check:
Missing unique machine ID = duplicates [OK]
- Blaming timestamp size for duplicates
- Thinking longer sequence number causes duplicates
- Confusing encryption with ID uniqueness
Solution
Step 1: Consider scalability and uniqueness needs
Global scale requires IDs unique across machines and time, with high throughput.Step 2: Evaluate design options
Combining timestamp, machine ID, and sequence number in 64 bits with synchronized clocks ensures uniqueness and scalability.Step 3: Reject other options
Random IDs risk collisions; timestamp-only lacks machine uniqueness; central server causes bottleneck.Final Answer:
Use a 64-bit ID combining timestamp, machine ID, and sequence number with synchronized clocks -> Option BQuick Check:
64-bit composite ID = scalable unique IDs [OK]
- Relying on random IDs risking collisions
- Ignoring machine ID causing duplicates
- Using central server causing bottlenecks
