A URL shortener like bit.ly
Design a URL Shortener
Short links that never collide, redirect quickly for users anywhere, count every click, and can be switched off in seconds. You will estimate the load first, then design each part from the numbers.
Foundational, about 45 minutes, 9 stages
The situation
A marketing-tools company offers short links. A customer pastes a long URL and gets back something like sho.rt/aZ3kQ9x to put in emails, posters and social posts. The customer can see how many clicks each link got, by day and by country.
They expect about 100 million new links a month and 10 billion redirects a month. Big campaigns push traffic to about five times the average. People click from all over the world, but the servers are in one US region today. Occasionally a link turns out to point at malware, and the trust-and-safety team needs it switched off immediately.
What it has to do
Functional
- Create a short link for a long URL, optionally with a custom alias.
- Redirect short links to their destination.
- Show click counts per link, by day and country.
- Disable a link immediately when it is reported as malicious.
Non-functional
- Redirects are fast for users worldwide (under 100 ms where possible).
- Two links never share a code, and a code never silently changes destination.
- Codes can't be enumerated: knowing one link doesn't reveal others.
- Problems in analytics never slow down or break redirects.
Constraints and assumptions
- About 100 million new links a month and 10 billion redirects a month, with peaks around 5× average.
- Links never expire unless the customer deletes them.
- One region today, Postgres as the primary database, a CDN available.
- Clicks are very uneven: a small fraction of links gets most of the clicks.
- Customers may change a link's destination after creating it.
- Click counts may lag by a minute or two.
Interview questions it prepares you for
- “Design a URL shortener like bit.ly.”
- “Design Pastebin.”
- “How would you generate unique short IDs across many servers?”
- “Your cache is serving a deleted item. How do you make deletions take effect immediately?”
Read and practise next
How Dub built it · Redirects at the edge, in their engineers' own words
Concepts to know first: Caching, Generating unique identifiers.
Similar systems: Design an API Rate Limiter, Design a Video Processing Pipeline.