Skip to content
$ lynuxpro
← All system designs

MID · 12 MIN · SRE · DEVOPS · PLATFORM

URL shortener (like bit.ly)

Short codes, a read-heavy cache, unique IDs at scale and click analytics. A favourite warm-up question.

Peak load
20,000 redirects/s
Availability target
99.99%
RTO: back up within
5 min
RPO: data you can lose
~1 s

← Swipe sideways to see the whole diagram →

ClientEdge / networkServiceData storeQueue / streamasync / background

The question

"Design a service that turns a long URL into a short one, like lnx.pro/aB3x9, and redirects people who open it."

Requirements

  • Create a short code for a URL; open the code and get redirected.
  • Very fast redirects (under ~50 ms on the server).
  • Highly available: a broken link breaks other people's websites and emails.
  • Count clicks for analytics.

Quick numbers

ItemAssumptionResult
New links100 million / month~40 writes per second
Reads to writes100 : 1~4,000 redirects per second, peaks ~20,000
Storage per link~500 bytes100M × 12 months × 5 years × 500 B ≈ 3 TB
Code lengthbase62, 7 characters62⁷ ≈ 3.5 trillion codes, plenty

How it works

  1. Create: the API gets a new unique number from the ID generator, turns it into base62 (0-9a-zA-Z), and stores code → long URL in a key-value database.
  2. Redirect: the API looks the code up in Redis first. On a miss it reads the database and fills the cache. It returns HTTP 301 (cacheable by browsers) or 302 (every click hits you, better for analytics).
  3. Every click also drops a small event onto Kafka. Analytics jobs read it later, so counting never slows a redirect.

Remember it: reads are 100x writes, so the design is mostly about caching reads.

Key decisions

DecisionOptionsPick and why
Unique codeshash the URL, random code, countercounter ranges: each API server reserves a block of 1,000 IDs from a central counter, so no collisions and no central call per link
DatabaseSQL or key-valuekey-value (DynamoDB/Cassandra): simple lookups by key, scales out easily
Redirect code301 or 302302 if analytics matter, 301 to save traffic
Analyticswrite to DB on click, or asyncasync via Kafka: redirects stay fast even if analytics is slow

What breaks and how it heals

  • Cache down: reads fall through to the database. Size the database for that, or run Redis with replicas.
  • Hot link (one viral link): it stays in cache; add a small in-memory cache in each API pod too.
  • ID service down: servers still have their reserved block, so creation keeps working for a while.
  • Abuse: rate-limit link creation per user, and scan URLs for malware and phishing.

Making it bigger

  • Serve redirects from the edge (CDN workers) for the most popular links.
  • Shard the database by code; replicate across regions for reads.
  • Expire old links with a TTL to control storage.

High availability and disaster recovery

TargetNumberHow it is met
Availability (redirects)99.99% = 4.3 minutes down per monthstateless API pods in 3 AZs with N+2 spare capacity
Cache node failsno user impactRedis cluster with a replica per shard; failover in ~10-20 s; misses go to the database
Cache fully lostslower, still upthe database is sized to take all 20,000 reads/s on its own (plan for it)
Region failsRTO 5 min, RPO ~1 sDynamoDB global tables copy writes to a second region in about a second; Route 53 health checks (10 s interval, 3 failures) plus a 60 s DNS TTL move traffic in ~1.5-2 min
Click analyticscan lose up to 5 minutesKafka with replication factor 3; analytics is not on the critical path

Remember it: protect the redirect path first. Losing a few minutes of click counts is fine; broken links are not.

What interviewers listen for

  • Back-of-the-envelope math (read/write ratio, storage, code length).
  • A collision-free ID strategy and its trade-offs.
  • Caching, and keeping analytics off the hot path.