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
Like a post
Count and push
A retried like
A worker crashes
Load a feed
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
The numbers, at a glance
100,000 likes/s2M count reads/s
30,000/sPer like
1,000/sRow limit
1/sStream
99% from the cache1% from the store
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.
30,000 likes/s
UPDATE like_count + 1
row lock: queue, timeouts
+ Exact and simple.− One row takes about 1,000 updates/s.
+1 on random row of 64
read = SUM of 64
2M reads → 128M
+ 64 rows take 64,000/s.− Every read sums 64 rows.
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.
App
App server
Database
App
Like API × N
Like records by userLike stream
Counter workersCount cache
Count store
AppGateways
Like API × NPub/sub
Like records by userLike stream
Counter workersCount cacheLikers index
Count store
Was this walkthrough useful?Your vote tells us what to improve next.