# Backpressure and Slow Consumers — WebSockets & Server-Sent Events

Source: https://www.skillbyai.com/en/realtime-web/rt-backpressure

> Detect slow clients with bufferedAmount, and apply dropping, coalescing or disconnecting policies.

## When the server produces faster than a client reads

`send()` never blocks; it queues data in memory until the network can take it. If a client is on a weak 3G link and the server pushes 50 price ticks a second, that client's queue grows without limit, and a few such clients can exhaust server memory. This is a **backpressure** problem. In `ws` (and the browser API), `socket.bufferedAmount` shows the bytes queued for a socket. Decide a **policy** per message type. For *latest-value* data, such as prices, cursor positions or a rider's location, **drop or coalesce**: send one snapshot every 100-250 ms containing only the newest values. For must-deliver data, such as chat messages, **disconnect** the slow client with a clear close code and let it resume from its last sequence number once it reconnects. Never let one slow consumer slow down a broadcast loop for everyone else; checking the buffer before each send keeps the loop fast.

## Shedding load for slow sockets

Ticks are coalesced into a snapshot every 250 ms. A socket with more than 1 MiB queued gets droppable updates skipped; for must-deliver data it would be closed with 1013 (Try Again Later).

```js
const HIGH_WATER = 1 * 1024 * 1024; // 1 MiB queued for one client is too much

function sendOrShed(ws, payload, { droppable }) {
  if (ws.bufferedAmount > HIGH_WATER) {
    if (droppable) return false;           // e.g. a stale price tick: skip it
    ws.close(1013, 'client too slow');     // 1013 = Try Again Later
    return false;
  }
  ws.send(payload);
  return true;
}

// Latest-value-wins: coalesce many updates into one send per client.
const latest = new Map(); // symbol -> price
function onTick(symbol, price) {
  latest.set(symbol, price);
}

export function startFlushing(wss) {
  return setInterval(() => {
    if (latest.size === 0) return;
    const snapshot = JSON.stringify(Object.fromEntries(latest));
    latest.clear();
    for (const ws of wss.clients) sendOrShed(ws, snapshot, { droppable: true });
  }, 250);
}

export { onTick };
```

## The overflowing water tank

Filling a tank faster than the outlet drains it ends with water on the floor. You can either fill less often (coalesce), let some overflow away harmlessly (drop stale updates), or shut the valve for that tank (disconnect).

## Coalescing also saves battery

Sending 4 combined updates per second instead of 50 separate ones cuts CPU, bandwidth and mobile radio wake-ups. Users rarely notice the difference for prices or map pins, but they do notice a hot phone.

**Quiz:** A live cricket page sends ball-by-ball commentary and a rapidly changing win-probability number. Which policy fits the win-probability updates for slow clients?

- [ ] Queue every update forever
- [x] Coalesce and send only the latest value periodically
- [ ] Disconnect the client after every update
- [ ] Switch the client to short polling permanently

*Answer:* Coalesce and send only the latest value periodically. Only the newest probability matters, so older values can be dropped or merged. Commentary, by contrast, should be delivered completely.
