How to design a URL shortener, step by step
How to design a URL shortener like Bitly in a system design interview: the requirements, the numbers, the first design, short codes, and 301 or 302.
"Design a URL shortener" is often the first question in a system design interview. The product fits in one sentence: you paste a long web address and get back a short one, like sho.rt/k3T9xQa, that leads to it. Yet a good answer touches unique IDs, caching, HTTP details and abuse.
Have a guess before you read on. Where's the hard part? Most people say storage. It isn't. The hard parts are making the short codes, and keeping every click fast for years. We'll build the design one piece at a time, the way you would at the whiteboard.
What does a URL shortener need to do?
Start by agreeing on the features, as you would with an interviewer. A short list is enough:
- Turn a long URL into a short link, with an optional custom alias like
/summer-sale. - Send anyone who opens the short link to the long URL.
- Let the creator set an expiry date, and let us disable a link that points to malware.
- Count clicks per link, by day and country. A minute's delay is fine.
Then the qualities that shape the design. Redirects must be fast, because every click waits for them: say 99% answered within 50 ms, timed at our servers. They must almost never fail, because a short link might be printed on a poster or sitting in a million inboxes. A link must never be lost once we've handed it out. And codes shouldn't be easy to guess, so nobody can walk through every link we've made.
Notice what's left out: editing a link's destination, accounts, billing, QR codes. Saying what you're cutting, out loud, is part of a good answer.
Pax asks: If two people shorten the same long URL, should they get the same short code?
Usually not. They might want different expiry dates, and each wants their own click counts. So each request gets its own code.
How big is it? The numbers that change the design
A number is worth working out only if it changes the design. We'll use a common interview assumption: 100 million new links a month, and about 100 clicks on each link. These are round, illustrative numbers.
- Writes. 100 million a month ÷ about 2.6 million seconds in a month ≈ 40 new links a second, perhaps 100 at peak. One database handles that easily.
- Reads. A hundred clicks for every new link gives 100 × 40 = 4,000 redirects a second, rising to around 10,000 at peak. This is the number to design for.
- Storage. Each link takes about 500 bytes. Over five years that's 6 billion links and about 3 TB. Large, but one well-run database holds it for years.
- Code length. With the 62 letters and digits (a–z, A–Z, 0–9), six characters give 62⁶ ≈ 56.8 billion codes, and seven give 62⁷ ≈ 3.5 trillion.

So what's the takeaway? Reads outnumber writes 100 to 1. Almost every choice from here on protects the redirect. And six characters would count to 6 billion, so why not use six? Hold that thought. It comes back when we make the codes.
The first working design
Start with the simplest design that meets every requirement. You can improve it once it works.

A request first reaches a load balancer, which spreads requests over a few identical link servers. The servers keep nothing important in memory, so any one of them can answer any request, and you add more when traffic grows.
Creating a link is a simple write. The server picks a new code, inserts one row into the database (code, long URL, owner, created time, expiry, status) and returns the short URL. Forty writes a second is light work for PostgreSQL or MySQL. A key-value store like DynamoDB is just as good a fit, because you only ever look a link up by its code. Pick the one your team knows.
The redirect is where the traffic is. The server checks a cache such as Redis first. A hit takes about a millisecond. When the code isn't there, the server reads the database, copies the row into the cache, and then answers. A link's destination never changes, so the cache can keep rows for a long time. With most clicks landing on a small share of popular links, the cache answers the large majority of redirects, and 10,000 reads a second at peak becomes a few hundred database reads.
Clicks are counted to one side. After answering, the server puts a small click event on a queue, and a separate worker adds them up. Why not just add 1 to a counter column on every redirect? Because that turns every read into a write: 4,000 database writes a second, all fighting over the same row when one link goes viral. With the queue in between, a redirect only ever reads.
Here's the API in two lines:
POST /v1/links body: long_url, plus optional alias and expires_at → 201 Created, with the code and short URL
GET /{code} → 302 Found, Location: the long URLMake sense? Good. That design works, but it skips the question every interviewer asks next: where do the short codes come from?
How do you generate short codes for a URL shortener?
There are three common answers. Each one is a reasonable instinct, so it's worth seeing where each goes wrong.
Hash the long URL. Take an MD5 or SHA-256 of the URL and keep the first seven base62 characters. It feels neat: same URL, same code, no coordination. But seven base62 characters hold only about 42 bits, a small slice of the hash, so now and then two different URLs end up with one code. You still need a database check and a retry. Worse, one URL can only ever produce one code, and we've just decided each request gets its own.
Pick a random code. Generate seven random characters and insert the row only if that code isn't taken yet (a conditional insert). There's nothing to coordinate. The chance of a clash grows as the table fills: by year five, 6 billion of 3.5 trillion codes are used, so about 1 random code in 600 clashes and needs a retry. At 40 writes a second, that's no problem.
Use a counter. Every new link takes the next number, written in base62. It never clashes. But a single counter could become a bottleneck, and sequential codes are easy to guess: if you got …aaab, someone else probably got …aaac.
| Approach | Clashes? | Needs coordination? | Easy to guess? |
|---|---|---|---|
| Hash of the URL | Sometimes: check and retry | No | Only by someone who has the URL |
| Random code | Retry about 1 time in 600 by year five | No | No |
| Counter | Never | One shared counter | Yes, unless you shuffle |
Random codes are a perfectly good answer. Let's make the counter work too, because fixing its two problems teaches the most.

Fix the bottleneck with ranges. Each link server reserves a block of 10,000 numbers with one database statement, then hands them out from memory. With 40 new links a second over 10 servers, each server uses about 4 a second, so a block lasts it around 40 minutes. The shared counter row is touched about once every four minutes. If a server crashes, the rest of its block is simply skipped. Gaps in the codes don't matter to anyone.
Fix the guessing with a shuffle. Before writing a number in base62, scramble it with a secret key, using a small cipher that works on the seven-character range and can be undone. Each number turns into exactly one other number, so two links never collide, yet numbers that were next to each other end up far apart. Now a guessed code hits a real link only as often as the space is full: 6 billion ÷ 3.5 trillion, about 1 time in 600. You can also slow scanners down by limiting how many "not found" answers one client can collect. (Rate limiting algorithms covers how.)
And that answers the earlier question. With six characters, one guess in ten would hit a real link by year five (6 billion ÷ 56.8 billion). The seventh character costs one byte and makes guessing 60 times harder.
Custom aliases use the same table and the same conditional insert. If two people ask for summer-sale at the same moment, one wins and the other gets 409 Conflict. Reserve words like api and login, and block offensive ones.
Should a URL shortener return a 301 or a 302?
Both send the browser to the long URL. The difference is what the browser remembers.
A 301 means "moved permanently". Browsers keep it, often for a long time, so a repeat click can go straight to the destination without asking you. You lose that click from your counts. Worse, a link you disable for malware keeps working for anyone whose browser kept the old answer.
A 302 means "found, for now". The browser keeps it only if your headers allow, so every click comes back to you. You can count it, and you can stop it.

So most designs send a 302. There's one good case for a 301: Google Search reads a permanent redirect as a strong hint to list the destination, not the short link, in its results, and a temporary redirect as only a weak hint. Bitly, for example, answers with a 301, and says it can do so because it never reuses a short link. Either choice is fine in an interview if you name what it costs.
Where you've met a URL shortener
You've clicked thousands of these. Bitly is the best known, and it never reuses a short link once it's been created, which is the promise our "never lose a link" requirement makes too. X wraps every link posted on it in a t.co link. X's help pages say it checks those links against a list of potentially harmful sites and warns users who click a flagged one, and it uses click counts as a signal of how interesting a post is. That's our "disable a malware link" and "count clicks" features, at a very large scale.
Here's the whole design moving, with a CDN edge in front and the click counting below the line:

What interviewers ask next
Once the basic design is on the board, the questions get harder. Try these before you look anything up:
- A link in a TV advert gets 50,000 clicks a second, all on one code. What breaks first, and what do you change?
- You disable a malware link. How do you make sure every cache stops serving it within seconds, not a day later?
- Advertisers now pay per click. How do the counts go from "about right, a minute late" to exact?
The full guide, Design a URL shortener (like Bitly), answers each one, with the numbers to use and what to say in two minutes.
Recap
A URL shortener is a read-heavy system: about 100 redirects for every new link, so the design protects the redirect with a cache and keeps click counting off the hot path. Short codes come from random codes or shuffled counter ranges, with seven base62 characters so guessing rarely finds a real link. A 302 keeps every click coming back to you, and a 301 trades that for search signals. That's enough to pass the first half of the round.