System Design Playbook
01 · System designs100 users → 100M+ users12 min read

Design a URL shortener (like Bitly or TinyURL)

Turn long URLs into short codes and redirect millions of clicks per second. A classic first system design: tiny write path, huge read path, unique ID generation and analytics that must not slow down redirects.

In one sentence

Store code → long URL in a key-value friendly table, generate unique short codes without collisions, and make the redirect path almost entirely cache hits.

A reference design: it covers what an app like this needs and follows how these companies have described their systems publicly, simplified to be learnable.

Requirements

Functional

  • Create a short link for a long URL (optional custom alias and expiry)
  • Redirect a short link to the original URL
  • See click statistics for your links

Non-functional

  • Redirects must be very fast (< 50 ms) and always available
  • Codes must be unique and hard to guess
  • Read-heavy: ~100 redirects per link created
  • Links live for years

Back-of-the-envelope estimates

MetricEstimate
New links100M / month ≈ 40 writes/s (peak ~100/s)
Redirects100× reads ≈ 4,000/s (peak 10K+/s)
Storage100M × 12 × 5 years = 6B links × ~500 B ≈ 3 TB
Code lengthbase62, 7 chars = 62⁷ ≈ 3.5 trillion codes

The numbers already tell the story: writes are tiny, reads dominate, and the whole dataset fits in a single database for a long time. The hard part is latency and availability of the redirect, not volume.

API and data model

POST /api/urls        { longUrl, alias?, expiresAt? }  → 201 { code, shortUrl }
GET  /:code                                              → 302 Location: longUrl
GET  /api/urls/:code/stats                               → { clicks, byDay, byCountry }

links(code PK, long_url, owner_id, created_at, expires_at)
clicks(code, day, country, referrer, count)   -- analytics store

Architecture

Pick a scale and a request to follow it through the system. Tap any box to see what it does.

Architecture
Scale
Follow a request
◱Browser / appuser◍CDN / edgecached redirects⇄Load balancer⚙Shorten servicePOST /urls⚙Redirect serviceGET /:code⚙Key generatorpre-made codes⛁PostgreSQLprimary + replicasϟRediscode → url☰Click queueSQS / Kafka↻Analytics workerbatches clicks⛁Analytics DBClickHouse

Step by step · Open a short link

  1. 01GET sho.rt/aZ3k9Qx
  2. 02Viral link? The edge already has the answer
  3. 03Routed to a server
  4. 04Looks up the code
  5. 05Cache hit most of the time
  6. 06On a miss: read from a replica
  7. 07Publishes a click event without waiting
  8. 08302 Found → browser goes to the long URL

At this scale · 1M users

Split reads from writes

  • ▸Redirect service and shorten service scale separately (100:1 traffic)
  • ▸A key generator hands out pre-made unique codes
  • ▸Clicks go through a queue into an analytics store
  • ▸Read replicas and an edge cache for viral links

Tap any box to see what it does.

Deep dive: generating unique codes

ApproachHowTrade-off
Hash the URLFirst 7 chars of base62(MD5(url))Collisions need a check-and-retry; same URL always gets the same code
Counter + base62Auto-increment ID → base62 (125 → "21")Unique and short, but codes are guessable and one counter is a bottleneck
Random + checkRandom 7 chars, retry if takenSimple; retries grow as the space fills
Key generation servicePre-generate random codes, hand out batches to serversNo collisions at write time; servers keep a batch in memory (a crash loses a few codes, which is fine)

Start with random + unique index (the database rejects the rare collision). Move to a key generator or a Snowflake-style ID split into base62 when many servers create links at once.

How it changes with scale

Architecture
Scale
Follow a request
◱Browser / appuser⚙App serverAPI + redirects⛁PostgreSQLlinks table

Step by step · Shorten a URL

  1. 01POST /urls with the long URL
  2. 02Validates the URL and generates a 7-character base62 code
  3. 03INSERT (code, long_url); the unique index rejects duplicates
  4. 04Gets back sho.rt/aZ3k9Qx

At this scale · 100 users

One server, one database

  • ▸A single app handles both creating links and redirecting
  • ▸PostgreSQL with a unique index on the code
  • ▸Click count is a column updated on each redirect

Tap any box to see what it does.

UsersWhat you runWhy it changes
100One app + PostgreSQLAnything works. Ship it.
10K2 servers + LB + RedisAvailability during deploys; cache popular codes
1MSeparate redirect/shorten services, key generator, replicas, click queueRedirects are 100× the writes and analytics must not slow them down
100M+Sharded key-value store, multi-region, Kafka + columnar analyticsBillions of rows and global users who expect a fast redirect anywhere

Key takeaways

  • 01It's a read-heavy system: optimise the redirect path with caches at every layer.
  • 02Generate codes without coordination at write time: random + unique index, or a key generator.
  • 03Record clicks asynchronously so analytics never slow down redirects.
  • 04A single PostgreSQL goes surprisingly far; shard by code only at billions of links.

Check your understanding

1 of 3

Why is a URL shortener like Bitly a read-heavy system?

Security PlaybookNow attack it: threat-model this designFind the STRIDE threats in this architecture and pick the mitigations.↗