Design a URL Shortener (TinyURL)

Base62 encoding, Key Generation Service (KGS), 100:1 read-heavy Redis caching, and HTTP 301 vs. 302 redirects

Difficulty: Beginner · Read time: 9 min · Access: Step-1 Preview + Knoera Pro

How It Works

A URL (Uniform Resource Locator) shortener maps long web links to compact 7-character codes (`sho.rt/aB9x2K1`). With `62^7 ≈ 3.5 trillion` combinations, Base62 encoding (`a-z`, `A-Z`, `0-9`) fed by a distributed Key Generation Service (KGS) or ZooKeeper counter ranges avoids the hash collisions of truncated MD5/SHA-256 (Message-Digest 5 / Secure Hash Algorithm 256) digests. Because redirect reads outnumber link creation writes by 100:1, an in-memory Redis cache serves 95%+ of lookups in under 5 ms while an asynchronous Apache Kafka pipeline records click analytics.

Why It Matters

TinyURL is the classic system design benchmark testing back-of-the-envelope capacity math, collision-free distributed ID generation, hot-key cache protection, and HTTP (Hypertext Transfer Protocol) redirect semantics (`301 Moved Permanently` vs. `302 Found`).

Interactive Architecture Scenarios

  • End-to-End Shorten & Redirect Flow
  • Failure Mode: Viral Hot-Key Stampede & DB Collapse

Related Lessons in Real-World Case Studies