SD-1 · Back to the write-up
Design a URL Shortener
Hand out unique 7-character keys, store key → URL, and serve the flood of redirects from a cache.
Follow a request
Create a link
Open a link · cache hit
Open a link · cache miss
Count clicks
Delete a link
Cache node fails
Clients
Edge
Services
Data
UserBrowser or app
Load balancerSpreads traffic
App serversStateless, many
Key serviceID blocks
Click queueAsync events
CacheHot keys, Redis
Database16 shards × 3
AnalyticsClick counts
The numbers, at a glance
1 write ≈ 40/s99 reads ≈ 4,000/s
0.6 TBY1
1.2 TBY2
1.8 TBY3
2.4 TBY4
3.0 TBY5
6B used in 5 years3.5T available
A redirect, in order
Cache miss shown. On a hit, step 4 returns the long URL and steps 5 to 7 disappear.
User
Load balancer
App server
Cache
Database
Click queue
1 GET /x7Kp2Qa
2 route
3 GET key
4 miss
5 SELECT by key
6 long URL
7 SET key
8 click event
9 302
10 302 → long URL
RequestResponseAsync, does not block
Three ways to make keys
The decision interviewers care about most.
https://very.long/url…
md5 → a9f3K2x8…
take 7 → a9f3K2x
exists? retry
+ No coordination needed.− Collisions mean a check and retry on every write.
server Aserver Bserver C
counter++ bottleneck
125000001 → 8sud3
+ Simple; never collides.− Every write waits on one machine; keys are guessable.
Key service
A: 1000–1999B: 2000–2999
1042 → shuffle → base62
"x7Kp2Qa"
+ No collisions and no per-request coordination.− A crashed server wastes its unused block. Harmless.
How it grows
Add a box only when a number forces you to. Highlighted boxes are new at each stage.
User
App server
SQL database
User
Load balancer
Key serviceApp × 6Click queue
CacheDB + 2 replicasAnalytics
User
Load balancer
Key serviceApp × 58Click queue
Cache × 516 shards × 3Analytics
Was this walkthrough useful?Your vote tells us what to improve next.