Linora

0020. Realtime tier: Node 26 + uWebSockets.js, gateway/owner threads; capacity for 100k+ connections

Status: Accepted (2026-11-27). Supersedes the realtime part of 0009.

Context

Target: 100k+ concurrent WebSocket connections per instance on modest hardware, low memory and CPU, fast p99. Assumed box: 8 vCPU / 16 GB Linux, TLS terminated at the load balancer, private network to Postgres. Contracts must stay single-sourced with the TypeScript clients.

Decision

Runtime: Node 26 + uWebSockets.js (Apache-2.0; C++ uWS core with epoll, native pub/sub, per-socket backpressure, corking), pinned to an exact release tag with a checksum check in CI (it ships prebuilt binaries from GitHub, not npm).

Process shape (one process per box, apps/realtime):

Hot path rules: binary frames only; gateways scan in place over uWS’s ArrayBuffer (copy only the accepted byte ranges into the ring); one encode per broadcast, same buffer for every subscriber; presence coalesced per tick; connection state in typed-array tables indexed by connection id (no per-message objects); uWS maxBackpressure with a soft limit (stop presence) and a hard limit (close slow_consumer).

Rejected options (per idle connection / per message)

| Option | Idle socket (user space) | Per-message cost | Why not | |—|—|—|—| | Node + uWS.js (chosen) | ~1 KB (uWS ~0.5 KB + JS userData + conn table slot) | ~0.8 us recv incl. JS callback; native fan-out ~0.4 us/socket | - | | Node + ws | ~20-40 KB (net.Socket, buffers, JS objects) | JS framing; fan-out in JS | 100k sockets = 2-4 GB heap, long GC pauses, single-core fan-out | | Bun (uWS-based) | ~1 KB | similar to uWS.js | less mature worker_threads/SAB/Atomics.waitAsync support for long-running servers; revisit later | | Rust (tokio + fastwebsockets) | ~1-2 KB | ~0.1-0.3 us, no GC | best raw numbers, but a second language: the wire codec, ScanBatch and StructIndex rules would be duplicated or code-generated, slowing every change. The gateway speaks the same binary protocol, so it can be replaced later without client changes if profiling demands it | | Go (gobwas/ws + netpoll) | ~2-4 KB (typical gorilla: 20+ KB) | ~0.3-0.5 us, GC | same duplication cost as Rust, less memory-efficient |

Capacity (estimates to verify with WP-H load test)

Load model at 100k connections: 20,000 small rooms x 3 users (60k), 1,000 medium rooms x 30 (30k), 10 large rooms x 1,000 viewers (10k), so 21,010 rooms. 20% of connections are active editors (cursor at 20 Hz, 2 op batches/s of ~5 ops); 80% view (rare presence). 12,000 rooms had an op in the last 10 minutes (index resident), mean page 8K nodes (median 2K), two of them 682K-node pages.

Memory | Item | Unit cost | Count | Total | |—|—|—|—| | Idle socket (user ~1 KB + kernel TCP/epoll ~3 KB) | ~4 KB | 80,000 | 320 MB | | Active editor socket (+ presence slot, selection, buckets ~1 KB; kernel buffers in flight ~12 KB avg) | ~17 KB | 20,000 | 340 MB | | Room base on owner (membership, presence table 64 B/peer, dedupe, counters) | ~2 KB + 64 B/peer | 21,010 | ~50 MB | | Catch-up rings (<= 256 KB/room; process cap 256 MB) | ~8 KB typical | 21,010 | ~170 MB | | Gateway topic subscriptions | ~100 B/sub | 100,000 | ~10 MB | | Structural indexes (14 B/node) | 112 KB per 8K-node page | 12,000 (+2 x 9.5 MB) | ~1.36 GB (budget 4 GiB) | | Journal commit buffers | <= 1 MiB in flight, <= 64 MiB backlog | 2 owners | ~2 MB typical, 128 MB worst | | SAB rings | 4 MiB | 20 | 80 MB | | V8 isolates | ~40 MB | 8 threads | ~320 MB | | Total | | | ~2.7 GB typical; ~6.5 GB with every cap hit (16 GB box: >2x headroom) |

CPU (8 vCPU) | Work | Rate | Unit | Cores | |—|—|—|—| | WS receive + JS callback | ~456k msg/s | 0.8 us | 0.37 | | Presence decode + slot write | ~416k/s | 0.2 us | 0.08 | | ScanBatch stateless validation | 40k batches, 200k ops/s | 1 us + 0.3 us/op | 0.10 | | Ring hops (ops + presence) | ~0.5M msg/s | 0.3 us | 0.15 | | Owner: structural check, seq, COPY encode | 40k batches/s | 2 us + 0.2 us/op | 0.12 | | Op fan-out (native publish, corked) | ~300k sends/s | 0.4 us | 0.12 | | Presence ticks (merge + publish) | 300k room-ticks/s, ~900k sends/s | 1 us + 0.4 us/send | 0.66 | | GC, timers, misc | | | 0.30 | | User space | | | ~1.9 | | Kernel TCP (~1.3M packets/s each way after corking) | | ~1 us | ~2.6 | | Total | | | ~4.5 of 8 (~56%) |

Latency targets: op broadcast p99 < 50 ms end to end on LAN (10 ms commit window + commit ~3 ms + fan-out); presence p99 < 100 ms; validation <= 300 ns/op p50.

Limits that keep these bounds: shared/src/limits.ts (rings, budgets, send buffers, rate buckets, large-room presence rate 5 Hz with at most 50 relayed cursors plus an others count, 5,000 subscribers per room).

Consequences