KatInterview

Build a Link-Shortening Service That Scales to Billions

System DesignModel answer
MediumModel answerSystem Designsystem-designurl-shortenercaching

Last updated

Design a service that maps long URLs to compact aliases and redirects visitors quickly, covering APIs, storage, code generation, caching, and sharding.

Design a Link-Shortening Service

Your team wants an internal product that works like a public link shortener. A user submits a long web address and gets back a short one. Anyone who opens the short address is sent to the original destination.

What the service must do

Functional

  1. Create a short link. Take a long URL and return a unique short URL, such as sho.rt/Ab3dE9z.
  2. Redirect. Opening a short URL sends the visitor to the original URL using an HTTP redirect (permanent or temporary; you decide and justify).
  3. Custom aliases. The creator may optionally choose the short string themselves.
  4. Expiry. Links live forever by default, but the creator may set an expiration date.

Non-functional

  1. Redirects must be low latency and highly available.
  2. Traffic is read-heavy, around 100 redirects for every link created.
  3. Data must be durable for the lifetime of the link.

What to cover in your answer

  • A high-level architecture (describe or sketch the components and the request flow).
  • The API: request and response shapes for creating a link and for following one, including error cases.
  • The data model: SQL or NoSQL, with a schema and your reasoning.
  • Short-code generation: how you guarantee uniqueness (for example Base62 encoding, hashing, random codes, or a dedicated ID service) and the trade-offs between these.
  • Scaling: caching, load balancing, replication, and how the database is partitioned.
  • Back-of-the-envelope numbers for traffic, storage, and key-space size.

Hints

Hint 1

Start with estimates: derive writes per second from monthly volume, multiply by 100 for reads, and compute total records over a long horizon. Then check how many Base62 characters you need to cover that many codes with generous headroom.

Hint 2

The access pattern is almost entirely point lookups by short code, with no joins. Let that drive your storage choice, and use a conditional 'insert if absent' to enforce uniqueness for both generated codes and custom aliases.

Hint 3

Compare three generation strategies (hash, random, counter) by collision handling, coordination needs, and guessability. Consider handing each app server a block of IDs so no per-request coordination is needed, then scrambling the ID before encoding.

Hint 4

Think about what 301 versus 302 does to caching, analytics, and the ability to change or expire a link later.