Design a URL shortener
medium 45 minTurn a long URL into a short code and redirect anyone who opens it.
Video lesson · Chapter 1 of 6
Create one durable mapping
URL shortener in 90 seconds
1:25Reel coming soon

Theory
Read or listen first. About 1 min.
What it is
A URL shortener is a service that stores a long web address under a short code. When someone opens the short address, the service looks up the code and tells the browser where to go.
It is a dictionary on the internet: code in, address out. The dictionary is easy. Making it work for billions of entries, for people all over the world, without ever opening the wrong page, is the design problem.
Why it exists
Long links break in emails, waste space in messages and are hard to type from paper. A short link fits on a poster or in an SMS.
Short links also sit in the middle of every click. That makes them useful for counting visits, changing a destination after printing, and stopping a harmful link. It also makes the service a promise: if it disappears, every link it issued stops working.
A short history
1996
HTTP/1.0 (RFC 1945) describes the 301 and 302 redirects that shorteners rely on.
2002
TinyURL launches and makes the idea popular.
2006 to 2010
Short messages with a length limit make short links a habit. Bitly starts in 2008 and adds click statistics.
2010
Twitter begins wrapping links with its own t.co domain.
2018 to 2019
Google closes goo.gl to new links, a reminder that issued links outlive product plans.
Words to know
- Short code
- The few characters after the domain, such as
3fZk9Qa. - Redirect
- A reply that tells the browser to open another address.
- Base62
- Writing a number with 62 symbols: digits, lower and upper case letters.
- Read to write ratio
- How many times data is read for each time it is written. Here about 100 to 1.
- Hot key
- One item that gets far more traffic than the others.
- Conditional write
- Store only if nothing is there yet.
Solve it like an interview
New to it? Start with Approach. Revising? Go straight to the mock.
- 1ApproachWhat this question tests, where it gets hard, and how to spend the time.
- 2ScopeAsk what changes the design, then commit to a short list.
- 3NumbersWork out a number when the answer changes what you build.
- 4API and dataThe contract first, then the data that serves it.
- 5DesignStart with the smallest system that works. Change it only when something breaks.
- 6Deep divesCompare the options before you open them, then say which you would pick.
- 7Explain your choicesAnswer first. Then compare your reasoning.
- 8By levelThe same question is judged differently for mid, senior and staff.
- 9MockAnswer against the clock, then score yourself.
How it works
Step through the idea, or press Play.
Shorten. POST a long URL, get back a short code like `sho.rt/3fZk9Qa`
Understand it

Scope
What are we building?
A URL shortener converts long, unwieldy URLs into short, memorable links. Opening the short link redirects to the original. It is one of the most asked design questions because it looks simple and has clear trade-offs.
Shorten
Long to short: a 108-character product URL becomes
sho.rt/3fZk9Qa.Redirect
A 301 or 302 sends the browser on, in under 100 ms.
Analytics
Clicks by time, country, referrer and device.
Similar applications
| Service | Known for |
|---|---|
| Bitly | Most popular, enterprise features |
| TinyURL | The original pioneer, simple interface |
| Rebrandly | Custom branded domains |
| Short.io | API-first |
| t.co | X (Twitter) link wrapping |
| goo.gl | Google, closed to new links in 2018-2019 |
The finished system
Where the design ends up
This is the last version, not the first. The Design tab starts with one service and one database, and shows which number forces each extra box.
The whole system
Summary
The whole topic in one minute.
A URL shortener turns a long address into a short code and back. The data is simple, so the interest is in three places: making unique codes on many servers, answering far more reads than writes, and counting clicks without slowing them.
Our design grows from one service and one database. Caches, a partitioned store, leased ID ranges, the edge and an event stream are added one at a time, each because a number or a failure asked for it.
- Links are read about 100 times more often than they are created, so the read path gets the attention.
- Hand out numbers in ranges and the codes cannot collide; scramble them so they cannot be walked.
- Each cache tier exists to shrink what reaches the next one.
- Counting clicks is a separate, slower system that must never delay a redirect.
- A code that was issued must keep working and must never point somewhere new.