Prod Squad

Worked case: design a URL shortener

The classic system design question answered PM-style, with requirements, scale, components, tradeoffs and metrics.

7 min readPM#worked case#system design#url shortener#caching
Ask about this lesson
On this page

"Design a URL shortener" (like Bitly or TinyURL) is the most famous system design question because it's small enough to finish in 30 minutes, yet touches every key idea: requirements, scale estimates, data storage, caching, redirects, analytics and abuse. Here's a full PM-style answer.

Why interviewers ask this

It's a fair test of structured thinking for PMs without deep engineering experience. Interviewers watch whether you clarify the product first, use rough numbers, keep the design simple, identify the read-heavy pattern, and bring product judgement: custom links, analytics, link expiry and abuse prevention.

The framework

We'll follow the six steps: requirements → users and scale → components → data flow → tradeoffs → metrics.

Worked example

Question: "Design a URL shortener."

1. Clarify requirements

"Who is this for: consumers sharing links, or businesses running marketing campaigns? Let's say a product for marketers, which makes analytics important.

  • Functional: create a short link for a long URL; redirect anyone who visits the short link; optional custom aliases (sho.rt/summer-sale); click analytics (count, country, device, referrer).
  • Non-functional: redirects must be very fast (people click and wait) and highly available (a broken link in a campaign is costly). Links shouldn't be guessable in bulk. Analytics can lag by a minute.
  • Out of scope: billing, team management."

2. Users and scale

"Assume 50 million new links per month (about 20 per second) and 100 redirects per link over its life, so around 2,000 redirects per second on average, with spikes of 10x when a campaign goes viral. It's heavily read-dominated: 100 reads per write. Storage: 50 million links × about 500 bytes ≈ 25 GB per month, which is modest."

3. Core components

flowchart LR
    M["Marketer"] -->|create link| API["Link API"]
    API --> ID["Short code generator"]
    API --> DB[("Links database")]
    V["Visitor clicks link"] --> E["Redirect service<br/>at the edge"]
    E --> C[("Cache<br/>code to URL")]
    C -->|miss| DB
    E -->|301 or 302 redirect| T["Target website"]
    E -.->|click event| Q[["Queue"]]
    Q -.-> AN["Analytics pipeline"]
    AN -.-> AD[("Analytics store")]
  • Link API: creates links, validates URLs, checks for malware or phishing, and handles custom aliases.
  • Short code generator: produces short unique codes (for example 7 characters from letters and digits gives over 3 trillion combinations). Random codes are harder to guess than sequential ones.
  • Links database: code → long URL, owner, created date, expiry.
  • Redirect service and cache: the hot path. Popular codes live in a cache (and at CDN edges), so redirects are served in milliseconds.
  • Click analytics: each redirect emits an event to a queue; a pipeline aggregates counts. Analytics never slows the redirect down.

4. Data flow

"Create: the marketer submits a URL → validated and safety-checked → code generated (or custom alias checked for availability) → saved → short link returned.

Redirect: visitor opens sho.rt/abc1234 → the edge service checks the cache → on a miss, reads the database and caches the result → returns a redirect → separately emits a click event. The user waits only for the lookup."

5. Tradeoffs and failures

  • 301 vs 302 redirects: a permanent (301) redirect lets browsers cache it, which is fast but means we miss some click analytics. A temporary (302) redirect counts every click. For a marketing product, analytics matter, so 302.
  • Random vs sequential codes: sequential codes are simpler but let people enumerate everyone's links; random codes are safer.
  • Abuse: spammers use shorteners to hide phishing links. Scan URLs on creation, re-scan periodically, rate-limit link creation, and let users report links. A takedown should take effect immediately, so invalidate the cache.
  • Failures: if the analytics pipeline is down, redirects must still work; events queue up. If the database is down, cached links keep working.
  • Expiry and edits: let marketers change a link's destination; invalidate the cache when they do.

6. Metrics

"System: redirect p95 latency (target under 50ms), redirect availability (99.99%), and analytics delay. Product: links created per active customer, clicks per link, and retention of business customers. Trust: malicious link reports and time to takedown."

Common mistakes

  • Diving into hashing algorithms before clarifying the product.
  • Missing the read-heavy pattern and the need for caching.
  • Putting analytics on the critical path, slowing redirects.
  • Ignoring abuse, which is the biggest real-world problem for shorteners.
  • No metrics, or only technical ones.

Interviewer tips

What "great" sounds like:

  • "It's about 100 reads per write, so I'll design around fast cached redirects."
  • "Analytics are asynchronous: clicks go to a queue so redirects stay fast."
  • "302 instead of 301, because our customers care about counting clicks."
  • "Abuse is the real risk here: scan, rate-limit, and enable fast takedowns."
  • "Redirect availability is the metric I'd guard most closely."

Practice questions

Answer one in a mock interview and get graded instantly.

  1. 1.Design a URL shortener.

    Practice this →
  2. 2.How would you prevent a URL shortener from being used for phishing?

    Practice this →
  3. 3.A URL shortener's redirects slow down during viral campaigns. What would you change?

    Practice this →

Finished reading?