System design · 2 of 16
Mid
Senior
- Monitoring and alerting for 100 servicesSRE · Platform · DevOps
- Secrets management platformDevOps · Platform · SRE
- Multi-region active-active serviceSRE · Platform
- Distributed job scheduler (cron at scale)SRE · Platform
- Internal developer platform (IDP)Platform
- Multi-tenant Kubernetes platformPlatform · DevOps · SRE
- GitOps for a fleet of 100 clustersPlatform · DevOps
- ML training platform and feature storeMLOps · Platform
- LLM inference platformMLOps · Platform · SRE
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 →
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
| Item | Assumption | Result |
|---|---|---|
| New links | 100 million / month | ~40 writes per second |
| Reads to writes | 100 : 1 | ~4,000 redirects per second, peaks ~20,000 |
| Storage per link | ~500 bytes | 100M × 12 months × 5 years × 500 B ≈ 3 TB |
| Code length | base62, 7 characters | 62⁷ ≈ 3.5 trillion codes, plenty |
How it works
- Create: the API gets a new unique number from the ID generator, turns it into base62 (
0-9a-zA-Z), and storescode → long URLin a key-value database. - 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).
- 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
| Decision | Options | Pick and why |
|---|---|---|
| Unique codes | hash the URL, random code, counter | counter ranges: each API server reserves a block of 1,000 IDs from a central counter, so no collisions and no central call per link |
| Database | SQL or key-value | key-value (DynamoDB/Cassandra): simple lookups by key, scales out easily |
| Redirect code | 301 or 302 | 302 if analytics matter, 301 to save traffic |
| Analytics | write to DB on click, or async | async 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
| Target | Number | How it is met |
|---|---|---|
| Availability (redirects) | 99.99% = 4.3 minutes down per month | stateless API pods in 3 AZs with N+2 spare capacity |
| Cache node fails | no user impact | Redis cluster with a replica per shard; failover in ~10-20 s; misses go to the database |
| Cache fully lost | slower, still up | the database is sized to take all 20,000 reads/s on its own (plan for it) |
| Region fails | RTO 5 min, RPO ~1 s | DynamoDB 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 analytics | can lose up to 5 minutes | Kafka 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.