LESSON 31.1
URL shortener end to end
Applying the 4-step delivery framework to design a high-throughput URL shortening service with low-latency redirects.
6 min read
The URL shortener (such as Bitly or TinyURL) is the quintessential system design interview question. While the problem statement sounds deceptively simple, it tests core competencies: calculating back-of-the-envelope capacity, choosing the right key generation strategy, managing heavy read-write asymmetries, and designing caching layers that withstand traffic spikes.
Here is how to apply the 4-step delivery framework end to end.
1. Requirements
Before writing architecture components, clarify the functional and operational scope with your interviewer:
- Functional Requirements:
- Given a long URL, generate a compact, unique short alias (e.g.
https://sho.rt/aZ89k1q). - When users visit the short URL, redirect them immediately to the original long URL.
- Track basic analytics (click counts, referrer, timestamp).
- Custom short alias support is optional.
- Given a long URL, generate a compact, unique short alias (e.g.
- Non-Functional Requirements:
- Low redirect latency: Redirects must complete in under 20 ms P99.
- High availability: 99.99% uptime; link redirection must not fail during peak hours.
- Scale: Read-heavy workload with an asymmetric 100:1 read-to-write ratio.
2. Estimates and scale
State clear assumptions and perform back-of-the-envelope math:
- Write volume: 100 million new URLs created per month, roughly 40 writes per second.
- Read volume: 100 to 1 ratio implies 4,000 redirects per second average; 10,000 requests per second at peak.
- Storage math: Each record holds short code (7 bytes), original URL (2 KB), created timestamp (8 bytes), user ID (8 bytes), totaling roughly 2.5 KB per URL.
- 5-year storage: 100M * 12 months * 5 years = 6 billion URLs * 2.5 KB ≈ 15 TB.
- Cache sizing (80/20 rule): 20% of URLs generate 80% of daily redirects. Daily reads = 4,000 * 86,400 ≈ 345M redirects. Caching 20% requires roughly 170 GB of RAM, easily hosted in a modest Redis cluster.
3. API design and data model
Define compact REST endpoints:
POST /v1/urlsreturns{"short_url": "https://sho.rt/aZ89k1q", "created_at": 1718000000}GET /:short_codereturns HTTP 302 Found withLocation: https://example.com/long/path
Data schema stored in a relational database (PostgreSQL) or distributed KV store (DynamoDB):
id: 64-bit integer primary key.short_code:VARCHAR(7) UNIQUE(indexed).long_url:VARCHAR(2048).created_at:TIMESTAMP.
4. High-level design
Deep dive 1: Generating unique short codes
How do you turn a long URL into 7 Base62 characters ([0-9, a-z, A-Z])?
MD5 / SHA-256 Truncation
- Deterministic; identical long URLs produce identical short codes
- Truncating 128-bit hashes introduces hash collisions; requires checking database on every write and appending counter salts
Pick it when: Systems where deduplicating identical URLs is mandatory.
Base62 Encoding of Sequential IDs
- Zero collision risk; mathematical guarantee that every unique 64-bit integer maps to a unique Base62 string
- Sequential IDs reveal business volume unless obfuscated with a Feistel cipher
Pick it when: High-throughput production URL shorteners.
To generate unique 64-bit integers without centralized bottlenecks, workers claim blocks of 10,000 IDs from a central token range coordinator (e.g. ZooKeeper or Redis INCRBY) and increment local memory counters.
Deep dive 2: Handling cache stampedes on viral links
When a breaking news link is posted on social media, thousands of concurrent requests arrive simultaneously for a newly created short URL not yet in Redis.
If all 10,000 requests miss the cache at once, they all query the database simultaneously, causing a cache stampede:
- Use single-flight request coalescing: In the application layer, ensure that only one concurrent worker queries the database for a given
short_code, while all other waiting threads share that single database response and populate Redis. - Use probabilistic early expiration (XFetch) or pre-warming to refresh hot keys before their TTL expires.
To build this design step by step, with a load test at the end, open the Bitly lab.
Try it
URL Shortener HLD
Or design it freely in the practice workspace.