Skip to content
SD-50 · Back to the write-up

Design a Live Likes

Save each like once by user, count likes per post from a stream once a second, and push one coalesced number to the people watching.

Follow a request

Clients
Services
Counting
Data
AppPhone or browser
Like APIStateless
GatewaysLive sockets
Like streamBy post
Counter workersSum per second
Pub/subChannel per post
Like recordsBy user
Count cacheIn memory
Count storeOne row per post
1stream → counter · 30,000 eventsThe worker that owns post 42’s partition reads a second’s worth of events: 30,000 +1s and 412 −1s.1 / 6

The numbers, at a glance

For every 100 feed reads……about 5 likes happen
100,000 likes/s2M count reads/s
Writes to a hot post’s countAt 30,000 likes a second
Count reads from memory2M reads/s, 20,000 reach the store
Write-through every second keeps the cache fresh, so it can answer almost everything.

A like, in order

The like is saved and acknowledged at once; the public count follows within a second.

App
Like API
Like records
Like stream
Counter worker
Gateways
1 PUT /posts/42/like
2 INSERT IF NOT EXISTS
3 inserted
4 +1 post 42
5 200 liked, count + 1
6 events for 42
7 42 = 1.23M (via pub/sub)
8 push 1.23M
RequestResponseAsync, does not block

How a hot post’s count is updated

The decision this question is really about.

Update per like
Hot row
30,000 likes/s
UPDATE like_count + 1
row lock: queue, timeouts
+ Exact and simple.− One row takes about 1,000 updates/s.
Sharded counter rows
Costly reads
+1 on random row of 64
read = SUM of 64
2M reads → 128M
+ 64 rows take 64,000/s.− Every read sums 64 rows.
Stream, sum per second
Our pick
like row by user+1 event by post
worker sums 30,000
1 write + 1 push / s
+ 1 write per post per second.− About 1 s of lag.

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
A likes table and a count column, updated in one transaction.
Stage 2 · ≈ 20,000 likes/sCount from a stream
App
Like API × N
Like records by userLike stream
Counter workersCount cache
Count store
Likes keyed by user, counts added up once a second, a write-through cache.
Stage 3 · ≈ 100,000 likes/sPush it live
AppGateways
Like API × NPub/sub
Like records by userLike stream
Counter workersCount cacheLikers index
Count store
One coalesced count per post per second to watchers, through gateways.
Was this walkthrough useful?Your vote tells us what to improve next.