KatInterview

Design a Link Shortening Service: Codes, Storage, and Redirects

System DesignModel answer
MediumModel answerSystem Designurl-shortenercachingdistributed-systems

Last updated

Design a system that converts long URLs into short codes and redirects visitors at scale, covering code generation, storage choices, caching, and failure handling.

Build a Link Shortening Service

A product team wants to launch a link shortening service. Users paste in a long URL and get back a compact link — something like sho.rt/aB3kZ9. Anyone who visits that short link gets redirected to the original destination. Your job is to drive the design from first principles: clarify requirements, estimate load, pick a code-generation strategy, choose a data store, and describe how the redirect path holds up under heavy and skewed traffic.

No scale numbers are given upfront — that is intentional. State your own assumptions and carry them through your estimates.

Scope for this exercise

The following are in scope:

  • Creating a short code for a given long URL
  • Redirecting a visitor who hits a short code to the original URL
  • Optional link expiration

Treat custom vanity codes and per-link click analytics as stretch topics to raise if time allows.

Constraints

  • Every short code must map to exactly one long URL; no two different URLs may share a code.
  • Redirect speed and availability outweigh the latency of creating a new link.
  • Codes must not be trivially guessable or enumerable for a public service.

Clarifying Questions to consider

Before diving in, think about what you would ask the interviewer:

  • What write and read volumes should the design target — links created per day and redirects per second?
  • If the same long URL is submitted twice, should the service return the same code or generate a new one?
  • Do links expire by default? Can creators delete or update them?
  • Is sequential or predictable code assignment acceptable, or is enumeration resistance required?
  • Is real-time click analytics required, or is an eventually consistent count acceptable?

Part 1 — Requirements, API, and capacity estimates

Pin down what the service must do and how it must perform. Define the two core endpoints. Then work through back-of-envelope estimates for write rate, read rate, storage needs, and the minimum code length required to avoid exhaustion.

Cover:

  • Functional requirements and non-functional priorities (latency, availability, durability)
  • POST /links and GET /{code} — request/response shapes and appropriate HTTP status codes
  • Estimated writes per second, reads per second, and total storage over a multi-year horizon
  • Why your chosen redirect status code matters for browser caching and analytics accuracy

Part 2 — Generating short codes

Compare at least two code-generation approaches and argue for one. The scheme must stay collision-free when many application servers are creating links simultaneously.

Cover:

  • At least two generation strategies (e.g., hashing, auto-increment counter, pre-allocated key ranges) with trade-offs
  • How your chosen approach guarantees uniqueness across concurrent servers without a bottleneck
  • Whether codes are guessable, and how to harden against enumeration if that is a requirement
  • How you handle custom aliases and idempotent shortening of the same long URL

Part 3 — Storage, caching, and the redirect path

Pick a data store, justify it against the access pattern, and walk through both the write path (creating a link) and the read path (serving a redirect). Explain how the system stays fast and available under skewed traffic — where a small number of links receive the vast majority of hits — and what degrades gracefully when individual components fail.

Cover:

  • Data model and store choice tied to the read-heavy, key-lookup access pattern
  • Cache placement, eviction policy, and how expiration or deletion gets reflected in the cache
  • Partitioning and replication strategy for scale and fault tolerance
  • Failure scenarios: what happens when the cache is cold, the primary store is slow, or a node goes down
  • Observability signals you would monitor on the redirect path

Hints

Hint 1

For code length, work backwards from your total projected links. A 7-character base-62 string gives over 3.5 trillion combinations — compute how many years that covers at your assumed write rate before committing to a length.

Hint 2

The redirect endpoint is a pure key-value lookup by code. That access pattern almost never needs a relational database. Think about what that implies for your store choice and your cache key design.

Hint 3

A 301 redirect tells browsers to cache the destination permanently, meaning future visits skip your servers entirely — great for load, bad for analytics and for serving deletions. A 302 keeps every request flowing through you. Pick deliberately and state the trade-off.