KatInterview

Building a Scalable Short-Link Service from Scratch

System DesignModel answer
MediumModel answerSystem Designsystem-designurl-shortenercaching

Last updated

Design a service that maps long web addresses to compact codes and serves redirects at very high read volume, covering API, storage, code generation and caching.

Task

Design a link-shortening platform in the spirit of well-known public services. A user submits a long web address and gets back a compact, unique code. Anyone who opens the compact link is sent to the original address.

Core operations

  • create(long_url) returns a short link.
  • resolve(code) returns the original address so the client can be redirected.

Quality goals

  • Fast, highly available redirects. Resolving a link is the hot path and must feel instant.
  • Large scale. Roughly 100 million new links per day, with about 100 reads for every write.
  • Durability. Links stay valid indefinitely unless the creator sets an expiry.

Numbers to plan around

  • Data is retained for 5 years.
  • About 10 billion redirects per day.
  • About 100 million creations per day.

What to cover in your answer

  1. API: request and response shapes, plus status codes for the main outcomes.
  2. Data model: the schema (and storage technology) for the code-to-address mapping.
  3. Code generation: how you produce unique codes of about 6 to 8 characters, and how you avoid or resolve collisions.
  4. Architecture: load balancers, application servers, database, cache and any supporting components, with reasoning for each.

Example

  • Submitted address: https://www.example.com/very/long/path/to/resource
  • Returned short link: https://tiny.url/aB3dEf
  • Visiting https://tiny.url/aB3dEf produces a 302 response pointing to the original address.

Hints

Hint 1

Convert the daily volumes into per-second rates (average and peak), then compute the total number of links over 5 years. Use that total to decide how many Base62 characters you need.

Hint 2

Compare three ways of making codes: hash-and-truncate, a unique counter encoded in Base62, and a pre-built pool of random codes. Ask which of them need a collision check on every write, and how counter allocation can avoid a network call per request.

Hint 3

Reads dominate by 100 to 1. Think about cache-aside with a skewed popularity distribution, protection against hot keys and invalid-code scans, and keeping the redirect path independent of the code-allocation component.