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

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
1writer → lb · POST /commentsMia types "what a goal!" and sends it with a client_id. Her app shows it at once.1 / 8

The numbers, at a glance

On the hottest post……viewers see about 1 comment in 600
10 shown live / s6,000 written / s
What sampling and batching saveMessages per second to viewers at 20M viewers
Pub/sub is nearly idleMessages a second with a topic per post
A broadcast to every gateway would need 5M a second, more than the cluster takes.

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.

Clients poll
Fallback only
every 2 s
GET after=…
mostly empty
+ Stateless and simple.− 10M mostly empty requests a second.
Broadcast
Small fleets
comment
→ all 250 gateways
5M msgs/s, 99% dropped
+ No subscriptions.− Comments × gateways messages.
Topic per post
Our pick
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.

Stage 1 · ≈ 10,000 viewersMake it work
Viewers
Comment server
Database
One server holds sockets and saves comments.
Stage 2 · ≈ 1M viewersMany gateways
Viewers
Load balancer
GatewaysComment service
Broadcast busComment store
Gateways hold sockets; a comment service writes; a bus reaches every gateway.
Stage 3 · ≈ 5M viewersTame hot posts
Viewers
Load balancer
GatewaysComment service
Broadcast busSamplerRecent cacheComment store
Sample busy posts to 10 a second; cache the latest comments.
Stage 4 · ≈ 20M viewersRoute and protect
Viewers
Load balancer
GatewaysComment serviceModeration
Pub/sub by postSamplerRecent cacheStore · 16 shards
A topic per post, 16 shards by post, moderation before publish.
Was this walkthrough useful?Your vote tells us what to improve next.