Courses System design · Part 6 · Case study 1
Case Study: A URL Shortener
We want a service that takes a long link and returns a short one, like dtf.ir/k3X9pQ. Anyone who opens the short link is sent to the original. The question looks simple, but almost every idea of this course fits into it.
This lesson follows the seven-step method.
1. Requirements
Functional:
- Make a short link for a long link.
- Open a short link and arrive at the original.
- (Optional) a custom name such as
dtf.ir/yalda, and an expiry date.
Non-functional:
- The redirect must be very fast: under 50 milliseconds.
- A short link must never go to the wrong place.
- The service must almost always be available; links already shared in messages must not break.
Out of scope: detailed click statistics and user accounts, for now.
2. Numbers
Assumptions: 1 million new links a day, and each link is opened 100 times on average.
- Creating: 1,000,000 ÷ 86,400 ≈ 12 a second.
- Opening: 12 × 100 ≈ 1,200 a second, perhaps 5,000 in the busy hour.
- Storage: each record (code, long URL, dates) is about 500 bytes. 1 million × 500 bytes = 500 MB a day, about 180 GB a year.
The result: the system is read-heavy (100 reads per write), so a cache plays the main role. The storage fits on one database for the first years.
3. API
POST /links {url, alias?, expiresAt?} → {short: "k3X9pQ"}
GET /{code} → 301 or 302 to the original URL
A small but important decision: browsers cache a 301 (moved permanently) and next time do not come to our server at all: faster and cheaper. A 302 (temporary) comes to us every time: slower, but we can count clicks or change the target. If statistics do not matter, use 301.
4. Data model
One table is enough:
| Column | Meaning |
|---|---|
code | The short code, primary key (unique) |
url | The long URL |
created_at, expires_at | Dates |
The most frequent query is “the URL for code X”, and code is the primary key, so the index is already there.
5. High-level picture
- Requests reach a load balancer and are shared between several app servers.
- For a redirect, the server first looks in a cache (for example Redis). Popular links are almost always there.
- If not, it reads from the database and puts the result in the cache.
- To create a link, the server makes a code and writes it to the database.
- A rate limiter stops bots from creating links in bulk.
6. Deep dive: how do we make the short code?
The code uses a-z, A-Z and 0-9: 62 symbols. With 6 symbols there are 62⁶ ≈ 56 billion codes. At 1 million a day, that lasts more than 100 years.
Three ways to choose a code:
a) Hash the long URL. For example the first 6 symbols of a hash. Problem: two different URLs can give the same code (a collision), and you must detect it and try again.
b) A random code. 6 random symbols. With billions of links, collisions become likely; detect them with a unique constraint on code and try again.
c) A counter, written in base 62. Each link gets a unique number, and the number is written in base 62. 125 million becomes a 5-symbol code. There are no collisions. The question is where the unique number comes from: one central counter (a bottleneck and a single point of failure), or the methods from the unique IDs lab. A simple, common way: each app server takes a range of 1,000 numbers from the counter at once and hands them out itself.
One note: codes in sequence are easy to guess (k3X9pQ, k3X9pR…). If links are private, scramble the number with a reversible function before encoding it, so the order does not show.
The cache for redirects
At 1,200 redirects a second, assume 20% of links get 80% of the clicks. One day of links is 500 MB; the hottest 20% of the last few days fit in a few gigabytes of memory. With this cache, most redirects never reach the database.
7. Trade-offs
- 301 versus 302: speed and lower cost, or click counting and the option to change the target.
- Counter versus random: no collisions but needs coordination, or no coordination but needs collision checks.
- Abuse: shorteners are used to hide phishing links. Rate limits, checking URLs against block lists, and a way to report links are needed.
Labs used
Cache · Database index · Unique IDs · Rate limiter · Load balancer