URL Shortener - Step 3: Core High-Level Design
Step 3 of 6: C - Core High-Level Design
Design the algorithm and basic architecture to satisfy functional requirements
✅ Decision: Counter
Snowflake ID → Base62
No collisions, fast, scalable
📊 Why Base62?
62^7 = 3.5 trillion URLs
Enough for 100+ years
⚡ Performance
< 1ms generation time
No DB check needed
🔢Alternative: Redis Distributed Counter▼
🎯 Third Approach: Use Redis INCR command for atomic counter operations across multiple instances
✅ Snowflake ID (Our Choice)
- • Coordination: None needed
- • Dependencies: None
- • Performance: Ultra-fast (<1ms)
- • Availability: No SPOF
- • Complexity: Medium
- • Collision Risk: Zero
🔄 Redis Counter
- • Coordination: Redis cluster
- • Dependencies: Redis availability
- • Performance: Fast (~2-5ms)
- • Availability: Redis SPOF
- • Complexity: Low
- • Collision Risk: Zero
❌ Hash-Based
- • Coordination: None
- • Dependencies: None
- • Performance: Degrades over time
- • Availability: No SPOF
- • Complexity: Low initially
- • Collision Risk: Increases with scale
🔧 Redis Counter Implementation
# Redis Cluster Setup
INCR url_counter # Atomic increment
GET url_counter # Returns: 1234567
base62_encode(1234567) # → "8KpQ"
# Multiple Redis masters for HA
counter_1 = INCR shard_1:counter
counter_2 = INCR shard_2:counter
✅ Redis Pros
- • Simple implementation (just INCR)
- • Guaranteed atomic operations
- • Sequential IDs (good for analytics)
- • Easy to understand and debug
- • Can shard counters for scale
❌ Redis Cons
- • Redis becomes critical dependency
- • Network latency for each generation
- • Need Redis HA setup complexity
- • Counter state persistence concerns
- • Potential bottleneck at massive scale
🤔 When to Use Redis Counter: Great for simpler systems where you already have Redis infrastructure. For massive scale (our 100M URLs/day), Snowflake's independence wins, but Redis counter is perfectly viable for millions of URLs/day with proper HA setup.
🏗️ Basic System Architecture
🔀 Why Separate Read/Write Services?
Write Service (1,200 QPS)
- •CPU-light: Just ID generation
- •Few instances needed (3-5)
- •Stateless, horizontal scaling
- •Can optimize for writes
Read Service (11,600 QPS)
- •Cache-heavy: Redis first
- •More instances (20-30)
- •Can scale independently
- •Optimize for low latency
💡 Core Design Complete
Algorithm Chosen
Counter-based with Snowflake ID for guaranteed uniqueness
Architecture Pattern
Read/Write separation for independent scaling
Storage Strategy
NoSQL with Redis cache for hot URLs