System design interview: the URL shortener, simulated live
The classic interview design with its numbers simulated over a real day of traffic: what breaks first, at which load, and what it costs.
Short answer: a URL shortener is a read-heavy key-value lookup. Put a CDN in front of the redirects, a small stateless API behind it, a cache for hot links and one database for all links.
When we simulated the design at 3× traffic, the API tier broke first, not the database. Raising the CDN hit ratio from 0.6 to 0.8 fixed it for free; two more API servers also fixed it. At this scale the CDN is most of the monthly bill.
1. Requirements and estimates
- Functional: create a short link for a long URL; redirect a short link to the long URL. Optional: custom aliases, expiry, click counts.
- Non-functional: redirects fast (a few ms at the server) and highly available; links never lost.
- Traffic: assume 3,000 requests/s on average, about 1.7× that at the evening peak, and 2 % of requests create links. That is 60 new links/s, about 5.2 million a day and 1.9 billion a year.
- Keys: 7 characters of base62 give 62⁷ ≈ 3.5 trillion codes, enough for more than 1,800 years at that rate.
- Storage: at about 500 bytes per link (URL, code, timestamps, owner), 1.9 billion links are roughly 950 GB a year. One database handles that; plan partitioning by code for later.
2. The design
Users → CDN → Load balancer → Redirect API (3 servers) → Redis (hot links) → Postgres (all links)
└──── creates (2 %) ────────────────→ Postgres
- CDN caches redirect responses at the edge. With a short TTL it answers 60 % of the redirects in our design; the rest reach the API.
- Redirect API: stateless, 3 ×
c7i.xlarge(4 vCPU) on AWS, about 2 ms of CPU per redirect. - Redis answers 95 % of the lookups that reach the API: link popularity is very skewed.
- Postgres (
db.r7g.large) stores every link, serves the cache misses and takes the writes. - Short codes: a counter encoded in base62, with each API server reserving ranges of the counter so they never collide. Hashing the URL also works but needs a collision check; random codes need one too.
3. Run it: what breaks first?
We built this design as a template in Stackrig and moved the traffic slider. These are Stackrig model results for that design. The traffic follows a measured day: its evening peak is about 1.7× the daily average, the shape of eight weeks of Wikimedia pageviews (public data, CC0).
| Scenario | API load at the peak | Errors at the peak |
|---|---|---|
| 1× traffic | 37 % | 0 % |
| 3× traffic | 127 % | 8.5 % |
| 3×, CDN hit ratio 0.6 → 0.8 | 59 % | 0 % |
| 3×, API servers 3 → 5 | 73 % | 0 % |
The database is not the bottleneck: Redis and the CDN shield it. The API tier is, because every CDN miss is a request it must serve. With three servers it runs out at about 2.4× traffic: that is where the evening peak reaches what they can serve. “Where does it break?” in the playground reports the same point as “breaks at 2.41× the peak traffic”. There are two fixes. More servers cost money every month. A higher CDN hit ratio, from a longer TTL on redirects, costs nothing and cuts the API's traffic in half (40 % misses become 20 %).
4. The part most answers skip: cost
At 1× the design costs about $11,500 a month on average over the day in Stackrig's AWS price tables, and about $10,100 of that is the CDN: billions of redirect requests a month at a per-request price. The servers are cheap by comparison: the load balancer, the API servers, Postgres and Redis together come to about $1,400. That changes the trade-offs you can discuss:
- 301 or 302? A 301 (permanent) can be cached by browsers and the CDN, so repeat clicks cost you nothing, but you stop seeing them. A 302 (temporary) reaches you every time and lets you count clicks, at the price of more CDN and API traffic. Say which one you pick and why.
- Analytics: if click counts matter, log them asynchronously (a queue and a worker), never in the redirect path.
- Cache TTL: links rarely change, so long TTLs are safe, except for deleted or expired links, which need an invalidation path.
5. Interview checklist
- Clarify the read/write ratio and the peak, then estimate requests/s, storage and key space.
- Draw the read path first (it is 98 % of the traffic), then the write path.
- Name the first bottleneck and what you would watch (API CPU at the peak, cache hit ratios).
- Give two fixes with their cost, and pick the cheaper one.
- Close with failure cases: cache flush (every read hits the database), CDN outage, a hot link.
Why simulate an interview design?
Reading about a design teaches you the boxes. Running it shows you which box breaks at 3× traffic, what a cache flush does to the database and what the fix costs, and you remember that. Stackrig has the URL shortener and eleven other designs as templates, each with one lesson you can reproduce in a minute. Its numbers are model results; how they compare with an exact simulation and real machines is published on How accurate is Stackrig?
Open “URL shortener” in the playground
Or make it a challenge: “Serve 10k requests/s under $6,000/month” → You get a design, a goal and a budget, and the playground checks your solution live. Your progress stays in your browser.
The app is free to try in your browser, without an account. To hear when Pro is ready, join the waitlist.