KatInterview

Global Link Shortener: Fast Redirects and Collision-Free Aliases

System DesignModel answer
HardModel answerSystem Designsystem-designurl-shortenercaching

Last updated

Design a worldwide short-link service with fast, always-available redirects, unique compact IDs, custom aliases, expiry, and asynchronous click analytics.

Problem

Design a link-shortening platform that turns long web addresses into compact aliases and serves redirects to users around the world. Redirects must feel instant and must keep working even when parts of the system fail.

Functional requirements

  • Create: Given a long URL, return a unique short alias of roughly 7 to 8 characters.
  • Custom alias: A user may request a specific alias, which is accepted only if it is free.
  • Redirect: Visiting a short link sends the visitor to the original URL with minimal delay.
  • Expiry: A link may carry an optional expiration time. After it passes, the service answers with 404 or 410.
  • Analytics: Record click count, timestamp, user agent and referrer. Collection must be asynchronous and must not slow the redirect.

Non-functional requirements

  • p99 redirect latency under 15 ms for users worldwide.
  • 99.999% availability on the redirect path.
  • Favor read availability over immediate cross-region write consistency.
  • No single point of failure; scale horizontally.

Expected load

  • About 500 million new links per month (roughly 200 writes/s on average, 2,000/s at peak).
  • A read-to-write ratio of 100:1 (roughly 20,000 reads/s on average, 100,000/s at peak).
  • Links are retained for about 5 years. Estimate total storage for links and metadata.
  • Aim to hold about 20% of each day's hot links in memory.

What to cover

  1. API: endpoints, payloads, status codes, and the choice between 301 and 302/307.
  2. ID generation: compare hashing with collision handling against distributed ID generators or a range-allocation service combined with Base62.
  3. Storage: database type and partitioning or sharding key.
  4. Caching and CDN: multiple cache tiers and how stale entries are invalidated.
  5. Edge routing: how users reach a nearby point of presence to meet the latency goal.

State your assumptions, do the capacity math, and discuss the trade-offs you accept.

Hints

Hint 1

Do the arithmetic first: 500M per month for 60 months, a few hundred bytes per record, and 62^7 versus your total link count. These numbers should justify ID length, storage engine and cache size.

Hint 2

A cross-ocean round trip alone is far larger than 15 ms, so think about where the redirect decision is made rather than only how fast the database is. Also consider what 301 does to analytics and expiry.

Hint 3

For IDs, ask which approach avoids a read-before-write on every create. Consider handing out blocks of counter values to app servers, then scrambling and Base62-encoding them, and keep custom aliases in a separate namespace.