Skip to content

fix(realtime): bound the per-connection output buffer - #13415

Draft
loks0n wants to merge 3 commits into
mainfrom
fix/realtime-bound-output-buffer
Draft

loks0n wants to merge 3 commits into
mainfrom
fix/realtime-bound-output-buffer

Conversation

@loks0n

@loks0n loks0n commented Aug 28, 2026 •

Copy link
Copy Markdown
Member

Draft — blocked on a utopia-php/websocket release carrying utopia-php/websocket#31, plus
the composer bump.
The code is otherwise complete.

Root cause fix for repeated Appwrite Cloud realtime OOMKills (fra1: OnCall #1098, #1175, and
further kills on 2026-08-28 and 2026-08-31).

The bug

A websocket client that stops draining makes the Swoole reactor buffer its undelivered frames
in process memory while push() keeps returning true. Swoole caps that at
SW_SOCKET_BUFFER_SIZE — 8MB per connection. For the ~1200 connections a realtime
container holds, that is a 9.6GB ceiling against a 1GiB limit.

Measured in the production image (PHP 8.5.9, Swoole 6.2.2, Alpine), 300 connections that never
recv():

worker  (pid 11)   36,152 →     36,644 kB   flat
reactor (pid  8)   73,848 →  2,344,232 kB   ← +2.27 GB

All of it in the reactor, none in the PHP worker: peak PHP heap 4MB against a 128M
memory_limit. That is why it never looked like a leak and why no application-level change
made any difference.

It was also invisible to the metrics we had. During a real production step the message rate
was 21/s — below a 74/s peak 70 minutes earlier that cost nothing. What moved was bytes
fanned out to clients that could not keep up.

The fix

Pass a per-connection cap at construction. Exposure becomes size × connections, and push()
returns false for a connection that is over — which utopia-php/websocket#31 turns into a
close, so the client reconnects and resubscribes from a known state.

Verified end to end through Server + Adapter\Swoole, 300 non-draining connections, 200
events of 50KB:

cap RSS growth stalled connections still open
0 (Swoole's 8MB) +2269 MB 301 — none shed
512KB +176 MB 1 — all 300 shed

Sizing

512KB × ~1200 connections bounds buffers at ~600MB, which fits a 1GiB limit alongside the
~190MB steady-state baseline. That is the worst case with every connection simultaneously at
the cap; realistically only a slow subset ever is.

The default comes from Adapter\Swoole::DEFAULT_SOCKET_BUFFER_SIZE rather than being repeated
here, so the two repos cannot drift. _APP_REALTIME_SOCKET_BUFFER_SIZE overrides it so the
value can be tuned during an incident without a release — raise it if legitimate slow-link
clients are being disconnected, lower it if containers still run out of memory. Documented in
app/config/variables.php and passed through in docker-compose.yml.

Behaviour change

A client far enough behind is now disconnected instead of having up to 8MB held on its behalf.
For a realtime protocol that is the recoverable failure — it reconnects and resubscribes. The
alternative is silent event loss: in a capped-but-not-closed run, 37500 events vanished from
clients that stayed connected and never learned.

Testing

docker compose config validates. The measurements drive a real Swoole\WebSocket\Server in
the production image — the failure lives entirely in the reactor's handling of a socket that
will not accept writes, which a stubbed server cannot exhibit. There is no e2e coverage for
slow-consumer behaviour in tests/e2e/Services/Realtime/*; adding one needs a client that
deliberately stops reading, which is worth doing separately.

🤖 Generated with Claude Code

Blocked on a utopia-php/websocket release carrying
utopia-php/websocket#31; needs the composer bump before merge.

A client that stops draining makes the Swoole reactor buffer every
undelivered frame in process memory without limit, while push() keeps
returning true. Reproduced in the production image: 300 non-draining
connections fanned 2930MB and grew RSS by 2.27GB, all of it in the
reactor, none in the PHP worker (peak PHP heap 4MB). That is what
OOMKills realtime containers, and it is invisible to every
application-level metric -- during a real step the message rate was 21/s,
below a 74/s peak that cost nothing.

Capping bounds the worst case at size x connections. 512KB against ~1200
connections per container is ~600MB of buffers, which fits a 1GiB limit
alongside a ~190MB baseline. Exposed as
_APP_REALTIME_SOCKET_BUFFER_SIZE so it can be tuned during an incident
without a release: raise it if legitimate slow-link clients are being
disconnected, lower it if containers still run out of memory.

Co-Authored-By: Claude Opus 5 (1M context) <[email protected]>
@github-actions

github-actions Bot commented Aug 28, 2026 •

Copy link
Copy Markdown

✨ Benchmark results

Comparing main (before) → fix/realtime-bound-output-buffer (after).

Metric Before After Change
🚀 Requests/sec 217.39 233.16 🟢 +7.3%
⏱️ Latency P50 82.69 ms 78.14 ms 🟢 -5.5%
⏱️ Latency P95 179 ms 164.91 ms 🟢 -7.9%
Per-scenario breakdown & investigation details

Metrics below reflect the current branch (after). Δ P95 compares against the base.

Scenario P50 (ms) P95 (ms) Requests RPS Δ P95 (ms)
API total 78.14 164.91 14,820 233.16 -14.09
Account 128.69 257.48 780 12.94 -32.48
TablesDB 79.42 143.33 8,060 129.1 -12.69
Storage 65.28 136.1 3,900 64.64 -8.11
Functions 97.06 186.59 2,080 35.14 -20.69

Top API waits (after)

API request Max wait (ms)
tablesdb.rows.delete 387.15
storage.files.create 383.62
account.get 374.71
functions.variables.delete 363.17
tablesdb.rows.create 359.15

loks0n and others added 2 commits August 31, 2026 10:22
Follows utopia-php/websocket#31 moving it from a setter to a constructor
argument: Swoole reads the config once at start(), so a later setter would
silently do nothing.

Co-Authored-By: Claude Opus 5 (1M context) <[email protected]>
The library now defaults the cap to 512KB, so the env override falls back
to Adapter\Swoole::DEFAULT_SOCKET_BUFFER_SIZE rather than repeating the
number here and risking drift.

Co-Authored-By: Claude Opus 5 (1M context) <[email protected]>

This branch has not been deployed

No deployments
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant