Backend system design · about 40 minutes
URL shortener
Design a service that turns a long URL into a short link and sends anyone who opens the short link to the long URL. Users can pick their own alias and see how many times a link was opened. Reads far outnumber writes: a popular link can be opened millions of times a day, and the redirect must feel instant everywhere in the world.
Ask first
- How many new links a day, and how many redirects? What is the read to write ratio?
- Can a user choose the alias? Can links expire or be deleted?
- Must the same long URL always get the same short code, or a new one each time?
- What do the statistics need: a total count, or clicks by day, country and referrer?
- Is a 301 (cached by browsers) acceptable, or must every click reach us to be counted?
The outline
Write your own outline for each part first, then open it to compare with the points a strong answer covers.
Requirements
- Functional: create a short link (optionally with a custom alias), redirect, show click counts, expire or delete a link
- Scale: say the numbers out loud, for example 1 million new links and 100 million redirects a day, about 1,200 redirects a second on average
- Latency: a redirect in tens of milliseconds at the 99th percentile; creating a link can be slower
- Availability matters most for redirects; a short outage of link creation is tolerable
- Codes are short (7 characters) and hard to guess one from another
Architecture
- A stateless API tier behind a load balancer, with two paths: POST to create, GET /{code} to redirect
- A key-value or relational store maps code to long URL; the code is the primary key, so a lookup is one indexed read
- A cache (Redis or similar) in front of the store holds hot codes; most redirects never reach the database
- Code generation: a counter encoded in base62, with ranges handed to each server so they never collide, or random codes checked for collision
- Click events go to a queue and are counted by a separate worker, so the redirect never waits for analytics
Data model
- links: code (primary key), long_url, owner_id, created_at, expires_at
- A unique index on code; custom aliases live in the same column, so a taken alias is a unique-key error
- clicks: code, time, country, referrer, as an append-only log aggregated into daily counts
- Storage estimate: about 500 bytes a link, so a billion links is about 500 GB before replication
Interface
- POST /links { url, alias?, expiresAt? } → 201 { code, shortUrl }; 409 when the alias is taken; 400 for an invalid URL
- GET /{code} → 302 with a Location header; 404 for an unknown code; 410 for an expired one
- GET /links/{code}/stats → { total, byDay: [...] } for the owner only
- Click event on the queue: { code, at, country, referrer }
Optimizations
- 302 instead of 301 when every click must be counted; 301 when browser caching matters more
- Cache with a time to live; cache the misses too, so a flood of unknown codes does not reach the database
- Replicas for reads, and a CDN or edge cache for the most popular codes
- Rate limit link creation per user and per IP, and check URLs against a list of malicious sites
- Shard by code when one database is no longer enough; the code spreads evenly by design
Pitfalls
- Counting clicks synchronously inside the redirect, so analytics slows every request
- Codes generated from a hash of the URL without handling collisions
- Sequential codes that let anyone list every link
- Not saying numbers: the design never checks whether one database is enough
Follow-ups an interviewer asks
- Two servers generate a code at the same moment. How do you know they differ?
- A link goes viral on social media. Which part fails first?
- Why 302 and not 301, or the other way round?
- How would you delete everything a user created, including the click data?
- How do you stop the service being used to hide phishing links?