Comentarios en Directo

Chat de vídeo en directo · reacciones

Escrito por Darjus Kazakevic · revisado el

Fan out to servers, not viewers, and send totals instead of every heart.

Viewers of a live stream post comments and tap reactions, and everyone in the room sees them within about 2 seconds. A popular stream has 200,000 viewers in one room. Comments are moderated and replay in sync with the recording.

Almost everyone reads and few write: 200,000 viewers may post 2,000 comments a minute but tap 20,000 hearts a second. Order only needs to be roughly right. A comment a second late is fine; a stalled stream is not.

En esencia — Subscribe each server to the room once and fan out to its own viewers, send reactions as totals, and show each viewer only as many comments as a person can read.

Cuándo recurrir a él

Recurre a él cuando

  • Thousands of people watch the same thing and react at the same time
  • Reading matters more than strict order or permanent history
  • Load spikes when something happens on stream, and must not stall it

Reconsidéralo cuando

  • Conversations are small and each message must be stored in order
  • Each viewer sees different content: push per user instead
  • A few hundred viewers: plain pub/sub to each socket is enough

The components

Comment intake (uses a rate limiter)
Checks a per-user limit (one comment every 2 s, say), runs moderation, stores the comment with the stream's time offset, and publishes it to the room once.
Room fan-out (uses pub/sub)
Each gateway subscribes to a room once, when its first viewer of that stream joins, and writes every comment to its own viewers' connections. Upstream work grows with gateways, not viewers.
Reaction aggregator
Counts taps per room per second and publishes one total, such as "+3,412 hearts", instead of 3,412 events. Each screen animates that many hearts, so the room still looks busy.
Sampling
Above about 10 comments a second nobody can read them all. Each gateway forwards a sample per viewer, always keeping comments from the streamer, moderators and the viewer's friends.
Replay store (uses database sharding)
Comments keyed by stream ID and time offset, so a recording shows each comment at the moment it was written. One range read per minute of video.

Key decisions

Fan out to servers, not viewers
Publish each comment once per gateway that holds viewers of the room, and let that gateway write to its own sockets. Upstream work then grows with servers, not with viewers.
Sample what nobody could read
Past about 10 comments a second the stream is a blur. Forward a sample to each viewer, and never sample out the streamer, moderators or the viewer's friends.
Send reaction totals, not events
Count taps for one second and send one number. 20,000 hearts a second become one message a second per room.
Echo the viewer's own comment at once
Show it locally before the server confirms it. Under load a sampled stream goes unnoticed, but a comment that takes 3 seconds to appear feels broken.

Errores habituales

  • Sending every reaction as its own event. 20,000 taps a second become a million gateway messages a second and drown the comments.
  • Fanning out per viewer from one service. Each comment becomes 200,000 sends from one place, and the whole room lags behind the busiest moment.
  • Ordering by arrival at each gateway. Two gateways show the same two comments in a different order. Stamp the stream offset at intake and sort within a short window.