0204. Overload control: bounded backlogs, shed order, retry floors held at the gateway, server-driven backoff
Status: Accepted (2026-11-28). Wave 3, R1. Refines 0201 (ring overflow, send-buffer caps) and 0202 (retry floor). Security review SR-03 (admission against backlog + payload, ids charged per user), SR-10 (buckets survive reconnects).
Context
At 100k connections and ADR 0020 activity (WP-H, 2026-11-27):
- Bug 1, unbounded memory. Gateway overflow lists grew without bound once owners fell behind: 68-79 MB of gateway-to-owner backlog per gateway, 61-136 MB owner-to-gateway, gateway heaps around 600 MB, RSS from 3.5 to 8.9 GB, then an OOM kill. Only OPS had an admission check (32 MiB), and it did not count the new payload.
- Bug 2, livelock. Every
busy made each editor resend its whole in-flight tail (up to 64 batches). Each resend reached the owner and got its own refusal, and the burst tripped the 60-message bucket too: 69M rate_limited in 90 s with zero ops accepted.
Decision
- Admission instead of queueing. The ring overflow list stays for messages that cannot be dropped (ops, ACKs, membership). Producers bound it:
- Gateway, OPS: busy when
backlog + payload > OWNER_BACKLOG_BUSY_BYTES (8 MiB per gateway/owner pair, next to the 4 MiB ring).
- Owner, new batches: busy when any owner-to-gateway backlog is above 8 MiB.
- Owner, room queues: at most 256 waiting batches per room (index loading, dedupe read), then busy.
- Owner, catch-up tails: they pause while a gateway’s backlog is above 4 MiB.
- Shed order: presence first.
- Presence uses
RingWriter.reserveDirect, which writes into the ring or fails, and never queues.
- A gateway keeps an unflushed delta merged in the socket’s slot and retries it next tick (
linora_rt_presence_deferred_total).
- An owner drops a room record a full ring cannot take (
linora_rt_presence_shed_total).
- Sockets above the soft send-buffer cap get no presence; above the hard cap they are closed.
- New: when a gateway’s sockets together buffer more than
SEND_BUDGET_BYTES (256 MiB per gateway), the sweep closes the largest buffers first until the total is under 80%.
- Retry floors live at the gateway, with a probe.
- Owner busy: the owner sends
busy as a binary O2G.BUSY record. The gateway sets the connection’s floor and formats the error itself.
- Floor refusals: after a floor at j, batches above j are refused at the gateway and never reach an owner (
linora_rt_floor_refused_total).
- Probe: when j is resent it travels alone, as the probe. Later batches wait at the gateway (refused with the same code) until j is answered: an ACK, a new busy, or 10 s without an answer.
- The first batch refused behind the probe becomes the new floor. So a client that sent j..k in one burst must resend from there, in order, and nothing can overtake it once j is ACKed.
- Only j itself lifts a floor, at the gateway and at the owner. An older resend, such as a duplicate, does not. Both were ordering holes found by the resend-all regression test.
- So a client that resends its whole tail costs the owner one batch per round, not 64. The owner keeps its own per-(file, clientId) floor for reconnects to another gateway.
- This tightens ADR 0202’s rule (“every batch after j gets the error until j is resent”): batches after j also wait until j is answered.
- Server-driven backoff.
- Per connection,
retryAfterMs = 250 ms x 2^level, jittered to [half, full] and capped at 8 s. Each new floor raises the level; each ACK to the socket lowers it by one.
- The owner’s busy carries a hint: its commit lag, at least 500 ms.
rate_limited from a bucket is at least the time the bucket needs to refill.
- Clients honour retryAfterMs as given. They resend only j, and the rest after j’s ACK (sync-client and load generator updated).
- Buckets.
- The per-connection op burst is
max(OPS_MSGS_PER_SEC, MAX_INFLIGHT_BATCHES) = 64, so a reconnecting client can resend its whole window without tripping the bucket by itself. The refill rate is unchanged.
- Node ids in DELETE / TRANSFORM / SET_GEOMETRY lists and CREATE_BULK / RESTORE blocks are charged to a per-user bucket of
USER_IDS_PER_SEC (2M). Ops stay on USER_OPS_PER_SEC.
- A user whose last socket closes keeps a depleted bucket for 10 s, so reconnecting does not refill it.
- Big batches on the ring. Above 65,536 ids the scan summary is a 16-byte header and the owner re-runs
scanBatch over the op bytes. That is deterministic for the same bytes and clientId. Scan arrays shrink back after big batches.
Alternatives
- Drop ops under overload: breaks every client’s op stream. ACKs and broadcasts are never dropped.
- Pause socket reads (TCP backpressure per socket): uWS.js exposes no read pause for WebSockets. A saturated gateway still falls back to the kernel buffers.
- Silently drop refused batches after a floor: cheaper, but the API’s forwarding connections need a per-clientSeq answer to fail an HTTP request promptly.
- Fixed retryAfterMs: retries arrive in synchronized waves at a fixed period, which is the livelock.
Consequences
- Memory under overload is bounded by configuration: rings, 8 MiB per pair per direction, the send budget, and the room queues.
- The overload regression tests (
test/overload.test.ts):
- A 3 s owner stall keeps the gateway backlog at or under 8 MiB, ops get busy, presence is deferred, and every batch commits afterwards in order.
- With the journal blocked for 6 s, a resend-all client made 186 resends in 4 rounds and only 11 reached the owner. retryAfterMs went 500 -> 942 -> 1862 -> 3913, and it recovered 1.3 s after the lock was released.
- An old client (resend everything) recovers one round slower than a new one, but no longer multiplies server work.