Repository navigation
Description
Problem to Solve
The method stream WebSocket is accepted with WebSocketUpgrade(allowAnyOrigin: true, ...) (serverpod/lib/src/server/server.dart:456-470; the only other setting there is pingInterval). relic upgrades with the dart:io defaults (relic_io/lib/src/adapter/io_relic_web_socket.dart:48, SDK _http/websocket.dart:264-275), which negotiate permessage-deflate with context takeover whenever the client offers it (browsers and web_socket_channel do). Each connection then keeps a zlib deflate and inflate state for its lifetime (roughly 300 KB of native memory by zlib's documented formula, about 3 GB at 10,000 connections; not measured by us), and every message is compressed separately per recipient. For servers with thousands of streaming clients this is mostly memory and CPU spent outside the Dart heap.
ServerpodConfig has websocketPingInterval (serverpod_shared/lib/src/config.dart:101), which next to allowedOrigins (:124) is the only WebSocket setting, but no compression setting, and relic's WebSocketUpgrade has no parameter for it yet (filed as serverpod/relic#(link)).
Today compression with context takeover is negotiated whenever the client offers it; the only way to avoid it is to strip Sec-WebSocket-Extensions in a reverse proxy.
Proposal
apiServer:
websocketCompression: off # off | default | noContextTakeoverAfter relic exposes compression on WebSocketUpgrade, add this config value (YAML and environment) and pass it to the upgrade in server.dart. Default unchanged.
Use Case
All method streams of a client share one WebSocket, so every connected client costs one deflate and one inflate context. We run with a per-instance memory budget of 512 MB and fan one event out to many subscribers, so per-connection native memory and per-recipient compression are what limit the number of clients per instance.
Alternatives
Strip Sec-WebSocket-Extensions in the reverse proxy (nginx proxy_set_header Sec-WebSocket-Extensions "";), which is what we plan to do. It works only where a proxy is in front, and the server cannot express its own choice.
Additional context
- Checked against
serverpod 4.1.0,serverpod_shared 4.1.0,relic2.0.0-rc.2to2.0.0-rc.6and Dart SDK 3.13.7; unchanged in 4.1.1. - The root cause is in relic (the upgrade happens in
io_relic_web_socket.dart:48, Serverpod only creates theWebSocketUpgradeatserver.dart:456), so this request depends on relic issue we wrote - We plan to measure memory per connection with and without deflate, and the CPU per fan-out message.
How experienced are you with this library?
None
Are you interested in working on a PR for this?
- I want to work on this
Metadata
Metadata
Assignees
Labels
Type
Projects
- StatusShow more project fieldsInbox 📫
Activity