How to Design a URL Shortener in a System Design Interview
A practical URL shortener walkthrough covering scope, key generation, storage, redirects, caching, abuse, and analytics trade-offs.
Clarify the product first
A URL shortener is simple only if you ignore the details. Clarify whether links are public or private, whether custom aliases are supported, whether links expire, whether analytics are required, and what abuse controls matter.
For a first version, a good scope is create short link, redirect short link, optional expiration, basic click analytics, and abuse prevention for obvious spam or malware patterns.
Key generation is the core trade-off
The short code can come from an auto-incrementing ID encoded in base62, a random token with collision checks, or a dedicated key generation service that pre-allocates ranges. Each choice has a trade-off.
- Sequential IDs are simple and compact but can leak volume and enable enumeration if links are meant to be private.
- Random IDs are harder to enumerate but need enough entropy and collision handling.
- Pre-generated keys decouple creation from the main database but add operational complexity.
Serve redirects on the hot path
The redirect path is read-heavy and latency-sensitive. A typical design uses an edge or application tier that looks up short code metadata from a cache, falls back to the primary store, validates expiration and safety status, then returns a 301 or 302 redirect.
Cache invalidation matters if links can be deleted, updated, or flagged for abuse. State the acceptable stale window and how moderation changes purge the cache.
Keep analytics off the redirect path
Click analytics should not slow down redirects. Emit an event asynchronously and aggregate it later. If the analytics pipeline is down, users should still be redirected unless the product requires strict click accounting.
Good follow-up topics include hot links, bot traffic, abuse detection, regional routing, and privacy for private or unlisted links.