SD-48 · Back to the write-up
Design a Live Comments
Save each comment once, publish it to its post's topic, and let gateways copy it to their own viewers, sampling and batching hot posts.
Follow a request
Post a comment
Deliver to viewers
Open a post
A hot post
Reconnect and catch up
Viewers
Edge
Services
Fan-out and data
ViewerWatching the post
WriterPosts a comment
GatewaysWebSockets
Load balancerHTTPS
Comment serviceSave, publish
ModerationBefore publish
Pub/subTopic per post
SamplerHot posts
Recent cacheLast 200
Comment store16 shards
client_id. Her app shows it at once.1 / 8The numbers, at a glance
10 shown live / s6,000 written / s
36B/sEvery comment
74M/sSampled
26M/sBatched frames
45,000 msgs/s2M/s capacity
A comment, in order
A quiet post. On a hot post the sampler sits between pub/sub and gateways and passes 10 a second.
Writer
Comment service
Moderation
Comment store
Cache
Pub/sub
Gateway
Viewer
1 POST /comments
2 check text
3 publish
4 INSERT (quorum)
5 ok
6 push to recent
7 publish post:8812
8 201 { id }
9 comment
10 frame (≤ 500 ms)
RequestResponseAsync, does not block
How a comment finds its viewers
The decision this question is really about.
every 2 s
GET after=…
mostly empty
+ Stateless and simple.− 10M mostly empty requests a second.
comment
→ all 250 gateways
5M msgs/s, 99% dropped
+ No subscriptions.− Comments × gateways messages.
comment
post:8812 → 3 gateways
gateway → its viewers
+ Only gateways that need it.− Subscription churn to manage.
How it grows
Add a box only when a number forces you to. Highlighted boxes are new at each stage.
Viewers
Comment server
Database
Viewers
Load balancer
GatewaysComment service
Broadcast busComment store
Viewers
Load balancer
GatewaysComment service
Broadcast busSamplerRecent cacheComment store
Viewers
Load balancer
GatewaysComment serviceModeration
Pub/sub by postSamplerRecent cacheStore · 16 shards
Was this walkthrough useful?Your vote tells us what to improve next.