FotoFlow wants short share links: could you design one today?
FotoFlow wants short, pretty share links: paste a long photo URL, get fflow.link/x7Kp2Qa, and anyone who opens it lands on the photo. This is the first part where everything in the series gets used at once, so the method matters more than the answer: requirements, numbers, simplest design, find the bottleneck, fix it, repeat.
Requirements, then numbers
The service must create links and redirect; it should expire them and count clicks; and the quality that matters most is that redirects are fast and almost never fail, since a dead shortener breaks every link ever shared. Now the napkin math. Say 100 million new links a month, about 40 writes a second, and each link opened roughly 100 times: 4,000 redirects a second, peaking near 12,000. Kept five years that is 6 billion links, about 3 TB at 500 bytes each. The numbers choose the design. A hundred reads per write says cache; 3 TB of one-key lookups says the data will spread across machines.
The simplest version is two endpoints, POST /links and GET /x7Kp2Qa, one table, stateless servers behind a load balancer. Start with the simplest design that meets the brief, then ask what breaks first.
Where codes come from
First idea: hash the URL and keep seven characters. No coordination needed, but seven characters cannot hold a hash, and by the birthday problem the first collision arrives after only about two million links, plus two users shortening the same URL get the same code and cannot have separate expiry or stats. Better: give each link a unique number and write it in base 62, digits plus upper and lower case letters. Seven characters cover 3.5 trillion codes, and each server reserves a block of a million ids from a small counter service, exactly the id patterns from earlier in the series.
Sequential codes have their own leak: tn1e, tn1f, tn1g invites anyone to walk the whole space and harvest "private" links, so scramble each id with a reversible keyed permutation before encoding. Unguessable codes are not security: private links need real access control.
Reads, storage, counts
The read path is where the 100-to-1 ratio pays: links are immutable and a small share of them takes most of the clicks, the ideal cache case. An in-memory cache answers about 90 percent of redirects in a millisecond, including negative caching for the random codes bots probe all day. The redirects use 302 rather than 301 so every click comes back and can be counted or redirected differently later. Storage: no joins, no range scans, just "get this code," which is exactly a key-value store like DynamoDB or Cassandra, partitioned by a hash of the code so load spreads evenly and three copies live in different zones.
Click counting stays off the critical path: instead of UPDATE links SET clicks = clicks + 1 turning 4,000 cheap reads into 4,000 writes on the hottest rows, the API drops a tiny click event on a stream and a consumer adds them up every few seconds. The redirect is the product; the count is a by-product.
What this does not solve
A short link hides where it goes, which is ideal for phishing: a script creates 5,000 links a minute pointing at fake bank pages, the domain lands on blocklists, and every customer's links stop working. The defense is layered: rate-limit creation per account and IP, check destinations against phishing and malware lists when created and again later, and expire abandoned links. For most shorteners the first serious incident is not load, it is abuse.
Where did the numbers change the design in this walkthrough, and which decision would a different ratio flip?
You inherited a shortener whose codes count upward. What exactly can a stranger read today?
One full design is done. Next: the home feed, where the hard part is not storing posts but deciding when to do the work. Next in the series: Five million home feeds: how do you build the page everyone opens?
Sources
Base62 on Wikipedia
System Design Primer
Snowflake ID on Wikipedia
Recommended for you
- Craft & Skills
One server, 10 million users: how does a photo app survive that?
A 40-post series that follows FotoFlow, a photo app, from one server and 100 users to 10 million users with direct messages. One problem, one fix, one honest cost at a time.
- Craft & Skills
Ten million users want direct messages: could you design the chat system?
The final design: 20 million open sockets and a promise that every message arrives, in order, exactly once. Dumb gateways, a connection registry, store-first durability and sync by cursor.
- Craft & Skills
Five million home feeds: how do you build the page everyone opens?
The home feed must load in 200 ms from two hundred followed accounts. Fan-out on read melts at 4.6 million lookups a second; fan-out on write melts on celebrities; the hybrid survives both.
- Craft & Skills
Draw the system twice: why one sketch is never enough?
Two engineers draw FotoFlow's architecture: one produces classes, the other boxes, and both miss the real problems. The map and the room are two different documents, joined by contracts.
Enjoyed this article?
Subscribe for new articles. No spam. Unsubscribe anytime.
Typed live, never pasted.