System design case study C01 · Caching · IDs

Design a URL Shortener

Turn long links into short ones like sho.rt/aZ3k9Qx, and redirect billions of clicks quickly. Small problem, surprisingly rich deep dives.

What you'll be able to explain
  • Generate short codes without collisions
  • Serve a read-heavy redirect path from cache and edge
  • Choose 301 vs 302 based on analytics needs
  • Collect click analytics without slowing redirects
  • Handle custom aliases, expiry and abuse

Scope

Functional

  • Create a short URL for a long URL; optionally a custom alias and expiry.
  • Redirect short URL → long URL.
  • Basic click analytics per link (counts by day, country, referrer).

Non-functional

  • Redirect latency p99 < 50 ms (server side); very high availability for redirects.
  • Codes must be unique and not trivially guessable.
  • Read-heavy: ~100 redirects per creation.

Out of scope: user accounts beyond API keys, link-in-bio pages, QR codes.

Size

  • 100M new links/month → ~40 creates/s (peak ~200).
  • Redirects 100× → ~4,000/s average, ~40k/s peak (viral links).
  • Record ≈ 500 B → 50 GB/month → ~6 TB in 10 years: one sharded-later relational or KV store handles it.
  • 7 base62 characters = 3.5 trillion codes ≫ 12B links over 10 years.
  • Click events: 4,000/s × ~200 B ≈ 70 GB/day of raw analytics.

Decision: the redirect path is everything. Optimise reads with caches and edge; writes are trivial.

APIs and data model

POST /v1/links        {long_url, custom_alias?, expires_at?}  → {code, short_url}
GET  /{code}          → 302 Location: long_url
GET  /v1/links/{code}/stats?from=&to=  → aggregated counts
DELETE /v1/links/{code}
Table Key Fields
links code (PK) long_url, owner_id, created_at, expires_at, status
click_rollups (code, day) total, by_country (map), by_referrer (map)

The architecture

CLIENTBrowserNETWORKCDN / edgecaches redirectsSERVICERedirect serviceCACHERediscode → urlDATABASELinks DBKV / SQLSERVICECreate serviceWORKERCode generatorrangesQUEUE / LOGClick logKafkaWORKERAggregatorGET /aZ3k9Qxmisscache missclick eventPOST /linksnext codeinsert
URL shortener: a fast redirect path, a separate write path, and async analytics.

The redirect path: CDN → redirect service → Redis → DB. The click is published asynchronously to Kafka; the redirect never waits for analytics.

The 30-second summary

Say it in the interview

"A read-optimised service: codes from leased ID ranges, scrambled and base62-encoded, so they're unique with no per-request coordination. Redirects are 302s served from CDN and Redis, with clicks published asynchronously to Kafka and rolled up per minute. The main trade-off is 302 over 301: more traffic to us in exchange for accurate analytics."

Follow-up questions the interviewer may ask

  1. How do you support custom aliases without colliding with generated codes?
  2. How would you make it multi-region?
  3. How do you delete a link that's cached in CDNs and browsers?
  4. What if analytics consumers fall behind for an hour?

Model answers to each of these are in the book.