Video in progress

A real-time dashboard sends the full state of 2000 rows to every client every second, and egress traffic has jumped. How do you reduce it?

Instead of the full state, a snapshot + delta model: one snapshot on connect, then only the changed fields with a version. On top of that: conflation, the client subscribing only to the part it is looking at, a compact format and compression. A client that notices a version gap asks for the snapshot again.

📖 Answer

Let's do the math: 2000 rows × ~150 bytes = 300 KB, every second, 5000 clients is about 1.5 GB/s. Meanwhile, usually only 20 to 30 rows change in a second.

Snapshot + delta.

{"type":"snapshot","version":18230,"rows":[...]}
{"type":"delta","from":18230,"to":18231,"upsert":[{"id":17,"p":101.2}],"remove":[44]}

The client applies a delta only if from equals its current version. If not, some delta was lost, and the client asks for the snapshot again. This simple rule keeps a wrong state from sitting on the screen for hours.

Conflation. I collect deltas into one packet every 250 to 500 ms. If a row changes 10 times in that window, only its last value goes out. The human eye can't pick up more than 2 to 4 updates a second anyway.

Only the visible part. Out of a 2000-row table, 40 are visible on screen. The client subscribes by filter and page (/topic/board.region-7), or the server keeps a "viewport" for each session. This often gives the biggest win.

Format. Short keys or arrays instead of repeated keys, and binary if needed. permessage-deflate works well on deltas too, but measure its memory cost.

Serialize once. If you send the same delta to everyone, turn it into bytes once and hand the ready-made TextMessage to every session, rather than serializing JSON 5000 times.

A trap: in the delta model the server has to know what changed for each version. If the data arrives from the database as a "full table", the diff against the previous state is computed on the server. That costs CPU, but it's done once, not N times.

The result must show up in metrics: egress per client and the number of snapshot requests. If snapshot requests grow, deltas are being lost, and you go looking for the cause.