Design a URL shortener that handles 100 million new links per month and 10 billion redirects.

Short answer

Separate the write path (create short code) from the read path (redirect). Generate unique codes with a distributed ID scheme or pre-allocated ranges and base62 encoding, store mappings in a key-value store, serve redirects from cache and CDN, and stream click events asynchronously to analytics.

Detailed answer

Requirements: create short links (custom alias optional), redirect fast (p99 < 50 ms), links rarely change, analytics on clicks, expiry support.

Estimates: 100M links/month ≈ 40 writes/s; 10B redirects/month ≈ 3,900 reads/s average, ~15K/s peak. Storage: 100M × 12 months × 5 years × ~500 bytes ≈ 3 TB. Read-heavy and cacheable.

Design:

  • Code generation: 7 base62 characters give 62⁷ ≈ 3.5 trillion codes. Use a counter-based approach with ranges allocated to each app instance (from a coordination store) to avoid collisions without per-request coordination, or a Snowflake-style ID encoded in base62. Hashing the URL needs collision handling.
  • Storage: key-value store (DynamoDB, Cassandra, Bigtable) keyed by code; or PostgreSQL at this scale.
  • Redirect path: CDN/edge cache → service → Redis → DB. Return 301 (cacheable, fewer analytics) or 302 (every click hits you, better analytics).
  • Analytics: emit click events to Kafka/Kinesis; aggregate asynchronously.

Architecture diagram

flowchart LR
  U[User] --> CDN[CDN / edge]
  CDN --> R[Redirect service]
  R --> C[(Redis)]
  R --> DB[(Key-value store)]
  R -- click event --> K[[Stream]] --> AN[Analytics]
  A[Creator] --> W[Create API] --> IDS[ID range allocator] 
  W --> DB

Real-world example

Bitly and TinyURL use variations of this design; Twitter's t.co wraps all links for safety checks and analytics.

Follow-up questions

  • How do you prevent malicious URLs? (Safe Browsing checks on create and periodically.)
  • How do you support custom aliases? (Conditional insert; reserved words.)
  • How do you delete expired links at scale? (TTL in the store.)

What the interviewer expects

Clear requirements and numbers, a collision-free ID strategy, read-path caching, the 301 vs 302 trade-off, and async analytics.

Common mistakes to avoid

Using a single auto-increment database sequence as a global bottleneck, synchronous analytics writes on the redirect path, ignoring abuse.