Skip to content

Design a URL shortener in 45 minutes: a worked walk-through

Draw the System

The URL shortener is the most common warm-up system design question, and candidates still lose points on it. The problem is simple, so interviewers expect a sharp answer. This post walks through the practice version in our catalogue, "Design a URL shortener at bit.ly scale", as a 45-minute interview.

The constraints from the brief: about 100M new links a month, about 10B redirects a month, a heavy tail where a few links get over 1M clicks an hour, p99 redirect latency under 50 ms globally, links that live for years, analytics fresh within a minute, globally unique custom aliases, and malicious links that can be killed within seconds.

Minutes 0 to 5: requirements

Ask before you draw. Useful questions:

  • Who creates links, and are they anonymous or logged in?
  • Are custom aliases required? Can a link expire or be edited?
  • What do the analytics need to show, and how fresh?
  • What happens to a link flagged as malware?

Then say out loud what is in scope and what is not. The brief hides one hard requirement: reads outnumber writes by about 100 to 1, and the read path is latency-critical. Say so. It shapes every later choice.

Minutes 5 to 10: estimates and API

Do the arithmetic where the interviewer can see it.

  • 100M links a month is about 40 writes a second on average.
  • 10B redirects a month is about 4,000 a second on average. Take 10x for peaks, so plan for tens of thousands a second.
  • Over five years that is about 6B links. At roughly 500 bytes a row, that is about 3 TB. It fits in a modest sharded store.

Now use the numbers. The write rate is trivial, so writes are not the problem. Redirect latency is.

Keep the API small: POST /links with a target URL and an optional alias, returning a short code, and GET /{code} returning a redirect. Choose 301 or 302 deliberately. A 301 gets cached by browsers and cuts load, but later clicks never reach you, so your analytics lose them. A 302 keeps every click visible. Since this product sells analytics, say 302, and say why.

Minutes 10 to 25: the high-level design

Draw about ten boxes and label them:

  • clients, and a CDN or edge layer in front
  • a stateless redirect service
  • a cache of code to target, close to the redirect service
  • the primary store, keyed by code
  • a create service that generates codes
  • a click event stream feeding an analytics pipeline
  • a dashboard query service over the aggregated data

Mark the read path from client to storage, and the write path separately. Mark the click stream as async, with a dashed line. The redirect must never wait on analytics. Put the numbers on the diagram: about 4k reads a second on average, about 40 writes a second, about 3 TB.

A good ordering: draw the redirect path first, because it is the critical one. Then the create path. Then analytics last.

Minutes 25 to 40: pick one deep dive

You get one or two, and the brief names three likely ones: the redirect hot path, ID generation, and analytics ingestion. Pick the one you can defend best, and say that you are picking it. Interviewers reward that.

The redirect hot path. Almost every redirect should be a cache hit. The hot set is small compared with 6B links, because the click distribution is heavily skewed. So cache by code at the edge and in a regional cache. Then discuss the tail: a launch link getting 1M clicks an hour is about 300 requests a second to a single key. One cache node can serve that, but say what happens on a miss storm, and use request coalescing so one miss fetches once. Then cover killing a bad link: a short TTL on cached entries, or an explicit purge, so a malware link dies in seconds and not at the next TTL.

ID generation. Seven base-62 characters give about 3.5 trillion codes, far more than 6B. Compare the options. A counter is simple and collision-free, but a single counter is a bottleneck and sequential codes can be guessed. Pre-allocated ranges per node fix the bottleneck. Hashing the URL needs collision handling. Random codes need a uniqueness check. Custom aliases always need an atomic uniqueness guarantee, so use a unique key constraint in the store, not a check-then-write.

Analytics ingestion. Emit a click event to a stream, off the hot path. Aggregate in a consumer into per-link, per-minute counters. Accept at-least-once delivery and deduplicate or tolerate small overcounts, and say which you chose.

Minutes 40 to 45: trade-offs and wrap-up

Close by naming decisions as trade-offs against the requirements. 302 against 301: analytics against load. Cache TTL: fast kill against hit rate. Counter against random codes: guessability against coordination. Eventual consistency for analytics, strong uniqueness for aliases. Then say what you would do with more time: multi-region, rate limiting on creation, abuse scanning.

Common mistakes

  • Starting with boxes and no requirements.
  • Never saying a number, or saying them and not using them.
  • Treating the cache as free. Ask about invalidation, stampedes and a cold start.
  • Putting analytics on the redirect path.
  • Picking a code scheme without saying what happens on a collision.
  • Forgetting how a malicious link is killed fast.
  • Spending 20 minutes on requirements and leaving no time for the deep dive.
  • Staying silent while you draw.

Practice it

The brief for this problem is on its problem page. Run it against a clock, say your numbers out loud, and write them on the canvas. Then see which of the ten rubric dimensions the feedback says was weakest. Your first session is free.

Product updates by email

An occasional email when something new ships in Draw the System. No account needed, and every email has a link that removes your address.