The most classic system design interview question is short: "Design bit.ly." It looks easy, just a two-column table. But behind it sit estimates, a database choice, codes that can collide, and redirects that have to be fast. We'll work through it step by step, the way you would in an interview.
A URL shortener is a service that turns a long URL into a short code, then redirects anyone who opens that code to the original URL. It's like the bag check at a mall. You hand over a big bag and get a small card that says "A-17". Whoever shows that card, the attendant just looks at the locker list and hands over the right bag. The short code is the card, the locker list is the code → URL table.
Step 1: requirements
The question: TicketTune opens a short-link service, tn.test, for concert promoters, so a link like https://tickettune.test/concerts/42-archipelago-concert?utm_source=whatsapp&utm_campaign=ticket-war can be shared as tn.test/4c92.
- Functional: create a short link from a long URL; opening a short link redirects to the original URL; count clicks per link.
- Non-functional: redirects must be fast (people are waiting at the screen), always available, and links last 5 years. One more thing shapes the design: links are read far more often than they are created.
Step 2: estimates
Example-model numbers: 100 million new links a month, read 100 times for each time one is created, kept for 5 years, 500 bytes per link.
| What | Sum | Result |
|---|---|---|
| New links per second | 100,000,000 ÷ (30 × 86,400) | ≈ 38.6 |
| Redirects per second | 38.6 × 100 | ≈ 3,858 |
| Total links in 5 years | 100,000,000 × 12 × 5 | 6 billion |
| Storage | 6 billion × 500 bytes | 3 TB |
| Cache for 20% of a day's redirects | 333 million × 20% × 500 bytes | ≈ 33 GB |
The table shows it: writing is only 39 a second, light. Reading is almost 4,000 a second, so the design has to focus on fast redirects. And 33 GB fits in the memory of one Redis server.
Step 3: API and storage
POST /api/shorten { "url": "https://tickettune.test/concerts/42-..." }
-> 201 { "short_url": "https://tn.test/4c92" }
GET /4c92
-> 302 Found, Location: https://tickettune.test/concerts/42-...
The data access is simple: given a code, fetch its URL. No JOINs, no complex searches. That's a job for a key-value store such as Redis or DynamoDB: lookup by key, O(1), very fast. A relational database with a unique index on the code column works too, but for an operation this simple its overhead is higher.
Step 4: making the short code
Codes use base62: 62 symbols, namely 0-9, a-z and A-Z. With 6 characters there are 62⁶ = 56.8 billion codes, enough for 6 billion links. With 7 characters, 3.5 trillion.
Way 1: a counter + base62. Every new link gets a sequence number from the database (auto-increment), then that number is written in base62, exactly like writing an ordinary number but with 62 digits. Link number 1,000,000:
| Step | Sum | Remainder | Character |
|---|---|---|---|
| 1 | 1,000,000 ÷ 62 = 16,129 | 2 | 2 |
| 2 | 16,129 ÷ 62 = 260 | 9 | 9 |
| 3 | 260 ÷ 62 = 4 | 12 | c |
| 4 | 4 ÷ 62 = 0 | 4 | 4 |
Read the remainders from bottom to top: 4c92.
const ALPHABET = "0123456789abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ";
function base62(id) {
if (id === 0) return "0";
let code = "";
while (id > 0) {
code = ALPHABET[id % 62] + code; // the remainder becomes one character, at the front
id = Math.floor(id / 62);
}
return code;
}
console.log(base62(125)); // "21"
console.log(base62(1000000)); // "4c92"
console.log(base62(62 ** 6)); // "1000000": the first id that needs 7 characters
ALPHABET[id % 62]: the remainder of dividing by 62 (0 to 61) maps to one character. Remainder 12 becomesc.code = ... + code: the new character goes at the front, because the first remainder is the rightmost digit.Math.floor(id / 62): the quotient carries on to the next round, until nothing is left.
A sequence number is guaranteed unique, so there is never a collision. The downside: codes are sequential, so people can guess other links (4c93, 4c94) and count how many links you have. If that matters, scramble the number with a fixed one-to-one formula before encoding it.
Way 2: hash or random. Hash the URL (with MD5, say) and take the first 6 characters in base62, or just make 6 random characters. They can't be guessed, but two different links can get the same code: a collision. The chance follows the birthday problem, roughly 1 − e^(−n² ÷ (2 × 56.8 billion)) for n links:
| Links | Chance of at least one collision |
|---|---|
| 10,000 | 0.09% |
| 100,000 | 8.4% |
| 1,000,000 | 99.98% (almost 9 collisions on average) |
At 100 thousand links the chance of a collision is already 8.4%, long before the 56.8-billion space feels full. So the hash or random way must check the database first and try again if the code is taken.
Step 5: redirect with 301 or 302
- 301 Moved Permanently: the browser remembers the redirect. Later clicks go straight to the target URL without passing through the
tn.testserver. Fast, but those clicks aren't counted, and the link's target is hard to change. - 302 Found (temporary): the browser doesn't keep it, so every click goes through the server. A little slower, but every click can be counted and the target can change at any time.
Since the requirements include counting clicks, pick 302.
Step 6: far more reads than writes
At 3,858 redirects a second, don't go to the database for every click. Put Redis in front of it (cache-aside): look the code up in the cache; if it isn't there (a miss), fetch it from the database and store it in the cache. A link that's going viral, such as the Archipelago Concert ticket-war link, almost always hits the cache. A small share of links gets most of the clicks, so a 20% cache (33 GB) catches most redirects.
Step 7: analytics without slowing the redirect
Don't increment click_count in the database inside the redirect request: 3,858 updates a second to the same row turn that row into a fight. Send a click event to a message queue, return the 302 right away, and let a worker add them up every minute. Click counts arrive a few seconds late; redirects stay fast.
⚠️ Codes that collide, clicks that go uncounted
- Truncating a hash without checking for collisions. With 6 characters, a collision is almost certain by 1 million links. Always check and retry.
- 301 when you need analytics. The second and later clicks are served from the browser's cache and never reach the server.
- A guessable counter. Sequential codes leak how many links you have and make other links easy to try.
- Counting clicks synchronously. A database update on every redirect slows down the path that's used most.
TL;DR: A URL shortener stores code → URL pairs and redirects. Work it like an interview: requirements (create links, redirect, count clicks; fast and read-heavy), estimates (100 million links a month = 38.6 writes a second, 3,858 redirects a second, 6 billion links and 3 TB in 5 years, a 33 GB cache), then the design. The mapping suits a key-value store (O(1) lookup). Codes use base62 (0-9a-zA-Z): 6 characters = 56.8 billion codes. A counter + base62 never collides (link 1,000,000 = 4c92) but can be guessed; hash or random can't be guessed but collisions come fast (8.4% at 100 thousand links), so you must check and retry. Use 302 so every click goes through the server and is counted; 301 is cached by the browser. Put a Redis cache in front of the database for the read load, and record clicks through a queue so redirects stay fast.
⚡ Read far more often than created
Meaning: The estimates: 38.6 new links a second, but 3,858 redirects a second. So the design focuses on fast redirects, with a 33 GB Redis cache in front of the database.
⚡ A key-value store
Meaning: The access is simple: given a code, fetch its URL. No JOINs, no complex searches. That's a job for a key-value store such as Redis or DynamoDB: lookup by key, O(1), very fast.
⚡ Counter + base62
Write:
const ALPHABET = "0123456789abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ";
function base62(id) {
if (id === 0) return "0";
let code = "";
while (id > 0) {
code = ALPHABET[id % 62] + code; // the remainder becomes one character, at the front
id = Math.floor(id / 62);
}
return code;
}
Guess first: what code does link number 1,000,000 get?
4c92. The remainders are 2, 9, 12, 4, read from bottom to top.
Meaning: A sequence number is guaranteed unique, so there's never a collision. The downside: codes can be guessed. After 4c92 there's surely 4c93.
⚡ 302 so clicks get counted
Meaning: A 301 is remembered by the browser, so later clicks skip the server and go uncounted. A 302 sends every click through the server. Since the requirements include counting clicks, pick 302.