System designInterview preparation
Advanced·System design·2 min read

How would you design a URL shortener?

Practice this question

Short explanation:

A URL shortener maps a compact public code to a destination URL and redirects visitors efficiently. Creation needs unique-code handling, while redirects usually dominate the read workload. Caching can speed up redirects, but expiry, abuse controls and private links influence the cache and access rules.

Key points:

  • Store a unique short code mapped to a validated destination.

  • Redirect reads use a cache; keep a database uniqueness constraint for codes.

  • Handle expiry and abuse; generate analytics outside the redirect path.

  • Use a database uniqueness constraint to resolve generated-code collisions and avoid making analytics writes part of the critical redirect path.

  • Choose permanent versus temporary redirects based on whether destinations may change and how clients should cache them.

Interview point:

  • A random code can be hard to guess but is not a substitute for authentication when a link is private.
Adapted from 07_SYSTEM_DESIGN_ARCHITECTURE.md · “How would you design a URL shortener?”. Code examples are study fragments. Difficulty and core labels are editorial estimates; version-specific behavior should be checked against your project.