SD-33 · Back to the write-up
Design a Like Counts at Scale
A like is a unique row; its change goes through a log to an aggregator that writes each post's count once a second; hot counts live in every app server.
Follow a request
Like a post
Count, 1 second at a time
Read counts · hit
Read counts · miss
A famous post
Retry and recount
Clients
Edge
Services
Data and pipeline
UserTaps the heart
API gatewayAuth + limits
Like APIHot counts in memory
Count cache5 s lifetime
Like rows64 shards by user
Like logBy post id
AggregatorΣ per post per second
Counter store8 shards by post
Recount jobNightly
Likers indexRows by post
The numbers, at a glance
1 like ≈ 100,000/s10 reads ≈ 1,000,000/s
20,000/sRow per like
1,000/sRow limit
1/sBatched
20% on one post80% on every other post
A like, in order
The request ends at step 6. Steps 7 to 9 happen within about 2 seconds.
App
Gateway
Like API
Like rows
Like log
Aggregator
Counter store
1 PUT /posts/42/like
2 user 7, post 42
3 INSERT row + outbox
4 1 row inserted
5 200, count + 1
6 liked: true
7 outbox → +1 event
8 events for post 42
9 n += Δ, batch id
RequestResponseAsync, does not block
How to keep one hot number
The decision this question is really about.
GET count
COUNT(*) 5M rows
+ Always exact.− Millions of rows per read.
PUT like
INSERT rowUPDATE n + 1
20,000 waits on 1 row
+ Exact at once.− One row lock for every like.
PUT like
INSERT row + outbox
log by postΣ per second
n += Δ once a second
+ Unique rows, 1 counter write a second.− Counts lag about 2 s.
How it grows
Add a box only when a number forces you to. Highlighted boxes are new at each stage.
App
App server
Database
App
Gateway
Like API × NCount cache
Like rows × 64Counter store
App
Gateway
Like API × NCount cache
Like rows × 64Like logAggregatorCounter store
App
Gateway
Like API + hot copiesCount cache
Like rows × 64Like logAggregatorCounter store
Likers indexRecount job
Was this walkthrough useful?Your vote tells us what to improve next.