All system design challenges
#76 Key Generation/Read-Heavy Caching

URL Shortener

Turn long links into short ones and redirect billions of clicks quickly, without two links ever getting the same code.

Asked at Expedia Bitly Google

What is being asked

Design a URL shortener like Bitly. A travel company uses one for SMS and WhatsApp messages, where long booking links don't fit. Writing is rare; redirecting is extremely frequent.

Requirements

Functional

  • Create a short code for a long URL, optionally custom ("/goa-deal") and with an expiry.
  • Redirect the short URL to the long one.
  • Count clicks per link.

Non-functional

  • Redirects in a few milliseconds, highly available.
  • Codes are short (6–8 characters) and not easily guessable.
  • Read : write ratio of 100 : 1 or more.

Design

BrowserCDN / edgeRedirect serviceCreate APIRedis cacheURL storeID generatorClick events
Boxes are services; rounded shapes are data stores and queues.
  1. Create: get a unique number from the ID generator, encode it in base62 (a–z, A–Z, 0–9), and store code → long URL. Seven base62 characters give about 3.5 trillion codes.
  2. Redirect: look up the code in Redis, falling back to the store; respond with 302 (or 301 for permanent links).
  3. Click events go to a queue and are counted asynchronously, so analytics never slow down a redirect.

Key decisions and trade-offs

  • ID generation. A counter handed out in ranges (each server takes a block of 1,000) avoids collisions and coordination per request. Hashing the URL avoids a counter but needs collision handling.
  • Guessability. Sequential IDs make codes enumerable; shuffle them (encrypt the number) if links point at private data like booking pages.
  • 301 vs 302. A 301 is cached by browsers and saves traffic but stops you counting repeat clicks.
  • Storage is a simple key-value lookup; any KV store or a single indexed table scales a long way.

Follow-up questions

  • How do you stop the service being used for phishing links?
  • Two users shorten the same long URL. Same code or different?
  • How do you delete expired links at scale?
  • How would you shard the store at 10 billion links?
Log in to tick off the challenges you have worked through. Log in