System design interview: the URL shortener, simulated live

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 4× 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

2. The design

Users → CDN → Load balancer → Redirect API (3 servers) → Redis (hot links) → Postgres (all links)
                                        └──── creates (2 %) ────────────────→ Postgres

3. Run it: what breaks first?

We built this design as a template in Stackrig and turned the traffic dial. These are Stackrig model results for that design, with a traffic curve that peaks at 2× the average:

Stackrig model results for the URL shortener template (live model, AWS Frankfurt prices). Load = CPU utilization of the API tier at the daily peak.
ScenarioAPI load at the peakErrors at the peak
1× traffic28 %0 %
4× traffic130 %9 %
4×, CDN hit ratio 0.6 → 0.861 %0 %
4×, API servers 3 → 575 %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. 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 $10,400 a month in Stackrig's AWS price tables, and about $9,100 of that is the CDN: billions of redirect requests a month at a per-request price. The servers are cheap by comparison. That changes the trade-offs you can discuss:

5. Interview checklist

  1. Clarify the read/write ratio and the peak, then estimate requests/s, storage and key space.
  2. Draw the read path first (it is 98 % of the traffic), then the write path.
  3. Name the first bottleneck and what you would watch (API CPU at the peak, cache hit ratios).
  4. Give two fixes with their cost, and pick the cheaper one.
  5. 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 4× traffic, what a cache flush does to the database and what the fix costs, and you remember that. Stackrig has the URL shortener and ten 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 Watch the live demo Get early access

More