What are the memory implications of maintaining many WebSocket connections?

Answer

Each WebSocket connection consumes memory for: (1) TCP socket buffers — typically 4–8KB send + 4–8KB receive kernel buffer per connection (configurable via net.core.rmem_default); (2) Application-level state — user session data, room memberships, message queues — this varies wildly by application (1KB–100KB+ per connection); (3) Node.js event emitter — the WebSocket object itself plus the socket event listener overhead (~few KB). A practical guideline: on a 1GB Node.js process with minimal per-connection state, expect ~10K–50K concurrent connections before memory pressure becomes an issue. To maximize connection density: minimize per-connection memory (share room/user data efficiently), avoid storing large buffers per connection, use binary encoding over JSON, set --max-old-space-size appropriately. Monitor with process.memoryUsage() and heap profiler. Alternatively, Elixir/BEAM handles millions of lightweight processes (actors) because each process has a much smaller overhead (~1–2KB) than a Node.js connection object.