Design a URL Shortening Service
Design a deliberately small URL-shortening core, then evolve it into a highly available service whose fixed logical shards can move safely as physical storage capacity grows.
Problem statement
Design a URL-shortening service in two explicit phases. Phase 1 is deliberately small: one process serves at most 100,000 active mappings in memory. create(url) accepts one valid HTTP(S) URL, normalizes it, and returns the same short code for repeat calls with the same normalized URL.
A deterministic hash chooses an initial slot in a fixed-capacity table; collisions probe subsequent slots until the same normalized URL or an empty slot is found. resolve(code) returns the destination or not found, and a full table must fail without overwriting a mapping.
Redis, databases, durability, replication, authentication, custom aliases, expiration, deletion, and analytics are outside this first phase.
Before selecting production components, derive average creation traffic, annual mapping storage, redirect traffic and bandwidth, cache footprint, and alias-space headroom from the stated workload; state any additional record-size or peak-factor assumptions.
Then evolve the design for production: authenticated clients may create generated or custom aliases, disable them or set expiration, readers receive redirects, and click analytics are recorded asynchronously.
Generated aliases use a case-sensitive URL-safe alphabet and are never reused; custom-alias conflicts return HTTP 409, expired or disabled aliases return HTTP 410, and not found remains distinct.
Production creates need a caller-supplied retry identity or equivalent safe-retry contract, and management writes compare a monotonic link version. The production service must tolerate a single-zone failure.
Model 256 fixed logical shards separately from physical storage machines, then expand from four to six machines without changing those logical-shard identities. Redirects remain available throughout the 24-hour expansion.
Only during final cutover, create and management calls may return an explicit retryable response for at most five seconds; no acknowledged write may be lost. Operators may pause, resume, or roll back before old ownership is retired.
Explain what changes between phases, how final aliases map to logical shards, and how routing and data migrate without losing redirects or admitting conflicting owners. Malicious-link scanning and rich analytics reporting remain outside scope, but discuss alias predictability and enumeration risk.
What to build
- In the small-scale phase, return one stable short code for each normalized destination without overwriting a mapping on collision or capacity exhaustion.
- Resolve an active short code and return its destination, or a distinct non-redirect outcome when it is unknown, expired, or disabled.
- In production, let an authenticated owner create a generated or custom alias, disable it or set expiration without reuse, and record click analytics asynchronously.
- Keep redirects available while physical storage expands, with only an explicit retryable create or management response during a final cutover window of at most five seconds.
At scale
- For the small-scale phase, assume one process holds at most 100,000 active mappings in a fixed-capacity in-memory table; persistence and distributed components are outside that phase.
- For the production phase, assume 100 million new links per month and 100,000 redirect requests per second at peak, with p99 redirect latency below 100 milliseconds from the nearest serving region.
- For the production phase, assume generated aliases use a case-sensitive URL-safe alphabet, aliases are never reused, link metadata survives a single-zone failure, and click analytics may lag by one minute.
- For the production expansion, assume 256 fixed logical shards move from four to six physical storage machines within 24 hours; redirects stay online, create or management writes may be retried for at most five seconds at final cutover, and operators may pause, resume, or roll back before old ownership is retired.
- Before choosing the production architecture, derive average and assumed peak create rates, one-year mapping storage, redirect bandwidth, a bounded hot-cache footprint, and alias-space headroom from the stated workload; state any additional size or peak assumptions.