URL Shortener System Design: Capacity Planning, Bottlenecks & Simulation
A complete architectural breakdown of a TinyURL / Bitly tier service handling 50,000 read QPS: Base62 key generation, 100:1 read/write traffic ratios, 80/20 cache sizing, database connection pool exhaustion, and discrete event simulation.
Requirements & Capacity Planning
Production sizing starts from hard capacity constraints and storage budgets, not hand-waving. Here is the operational capacity math for 500M writes/month under skewed read traffic.
System Requirements Matrix
- Given a long URL, return a unique 7-character short URL (e.g.,
https://short.io/abc12xy). - When accessing a short URL, redirect users to original destination via HTTP 301 or 302.
- Users can optionally provide custom aliases and expiration timestamps (TTL).
- Analytics recording (clicks, referrer, geolocations) without blocking redirect latency.
301 (Permanent): Browsers cache the redirect locally; subsequent clicks bypass your servers completely. Decreases server load by 35% but blinds click analytics.
302 (Found / Temporary): Every request hits the backend first. Mandatory if accurate billing, rate limiting, and real-time click telemetry are required.
Back-of-the-Envelope Capacity Math
Key Generation Strategy: Base62 vs Distributed Range
How do you encode 64-bit integer IDs into compact URLs without hash collisions, database locks, or coordination bottlenecks?
MD5 / SHA-256 Truncation
Compute MD5(original_url), convert to Base62, and take the first 7 characters.
Centralized Auto-Increment ID
Store URLs in PostgreSQL with an auto-incrementing BIGSERIAL primary key. Convert integer ID to Base62.
Distributed Range Key Generator
ZooKeeper or Raft coordinator assigns unique blocks of 100,000 IDs to compute instances. Each worker generates IDs locally in memory without coordination.
Length 6: 62^6 = 56,800,235,584 (56.8 Billion unique URLs)
Length 7: 62^7 = 3,521,614,606,208 (3.52 Trillion unique URLs). At 500M URLs/month, length 7 lasts for over 580 years.
Where the Naive Design Crashes Under Load
Why naive architectures fail in production: Little's Law, thread starvation, and database connection pool exhaustion.
The 50,000 QPS Direct Database Crash
Junior engineers often assume a relational database with an indexed hash column can handle 50,000 reads/sec. Queueing theory proves mathematical impossibility on single-node storage:
The Cascade Sequence
- Connection Pool Exhaustion: Postgres default
max_connections = 100saturates in under 2ms. Subsequent queries queue in memory. - Upstream Worker Lockup: Node.js event loops and Go worker routines block waiting on the DB pool, exhausting thread limits.
- Client Retry Storm: Edge clients time out after 150ms and trigger 3 retries, multiplying ingress load from 50k to 150k QPS.
- Total 504 Outage: API Gateway returns HTTP 504 Gateway Timeout across 100% of incoming traffic.
The Resilient Production Architecture
How StackMonkey models the complete, fault-tolerant topology with edge caching, token bucket throttling, read-through Redis clusters, and async Kafka logging.
allkeys-lru to automatically cycle out cold links.
Simulate the URL Shortener Topology Live
Watch Redis absorb 80% read traffic, observe database pool saturation under cache eviction, and adjust queue depth thresholds in real-time.