Design a Complete URL Shortener
Design a URL shortening service like bit.ly from scratch. Cover requirements gathering, API design, encoding strategy, database schema, caching, scaling to 20K QPS reads, analytics, and monitoring. Walk through the complete architecture.
Base62 Encoding
An encoding scheme using 62 characters [a-zA-Z0-9] to convert numeric IDs into compact alphanumeric strings. Each character represents a digit in base-62.
Base62 maps auto-incrementing IDs to short, URL-safe strings with zero collision probability. It is the preferred encoding for most URL shortener designs.
Read-Heavy Caching (Cache-Aside)
A caching pattern where the application checks the cache first, falls back to the database on a miss, and populates the cache before returning. Ideal for read-heavy workloads.
URL shorteners have a 100:1 read:write ratio. Caching the top 20% of URLs serves 80% of traffic (Zipf distribution), dramatically reducing database load.
Database Sharding
Horizontal partitioning of data across multiple database instances. Each shard holds a subset of rows, determined by a shard key (e.g., hash of short_code).
A single database cannot hold 15TB+ of URL data. Sharding by short_code hash distributes reads and writes evenly across shards.
TTL and Expiration
Time-to-live (TTL) sets a maximum lifetime for a record. Expired URLs are cleaned up by a background job, freeing short codes for reuse and bounding storage growth.
Without TTL, storage grows unboundedly. A 2-year default TTL with background cleanup keeps the system healthy and reclaims short codes over time.
Horizontal Scaling
Adding more machines (scale out) rather than upgrading a single machine (scale up). Stateless application servers can be scaled horizontally behind a load balancer.
At 20K read QPS, a single server is insufficient. Stateless app servers behind a load balancer scale linearly with traffic.
CDN Edge Caching
Caching redirect responses at CDN edge nodes closest to the user. Reduces latency and offloads traffic from origin servers for globally popular URLs.
For a global URL shortener, edge caching can serve redirects in under 10ms for popular URLs without hitting the origin server at all.