“Design a URL shortener” (TinyURL, bit.ly) is often the first system design question people get. It looks simple, which is the trap: the interviewer wants to see how you scope it, size it and make key generation safe at scale. Pastebin is the same design with text instead of links.
Functional
https://short.example/aZ3k9Qx).Non-functional
For Pastebin the list is almost the same: text only, a unique URL, configurable expiry, an optional custom alias, and no data loss. (session clip at 2:16)
Pick round numbers and say them out loud. Say 100 million new links a day and 10 reads for every write.
| What | Math | Result |
|---|---|---|
| Writes per second | 100M / 86,400 | about 1,200 |
| Reads per second | 10 x writes | about 12,000 |
| Links in 5 years | 100M x 365 x 5 | about 183 billion |
| Storage (500 bytes each) | 183B x 500 B | about 90 TB |
Two things follow: the system is read-heavy, so caching matters, and we need a key space well over 183 billion. Seven base62 characters give 62^7, about 3.5 trillion keys, which is plenty. See back-of-the-envelope estimation for the method.
A good interviewer may push back on “users”: a script can be a user, and traffic can grow 10x a week, so straight-line guesses break. Size for the workload, not the head count. (session clip at 5:01)
POST /links {"long_url": "...", "alias": "optional", "expires_at": "optional"} -> {"short": "aZ3k9Qx"}
GET /{short} -> 301 or 302 redirect to the long URL
Use 302 if you want to count every click (the browser asks you each time). Use 301 if you want the fewest requests (browsers cache it).
This is the heart of the question. Three common options:
Base62 turns a number into a short string:
ALPHABET = "0123456789abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ"
def encode(n: int) -> str:
if n == 0:
return ALPHABET[0]
out = []
while n:
n, r = divmod(n, 62)
out.append(ALPHABET[r])
return "".join(reversed(out))
def decode(s: str) -> int:
n = 0
for ch in s:
n = n * 62 + ALPHABET.index(ch)
return n
(Checked on 100,000 random numbers: decode(encode(n)) == n every time.)
Avoid locks to hand out unique IDs; in a distributed system they slow everything down. Instead, give each app server its own range of IDs, or stream pre-made keys through a queue such as Kafka. (session clip at 44:58)
The data is simple: short key, long URL, created time, expiry, owner. A key-value or wide-column store (DynamoDB, Cassandra) fits well because every read is a lookup by key.
If you also need “has this long URL been shortened before?”, don’t add a secondary index. Keep a second table keyed by the long URL, one table per lookup direction. (session clip at 36:42)
Mention what you would add with more time: rate limiting on link creation (see how to design a rate limiter), a cleanup job for expired links, abuse and malware checks, and click analytics written asynchronously so redirects stay fast.
Design Pastebin: requirements, estimation and pre-generated keys
Design a URL shortener: scaling, Kafka key generation and Cassandra modeling (advanced)
SDE Skills is a volunteer-run 501(c)(3) nonprofit with free weekly system design sessions and mock interviews on Zoom. See the schedule or read how to prepare for a system design interview.