Highlights

Contents

Part VI
06

Fan-out and slow consumers

We count the repeated work after a single event. Fast producers can fill queues behind slow recipients and destabilize healthy viewers. We will compute fan-out, choose batching, bound per-client queues, and isolate hot topics.

CHAPTER ATLAS / FOLLOW THE READING PATHCHAPTER ATLAS / FOLLOW THE READING PATH1Real-time requirements,ordering2Polling and long polling3SSE and WebSockets4Connections, heartbeats,presence5Pub/sub to WebSocketgateways6Fan-out and slowconsumers7Reconnect and replay8Client state andacknowledgements9Case study: live scoreupdates

#Section 21One event multiplies by its recipients

Heron’s twenty events per second each reach fifty thousand viewers. Fan-out is the replication of one input into deliveries to many recipients. The calculation is F=e×vF=e\times v, read as deliveries per second equal events per second times viewers. It gives 1,000,000 deliveries per second.

Payload bandwidth is B=e×v×mB=e\times v\times m, where mm is payload bytes per event. At 200 bytes, the result is 200,000,000 bytes per second or 1,600,000,000 bits per second before framing, TLS, transport, retransmission, and other traffic. A gateway with ten thousand viewers handles 200,000 deliveries and 40,000,000 payload bytes per second.

20 EVENTS/S × 200 B: 20,000 B/S TO GATEWAYS, 200,000,000 B/S TO VIEWERS20 EVENTS/S × 200 B: 20,000 B/S TO GATEWAYS, 200,000,000 B/S TO VIEWERSbroker20,000 B/s50,000viewers200,000,000 B/s(one gateway: 40,000,000)
Figure 16.21Computed illustrative payload bandwidth. Bars compare different delivery boundaries explicitly; protocol overhead is excluded.

A tiny event rate can therefore produce a network-bound system. Zero-copy techniques or shared encoding can reduce CPU, but each recipient still needs bytes over its connection. Multicast assumptions do not apply automatically to ordinary internet clients. The architecture must spread final egress and retain enough capacity after a gateway fails.

#Section 22Batch when the latency contract permits it

Five consecutive events can be grouped into one application message. Batching combines multiple logical records into one transport operation. At twenty events per second with evenly spaced illustrative arrivals, waiting for a five-event batch makes the first event wait at most 4/20=0.24/20=0.2 seconds before the fifth arrives.

The payload is 5×200=1,0005\times200=1,000 bytes under the stated sizes, before a batch envelope. Batching reduces per-message framing and scheduling overhead but does not reduce the sum of event payloads. A size-or-time flush rule bounds waiting when arrivals slow down. Without the timer, a quiet match could leave an incomplete batch waiting indefinitely.

BATCH 5 EVENTS INTO ONE 1,000 B MESSAGE; THE FIRST ONE WAITS 0.2 SBATCH 5 EVENTS INTO ONE 1,000 B MESSAGE; THE FIRST ONE WAITS 0.2 S00.050.10.150.20.25 sfirst event waits 4 ÷ 20 s = 0.2 s5 × 200 Bfewer frames, same payload; a flush timer bounds quiet periods
Figure 16.22Computed illustrative evenly spaced batching trace. The time bound relies on the stated arrival pattern unless a flush deadline enforces it.

The batch envelope should carry enough per-event identity and ordering data for the client to apply the same reducer rules as individual messages. A repeated batch is not a new group of changes. If one item cannot parse, the recovery protocol must explain whether the entire batch is rejected or how valid item progress is acknowledged without hiding a gap.

Coalescing keeps the latest state instead of every intermediate event. It can reduce payload work when spectators only need the current score, but loses intermediate display states. Never coalesce an audit stream or command history whose individual events matter. Batching preserves all included events; coalescing changes which information is delivered.

#Section 23Bound the queue of each slow recipient

A viewer’s link drains 1,000 bytes per second while incoming events require 4,000 payload bytes per second. The outbound queue grows at 3,000 bytes per second. At a sixty-thousand-byte queue limit, the illustrative queue fills in twenty seconds. Larger buffers delay failure but cannot fix a sustained production rate above drain rate.

Backpressure communicates or enforces a limit when downstream work cannot keep pace. For broadcast, one slow viewer should not force every healthy viewer to stop receiving updates. Each connection needs an independent queue budget and a policy at that budget. Options include coalescing replaceable state, dropping explicitly lossy updates, or disconnecting with a recoverable cursor.

A SLOW RECIPIENT FILLS ITS BOUNDED OUTPUT BUFFERA SLOW RECIPIENT FILLS ITS BOUNDED OUTPUT BUFFERgatewayslow viewer4,000 B/s60,000 B buffer1,000 B/s4,000 − 1,000 = 3,000 B/s growth60,000 ÷ 3,000 = 20 s until full
Figure 16.23Computed illustrative slow-client queue growth. Sizes count event payload and exclude framing.

Record queue age as well as bytes. A queue with little data may still contain old messages if events are rare. The overflow policy depends on whether the payload is a complete state or required deltas. Disconnecting a delta consumer is safe only when reconnect can recover the missed changes or fetch a compatible snapshot.

#Section 24Hot topics need their own budget

Three matches have 30,000, 15,000, and 5,000 viewers in the illustrative split. At twenty events per second each under this separate sizing exercise, their fan-out rates are 600,000, 300,000, and 100,000 deliveries per second. The popular topic’s work dominates even when match counts are balanced.

A hot topic creates disproportionate delivery demand for one logical channel. Gateways can spread its clients across machines, while the producer remains match-local ordered. An intermediate routing tier or hierarchical broker topology may spread delivery work, but it adds another failure and latency boundary. Introduce it when measured topic fan-out exceeds the simpler path’s capacity.

THREE MATCHES, VERY UNEQUAL WORK: 600,000 vs 300,000 vs 100,000 DELIVERIES/STHREE MATCHES, VERY UNEQUAL WORK: 600,000 vs 300,000 vs 100,000 DELIVERIES/S30,000 viewers600,00015,000 viewers300,0005,000 viewers100,00020 events/s each; together still 1,000,000 deliveries/s; route by work
Figure 16.24Computed illustrative per-topic fan-out under a twenty-event-per-second assumption for each topic. This separate split sums to the same million deliveries.

Admission controls protect healthy topics when one match becomes overloaded. Reserve or fairly allocate gateway CPU, outbound bytes, and registration work. A connection limit alone cannot express the difference between an idle spectator and a spectator receiving a busy stream. Observe the resources responsible for the product delay.