How to design Bitly
The first sections are free. The deep dives, level expectations and quiz are part of Pro.
On this page
The problem
A URL shortener turns a long link into a short code and sends anyone who opens that code on to the original page. It creates very few links and serves a huge number of redirects, so the answer turns on two things: handing out codes that can never repeat, and keeping redirects fast when one link goes viral.
Clarifying questions
| Ask | Why it matters | A typical answer |
|---|---|---|
How many links do people create, and how many redirects do we serve? | The ratio decides the shape of the design. If redirects dwarf creates, the read side gets its own service and a cache; if not, one service is fine. | About 100 million daily users, a billion stored links, about 1 new link a second, and redirects that average 5,787 a second and peak near 600,000 a second when a link goes viral. |
Can people pick their own code, and can links expire? | A chosen code shares the namespace with generated ones, and expiry changes what the cache is allowed to keep. | Both are optional per link: a custom alias if the person asks for one, and an optional expiry time. |
Do the codes need to be hard to guess? | It decides between codes that come from a counter (short, but in order) and codes that look random. | Not for the first version. Say which way your scheme leans, and be ready to defend it. |
Do we need click counts or analytics? | Counting clicks means every click has to reach something you control, your service or your edge, which rules out letting browsers remember the redirect. | Dashboards are out of scope, but keep the door open: pick a redirect that still passes through your servers. |
Is it fine if a new link takes a moment to work everywhere? | It tells you whether to favor availability or strict consistency on the read path. | Yes. Redirects must stay up and fast; a brand-new link working a moment later in a far region is acceptable. |
Can a link be deleted or changed after it is made? | A link that can change or disappear limits how long any cache may hold it. | Owners can delete a link. Changing its destination is not needed. |
Non-functional requirements
| Requirement | What it forces |
|---|---|
Every short code is unique | The code column is the primary key, so the database refuses a duplicate no matter what goes wrong upstream. The generator must never hand out the same value twice, and a generated code that lands on a taken alias is skipped. |
Redirects answer in under 100 ms | A redirect does one lookup and nothing else: no joins, no calls to other services. Popular codes are answered from memory, and far-away users from a server near them. |
99.99% availability for redirects | That allows about 52 minutes of downtime a year, so no single machine may sit alone on the redirect path: the database has a standby that takes over by itself, and cached codes keep resolving while the write side is down. It also favors availability over consistency: a new link may take a moment to show up everywhere. |
Reads outnumber writes about 1000 to 1 | Split the redirect service from the write service so each scales on its own, and spend the design effort on the read path. The write path can stay simple. |
Estimates
| Quantity | Working | Result | What it sizes |
|---|---|---|---|
Codes that | 62^5 ≈ 916 million; 62^6 ≈ 56.8 billion; 62^7 ≈ 3.52 trillion | 6 characters is the floor for 1 billion links; 7 characters yield roughly 3.5 trillion, about 3,500 per stored link | The code length: seven characters, which leaves so much room that a random code almost never lands on a taken one. |
Chance a random 7-character code is already taken | 1 billion taken ÷ 3.52 trillion possible ≈ 0.03%; with 6 characters, 1 billion ÷ 56.8 billion ≈ 1.8% | About 1 insert in 3,500 needs a retry at 7 characters; about 1 in 57 at 6 | The retry loop of a hash-based code: at seven characters it almost never runs twice. |
Counter calls with batches of 1,000 | 1 write a second ÷ 1,000 values per batch = 1 call every 1,000 seconds, about 17 minutes; at Alex Xu’s 1,200 writes a second, 1.2 calls a second | Between once every 17 minutes and about once a second | The load on the counter, which decides whether one instance with a replica is enough. |
Peak against average redirects | 600,000 at peak ÷ 5,787 on average ≈ 104 | The peak is about 100 times the average | Why you design for the peak, not the average: a viral link multiplies the load on a single code. |
Memory to cache a tenth of all links | Assuming a cached entry takes about as much as a stored row, 500 bytes: 10% of 1 billion links = 100 million; 100 million × 500 bytes = 50 GB | About 50 GB for each 10% of links cached | The memory of the redirect cache. |
Downtime that 99.99% allows | 0.01% × 525,600 minutes a year ≈ 52.6 minutes; 0.01% × 43,200 minutes a month ≈ 4.3 minutes | About 52 minutes a year, about 4 minutes a month | How fast failover must be: automatic, not a person paged at night. |
Length of a code made from a full id | A 64-bit id needs 11 | 11 to 22 characters, against 7 | Why a full UUID or a 64-bit id makes a poor code: unique for practical purposes, but several characters longer than it needs to be. |
Core entities
| Entity | What it holds |
|---|---|
Short link | One row per code: where it points, when it stops working, and who made it. |
User | Whoever owns links. Accounts are out of scope, so this stays a stub the link points at. |
The API
| Endpoint | Notes |
|---|---|
| Checks the URL, takes the alias if one is given (a taken alias returns |
| One lookup by primary key. Unknown code: |
| Owner only. Deletes the row first, then removes the code from the cache and purges it at the edge. Deleting the cached copy first would let a reader load the old row and cache it again; this order closes most of that window, and short cache lifetimes cover the rest. |
The high-level design
Step 1 of 4
Two paths: create and redirect
Start with the two jobs. A write service takes POST /urls, checks the long URL and inserts a row. A redirect service answers GET /{code} with one lookup and a 302, or 410 if the link has expired. They are separate services because redirects outnumber creates about 1000 to 1: the redirect side scales on its own, and a bad deploy of the write side cannot stop redirects. One relational table holds every link: a billion rows of about 500 bytes is about 500 GB, which one database server holds.
What breaks first
The deep dives
Generating unique codes. Two links created in the same second must never share a code, and the code should stay short. How do you make codes, and how many characters do they need?
Scaling redirects. One link goes viral and redirects jump toward 600,000 a second, nearly all for a single code. What serves them, and which redirect status do you send?
Going multi-region. The service now runs in three regions. How does each region make codes without clashing with the others, and what happens when a link made a second ago in one region is opened in another?
Concepts this guide uses
Sources
- Alex Xu, System Design Interview, chapter 9: notes by Preslav Mihaylov
- Redis documentation: INCRBY
- Redis documentation: EXPIREAT
- PostgreSQL documentation: CREATE SEQUENCE
- Amazon ElastiCache documentation: Caching strategies
- MDN: Cache-Control
- Wikipedia: HTTP 301
- MDN: 302 Found
- Wikipedia: URL shortening
- Wikipedia: Format-preserving encryption