How would you design a URL shortener?
Practice this questionShort 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.