Stackbook Logo
distribution-communicationestablished · high operational burden

WebSockets

Also known as: websocket, full-duplex-channel

Intent

Provide a persistent bidirectional channel between client and server for low-latency interactive messaging.

Problem

Interactive applications such as chat, collaboration and games need both sides to send messages at any time. HTTP request-response forces polling or workarounds that add latency and overhead.

Forces

  • •Both parties initiate messages
  • •Low latency and small per-message overhead
  • •Connections are stateful and long-lived
  • •Scaling requires routing messages to the right node

Solution

Overview

WebSockets upgrade an HTTP connection into a persistent, full-duplex channel. Either side can send frames at any time with just a few bytes of overhead, which suits interactive features that would be clumsy over request-response.

How it works

The client sends a GET with Upgrade: websocket; the server replies 101 Switching Protocols. From then on both sides exchange frames.

client                    node A        backplane      node B
  |--- upgrade ---------->|
  |<-- 101 ---------------|
  |--- msg to room 7 ---->|--publish---->|--deliver--->| other users

Since a user is connected to one node, messages for room 7 are published to a backplane, and each node forwards to its local subscribers. Authenticate during the handshake, then authorise each message. Track a sequence number per stream so a reconnecting client can ask for messages after the last one it saw.

Pitfalls

  • No heartbeats. Half-open connections look alive; send ping frames and drop silent peers.
  • Rolling deploys that kill thousands of sockets at once. Drain gradually and add jittered reconnect delays.
  • Unbounded send queues for slow clients. Apply backpressure or disconnect them.
  • Assuming delivery guarantees. Add ids, acknowledgements and idempotent handling.
  • Per-connection memory and file descriptor limits; load test connection counts.

Key takeaways

  • Use for genuinely bidirectional, low-latency traffic.
  • Scale with a pub/sub backplane, not sticky assumptions.
  • Build heartbeats, resume and backpressure yourself.
  • Prefer SSE or webhooks when one-way is enough.
  • Measure it in production: track latency, error rates and volume for this pattern, and review the numbers regularly so that limits and timeouts stay matched to real traffic.

✓ When to Use

  • •Chat, multiplayer, collaborative editing
  • •High-frequency bidirectional updates
  • •Binary or mixed payloads over a single connection

✗ When Not to Use

  • •Simple server-to-client notifications that SSE handles
  • •Infrequent updates where polling or webhooks suffice
  • •Environments where proxies block upgrades

Pros

  • +Full duplex with low overhead
  • +Text and binary frames
  • +Single connection replaces many requests

Cons

  • −Stateful connections complicate scaling and deploys
  • −No built-in reconnect, ordering across reconnects or delivery guarantees
  • −Bypasses HTTP caching and standard tooling

Cost Profile

Infrastructure

Medium - connection-holding servers and backplane

Operational

High - state, deploy draining, monitoring

Cognitive

Medium - protocol and lifecycle design

Failure Modes

  • Silent dead connections without heartbeats

  • Reconnect storm after deploy or node loss

  • Messages lost or duplicated across reconnects

Real-World Examples

Alternatives

    Related Patterns

      Competency Domains

      distribution communicationreliability ops