Skip to content
System Design Lab
Case Studies

Design a URL Shortener

The classic warm-up: turn long links into short codes and redirect billions of clicks. From requirements and estimates to a design that grows from one database to caches and edge redirects.

Beginner 5 min read· Updated 2026-11-18

What you will learn

  • Turn vague requirements into numbers: traffic, storage, read/write ratio.
  • Choose a short-code scheme that is unique, compact and hard to enumerate.
  • Scale a read-heavy system with caching and edge redirects, step by step.
  • Handle analytics, expiry and abuse without slowing redirects.

Before you start

Requirements

  • Functional: create a short link for a long URL (optionally a custom alias and expiry); visiting the short link redirects to the long URL; owners see click counts.
  • Non-functional: redirects are fast (p99 < 100 ms worldwide) and highly available; codes are short and not guessable in sequence; links never point to the wrong target.
  • Out of scope for now: editing targets, per-link access control, rich analytics dashboards.

Back-of-the-envelope estimates

QuantityAssumptionResult
New links100 M per month~40 writes/s (peak ~200/s)
Redirects100:1 read/write ratio~4,000 reads/s (peak ~20,000/s)
Storage5 years × 100 M × 12 × ~500 B~3 TB — fits a single partitioned database for years
Code length62 characters (a–z, A–Z, 0–9)7 characters ≈ 3.5 × 10¹² codes

The numbers say the problem is reads, not writes or storage: a redirect must be answered thousands of times per second, mostly for a small set of popular links. That shapes everything that follows.

API and data model

One table keyed by the code
-- POST /api/links {"url": "https://example.com/very/long", "alias": null, "expiresAt": null}  → 201 {"code": "aZ3k9Qx"}
-- GET  /{code}  → 302 Location: https://example.com/very/long

CREATE TABLE links (
    code        varchar(10) PRIMARY KEY,
    long_url    text        NOT NULL,
    owner_id    bigint,
    created_at  timestamptz NOT NULL DEFAULT now(),
    expires_at  timestamptz
);

Generating short codes

ApproachProsCons
Hash the URL (MD5/SHA) and take 7 charsSame URL → same code; no coordinationCollisions must be detected and retried; a custom alias still needs a check
Database sequence, base62-encodedSimple and uniqueSequential codes are guessable; one sequence is a write hotspot
Pre-generated ID ranges per server (or Snowflake IDs)Unique without per-request coordination; scales writesMust shuffle or encrypt the number so codes are not enumerable
Random 7–8 chars + insert-if-absentUnguessable, trivially distributedRare collision retries; slightly longer codes for safety

Evolve the design

Play each version, watch where it breaks, then move to the next. The simulation uses 1,500 requests/s — scaled down, but with the same shape: 99% redirects, a few very popular links.

Stage 1 of 3

API + one database

Current architecture
Every redirect reads the code from the database.
Limitation
Read capacity equals database capacity: ~1,100 lookups/s on 10 connections.
Bottleneck
The database connection pool: it is 100% busy, a third of redirects time out.
Improvement
Popular links are read over and over — keep them in memory.
Performance impact
~67% success, p95 ≈ 300 ms.
Complexity & operational cost
One app tier and one database.
Trade-off
Simplest possible design, capped by the slowest component.
Loading simulation…

Deep dives

  • 301 vs 302: a 301 is cached by browsers forever — great for load, terrible if you need click counts or to disable a link. Most shorteners use 302 (or 307) with a CDN TTL.
  • Click analytics: never write to the database on the redirect path. Publish a click event to a log (Kafka) and aggregate asynchronously.
  • Abuse: rate-limit link creation per user and IP (rate limiting), scan targets against malware lists, and be able to purge a code from every cache quickly.
  • Growth: when one database is no longer enough, shard by code — every lookup carries the code, so routing is trivial.

Trade-offs

BenefitsCosts
CachingAbsorbs the read-heavy, skewed traffic cheaply.Changes and takedowns propagate only as fast as TTLs and purges.
Code schemeRandom codes are unguessable and easy to generate anywhere.Slightly longer codes and collision handling.
Edge redirectsSingle-digit-millisecond redirects worldwide.Analytics and control move partly to the CDN.

Interview checklist

  • State the read/write ratio early — it justifies caching and the CDN.
  • Pick a code scheme and explain uniqueness and enumerability.
  • Keep the redirect path read-only and fast; push analytics to an async pipeline.
  • Mention expiry, custom aliases, abuse handling and how you would shard later.

Further reading

Related topics