GuidesDesign a URL shortener
Design a URL shortener: a system design interview walkthrough
11 min read · Updated 1 December 2026
You paste a 300-character link and get back something like tradeoffs.in/x7Kp2. Anyone who opens the short link lands on the long one.
It sounds like a weekend project, and that is why interviewers like it. The idea is simple, so all of the interview time goes on the real questions: how do you make short codes that never collide, and how do you answer billions of redirects quickly when almost every request is a read?
This guide walks through the answer in the order you would give it in an interview.
Key takeaways
- Redirects outnumber new links about 100 to 1, so design the read path first.
- Seven base-62 characters give 3.5 trillion codes, enough for decades.
- A counter or pre-made ID ranges avoid collisions without checking the database on every write.
- A cache in front of the database answers almost every redirect from memory.
- Count clicks asynchronously so analytics can never slow a redirect down.
Step 1: Pin down what you are building
Start by agreeing on scope. A good first minute sounds like this:
- Must have: create a short link for a long URL, and redirect anyone who opens it.
- Nice to have: custom aliases (
/launch-day), expiry dates, click counts. - Out of scope: user accounts, editing links after creation, link previews.
Then the qualities that drive the design: redirects must be fast (under about 50 ms on our side) and always available, because a broken short link is a broken link printed on a poster. Creating links can be a little slower.
Step 2: Estimate the load
Assume 100 million new links a month and a 100:1 read-to-write ratio.
- Writes: 100 million ÷ 30 days ÷ 86,400 seconds ≈ 40 new links a second.
- Reads: 100 × 40 = 4,000 redirects a second on average; plan for 5× at peak, so 20,000 a second.
- Storage: each record is roughly 500 bytes (long URL, code, timestamps). 100 million × 12 months × 5 years × 500 bytes ≈ 3 TB over five years.
What the numbers decide: writes are tiny, storage fits on a few machines, and the hard part is serving 20,000 reads a second with low latency. So the design is built around the read path.
| Quantity | Value | What it means |
|---|---|---|
| New links | ~40 / s | One database can take every write |
| Redirects at peak | ~20,000 / s | Needs a cache and several app servers |
| Storage (5 years) | ~3 TB | Fits a small sharded or managed database |
| Codes needed (5 years) | ~6 billion | 7 base-62 characters is plenty |
Step 3: The API
Two calls are enough:
POST /api/links{ "url": "https://example.com/a/very/long/path", "alias": "launch-day" }-> 201 { "code": "x7Kp2", "short": "https://tradeoffs.in/x7Kp2" }GET /x7Kp2-> 302 Location: https://example.com/a/very/long/pathSay why you chose the redirect status. 301 (permanent) lets browsers cache the redirect, which saves load but means you never see repeat clicks. 302 (temporary) sends every click through you, which you need if click counts matter. Most shorteners use 302 for that reason.
Step 4: Making short codes that never collide
This is the heart of the question. Seven characters from a-z, A-Z and 0-9 give 62⁷ ≈ 3.5 trillion codes, far more than the 6 billion we need. The question is how to pick one without two links getting the same code.
Option 1: hash the long URL (for example the first 7 characters of an MD5). Simple, but two different URLs can produce the same 7 characters, so every write needs a "does this code exist?" check and a retry.
Option 2: a counter turned into base 62. Link number 125 becomes cb (2 × 62 + 1), and even link 10 billion needs only six characters. Codes are unique by construction, so there is nothing to check. The catch is that one counter is a single point of failure and makes codes guessable.
Option 3: hand out ID ranges. A small coordination service gives each app server a block of a million IDs. The server spends them from memory and asks for a new block when it runs out. No collisions, no database round trip per write, and no single counter on the hot path.
A strong answer picks option 3 and mentions the cost: if a server crashes, the rest of its block is never used. With trillions of codes available, that waste does not matter. If guessable codes are a concern, shuffle the ID with a reversible bit mix before encoding it.
Step 5: The high-level design
Follow one redirect through the system: the request reaches a load balancer, then one of several stateless app servers. The server looks the code up in a cache. On a hit it answers straight away; on a miss it reads the database, fills the cache, and answers.
Creating a link goes the other way: the app server takes the next ID from its block, encodes it, writes the record to the database, and returns the short URL.
Step through the diagram to see both paths, then what happens when the cache fails.
Create a link
The app server takes the next ID from its block, encodes it in base 62 and stores code → URL. No collision check is needed.
Step 6: Storage and caching
The data is one simple lookup: code → long URL. There are no joins and no transactions across rows, so a key-value or wide-column store (DynamoDB, Cassandra) fits well and shards cleanly by code. A relational database also works at this size; say that both are fine and why you lean one way.
For the cache, link popularity follows a steep curve: a small share of links gets most of the clicks. A cache holding the hottest 20% of codes is about 20% of a day's lookups, which fits in a few gigabytes of memory and serves well over 90% of redirects. Evict with LRU (least recently used) so cold links fall out on their own.
Short links rarely change, so the cache almost never goes stale. If you support expiry or deletion, delete the cache entry when the record changes.
Step 7: What can go wrong
- A link goes viral. One code gets 50,000 hits a second. The cache absorbs it; a CDN in front of the redirect can absorb even more.
- The cache fails. All redirects fall through to the database. Run replicas of the cache and keep database read replicas warm so the fallback holds.
- Abuse. People will shorten phishing links. Check new URLs against a blocklist and rate-limit link creation per client.
- An ID server crashes mid-block. Some IDs are skipped. Nothing breaks: codes are still unique.
Trade-offs to say out loud
- 302 vs 301: you pay for every redirect to get accurate click counts.
- ID ranges vs hashing: no collision checks, at the cost of a small coordination service and some unused IDs.
- Key-value store vs SQL: easy sharding and steady latency, at the cost of flexible queries you do not need here.
- Async click counting: counts can lag by seconds, which is fine for analytics and keeps redirects fast.
Questions people ask
How long should the short code be?
Work it out from the number of links you expect. Seven base-62 characters give about 3.5 trillion codes, which covers billions of links with plenty of room. Six characters (about 57 billion) is also enough for most estimates.
Should a URL shortener use SQL or NoSQL?
Either works. The data is a single code-to-URL lookup with no joins, which suits a key-value store and makes sharding easy. A relational database is fine at moderate scale; what matters in the interview is explaining why.
Should the same long URL always get the same short code?
Not necessarily. Reusing codes saves a little space but needs an extra index on the long URL and breaks per-user analytics. Most designs create a new code each time unless the product asks otherwise.
Why 302 and not 301?
A 301 is cached by browsers, so repeat visits never reach your servers and you cannot count them. A 302 keeps every click visible, which is what analytics need.