Repaso
Español
System DesignIntermediateSD-23

Design a URL shortener (like bit.ly).

Answer

  • Requirements: shorten, redirect, optional custom alias and expiry; read-heavy (e.g. 100 reads per write).
  • API: POST /urls {long_url} → short code; GET /{code} → 301/302 redirect (302 if you want click analytics).
  • Code generation: base62 of a unique ID (DB sequence or Snowflake-style IDs); 7 characters ≈ 3.5 trillion codes. Or hash + collision check.
  • Storage: key-value table code → long_url, created_at, expires_at.
  • Cache popular codes in Redis; redirects at the edge/CDN.
  • Analytics as async events to a queue.
  • Abuse: rate limits, malicious URL scanning.

Key terms

Practice this question in Repaso