System Design (50 Qs)•Part 2: Real-World High-Scale Architecture Problems (11 - 25)
Design a Distributed URL Shortener (TinyURL / Bit.ly).
In Simple Words:
Design a scalable service that converts long URLs (e.g. 150 characters) into concise aliases (e.g. https://tiny.cc/a8X9k2) and redirects users with sub-15ms latency.
Scale Estimations:
- Traffic: 100M new URLs created per month (~40 writes/sec), 10B redirects per month (~4,000 reads/sec → 100:1 Read-Heavy).
- Storage: 100M URLs × 500 bytes per record × 12 months × 5 years = 3 TB total storage (easily manageable).
- Base62 Character Set:
[a-zA-Z0-9](62 characters).- Length 6: $62^6 \approx 56.8$ Billion combinations.
- Length 7: $62^7 \approx 3.5$ Trillion unique combinations (safely covers 100 years).
Architecture Flowchart
flowchart TD
Client["User Browser"] -->|"1. GET /a8X9k2"| CDN["Edge CDN / Cloudflare"]
CDN -->|"Cache Miss"| LB["Load Balancer"]
LB --> WebApp["URL Redirection Service"]
WebApp --> Redis["Redis Cluster (LRU Hot Cache)"]
Redis -.->|"Cache Miss"| DB["NoSQL Storage (DynamoDB / Cassandra)"]
WebApp -->|"301 / 302 Redirect"| Clienttypescript
// Base62 Converter for Distributed Unique Integer IDs
const BASE62_CHARS = '0123456789abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ';
export function encodeBase62(num: bigint): string {
if (num === 0n) return '0';
let str = '';
let n = num;
while (n > 0n) {
const rem = Number(n % 62n);
str = BASE62_CHARS[rem] + str;
n = n / 62n;
}
return str;
}