AlgoViz

Design a URL shortener

medium 45 min

Turn a long URL into a short code and redirect anyone who opens it.

Agree on the scope

Ask what changes the design, then commit to a short list.

Clarifying requirements

  1. Interviewer

    Let's design a URL shortener like Bitly or TinyURL. What questions do you have?

  2. You

    First scale, then features, then technical limits:

    • How many new URLs a day? At Bitly's size I would assume 100M or more.
    • What is the read to write ratio? I would guess 100:1 or higher, since links are clicked many times.
    • Do links live forever, or can they expire?
    • Custom aliases, analytics, QR codes, accounts?
    • Redirect latency? I would target under 100 ms at p99.
    • Must we handle phishing and malware?
    • Availability? Even a short outage breaks millions of links, so 99.99%.
  3. Interviewer

    100M new URLs a day, 100:1 reads, links live forever with optional expiry. Yes to custom aliases and analytics.

  4. You

    Then I will design for 100M writes and 10B reads a day. Password-protected links, A/B tests, mobile deep links and team workspaces are out of scope for now.

Functional requirements

  • Generate a unique short URL from a long URL
  • Redirect a short URL to the original (301 or 302)
  • Custom short codes (optional aliases)
  • Expiry (optional TTL), then 410 Gone
  • Click analytics: count, geography, referrer, device, over time
  • User accounts to manage links

Out of scope for now

  • Password-protected links
  • A/B testing for links
  • Deep links into mobile apps
  • Team and workspace management

Non-functional requirements

QualityTargetWhy
Latencyredirect < 100 ms p99Users should not notice the hop
Availability99.99% for redirectsA broken link is worse than a slow shorten call
Scale100M writes, 10B reads a dayMust scale horizontally
Durabilityzero link lossA link that was issued must work
Securitycodes hard to enumerate, abuse blockedLinks may point at private or harmful pages
Consistencynew link works at once, analytics may lagRead your own write; counts can be late
Uniquenesscodes never reassigned, even after expiryAn old link must not start pointing elsewhere

Why these questions

Each answer changes the design

You askIf the answer isThen
Can a link's destination change?YesNo permanent redirects; short edge TTLs and purges
Must clicks be exact?YesCount from edge logs, or skip edge caching
Are destinations private?YesRandom codes plus access control, not scrambling alone
Custom aliases?YesA conditional write decides; generated codes and aliases share one namespace
How many links a day?ThousandsStop at version 1
Global users?YesEdge, regional caches, one home region for alias claims