Skip to content
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

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
1user → gw · 100,000 reads/sA famous singer posts. 100,000 feeds a second ask for the same count.1 / 7

The numbers, at a glance

For every like……a count is read 10 times
1 like ≈ 100,000/s10 reads ≈ 1,000,000/s
Writes to the hottest counterOne famous post, per second
One post can be a fifth of everythingAt a peak moment
So no part of the design may route one post to one machine.

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.

Count rows on read
Too slow
GET count
COUNT(*) 5M rows
+ Always exact.− Millions of rows per read.
Counter in the transaction
Breaks on hot posts
PUT like
INSERT rowUPDATE n + 1
20,000 waits on 1 row
+ Exact at once.− One row lock for every like.
Rows + log + batches
Our pick
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.

Stage 1 · ≈ 500 likes/sMake it work
App
App server
Database
One database; the count sits on the post row.
Stage 2 · ≈ 10,000 likes/sSpread the load
App
Gateway
Like API × NCount cache
Like rows × 64Counter store
Shard likes by user, counts in their own store, cache the reads.
Stage 3 · ≈ 100,000 likes/sCount in batches
App
Gateway
Like API × NCount cache
Like rows × 64Like logAggregatorCounter store
A log and an aggregator turn 20,000 likes into 1 write.
Stage 4 · 100,000 reads/s on one postHot posts and drift
App
Gateway
Like API + hot copiesCount cache
Like rows × 64Like logAggregatorCounter store
Likers indexRecount job
Hot counts in app memory, a likers index, a nightly recount.
Was this walkthrough useful?Your vote tells us what to improve next.