Batching and coalescing updates
Batching packs many small updates into one send so a fixed cost is paid once; coalescing keeps just the newest value per key. Together they turn a flood of changes into a steady, bounded stream to each consumer.
What it is
Batching groups many small items that would each be sent, written or processed on their own into one unit of work, so the fixed cost per operation (a network round trip, a system call, a request to a broker) is paid once per batch instead of once per item, and small writes that would each have gone out as their own packet can share one. Coalescing goes a step further: when several queued updates concern the same key, it keeps one of them, usually the newest, or merges them, and the rest are not sent at all. Batching keeps each item and changes how they are packaged; coalescing discards items by design, which is fine for a value and wrong for an event.
How a batch closes. Something has to decide that a batch is finished, and a common rule, and Kafka's, is two thresholds, whichever is hit first: a size (bytes or items) and a time bound. The Kafka 4.3 producer collects records headed for the same partition until the batch reaches batch.size (16,384 bytes by default) or linger.ms has passed; the default linger is 5 ms, raised from 0 in Kafka 4.0 because larger batches typically gave similar or lower producer latency. The time bound limits the delay the producer adds on purpose, though Kafka notes that broker backpressure can stretch the effective wait beyond it; the size bound caps how big one batch grows, while the producer's total buffer is a separate setting, buffer.memory. Redis pipelining is the same idea on a request/response protocol: the client sends many commands without waiting for each reply, so the round trip is paid once per pipeline, and the docs suggest about 10,000 commands per pipeline because the server has to queue the replies in memory until the client reads them.
How coalescing picks what to keep. Coalescing needs a key, the thing an update is about: a player's score, a stock's price, a cursor's position. The Diffusion 6.12 manual describes two forms. Simple conflation removes earlier queued messages for a topic when a new one arrives, placing the new one either where the old one sat (replace) or at the end of the queue (append), so the client receives the newest value for that topic. Structural conflation runs a user-supplied operation instead, for example adding two increments, so the one delivered message carries their combined effect. In the manual's example, five queued messages leave as three.
| Layer | Fixed cost per item | How it is batched or coalesced |
|---|---|---|
TCP connection | A packet, often carrying one byte of payload | Nagle's algorithm combines small outgoing writes while earlier data is still unacknowledged |
Request/response client | A round trip and two system calls | Pipelining sends many commands before reading any reply |
Kafka producer | A request to the broker | Records for one partition wait up to |
Game server | A state broadcast to each client | The world advances in fixed steps, 10 a second in one worked example, applying all queued input and broadcasting the new state |
Browser | A repaint |
|
A fixed-step broadcast does both at once. Gabriel Gambetta's multiplayer series describes a server that queues client inputs as they arrive without processing them, advances the world at a low fixed rate, 10 times a second in his example, applies all the queued input in that step, and then broadcasts the new state. The inputs are batched into one simulation step, and because a client receives the state as it stands after the step, whatever happened between two steps is coalesced. The client covers the 100 ms gaps by showing other players slightly in the past and interpolating between the last two positions it received.
When to use it
Batch when items are small and frequent and the per-item overhead outweighs the work itself. Redis's docs put it plainly for their case: serving a command is cheap, but the socket read and write around it are expensive, and their pipelining benchmark reached roughly ten times the unpipelined throughput. Coalesce when consumers want the current value of something rather than its history: scores, prices, positions, presence, a progress percentage. If a reader would act the same way on the latest value as on the full sequence, the sequence is overhead.
The payoff multiplies when one change fans out to many consumers. Pushing each of C changes per second to W watchers costs C × W messages a second. Keeping the latest value per key and sending one update per tick costs T × W, where T is a tick rate you choose, and T does not grow when the data changes faster. That is the lever for a popular live view: pick a tick rate a reader can use and let the server absorb the rest. Pair it with deltas (send a watcher the keys that changed since their last update) and the message usually shrinks too, though a tick in which most keys changed is still a large one.
Inside the browser the same idea applies to rendering: Batching UI updates covers collapsing many state changes into one paint. On the read path, Request coalescing and leases is the mirror image: many identical requests collapsed into one fetch.
When not to
Don't coalesce events that each carry meaning. A chat message, an order, an audit record or a signal such as "question revealed" isn't the value of a key; replacing an earlier one with a later one loses it without any error. Batch those if volume demands it, in order, and deliver each one. Merging is safe when the merge is exact, as adding increments is; applying latest-wins to a counter that receives increments is not.
Don't hold an interactive request behind a linger timer. A batch of one that waits for its time bound is pure added latency; Kafka kept the producer's linger at 0 until version 4.0 and raised it to 5 ms on the grounds that the efficiency gains from larger batches typically give similar or lower latency anyway. For a user's own action, a purchase or a submitted answer, send it right away and batch the telemetry around it.
Watch two failure modes the buffer itself creates. Memory: a pipeline that is not flushed grows without bound, which is why Redis suggests sending commands in chunks of around 10,000 and reading the replies between chunks. Starvation: the Diffusion manual warns that with append-mode conflation a topic updating faster than the queue drains can keep being moved to the back and go undelivered; replace-mode, or a fixed tick that sends the current snapshot, avoids that.
And when the stream is a few cheap updates a minute, neither usually helps much: the fixed cost rarely adds up to enough to justify a buffer, a timer and a flush path to test. Bursty or widely fanned-out updates are the exception, so judge by the peak rate times the fan-out rather than the average.
Compared
| Approach | What the consumer receives | What it costs | When it is the right choice |
|---|---|---|---|
Send each update as it happens | Each item, at once | Per-item overhead, multiplied by fan-out | Low volume, or each event matters and latency comes first |
Batch by size or time | Each item, grouped, up to the time bound late | Latency up to the linger; buffer memory | Many small items that must all arrive: logs, metrics, broker writes |
Coalesce, newest wins | The latest value per key | Intermediate values are gone | Readers of state: scores, prices, positions |
Coalesce by merging | One combined value per key | A merge function that must be exact | Increments and deltas whose combination is well defined |
Snapshot at a fixed tick, with deltas | The keys changed since the last acknowledged update, T times a second | Up to one tick of staleness; per-client acknowledgment tracking | Many watchers of one changing state |