Skip to content

Lesson 9 of 9 · 26m

Operating Realtime in Production: Metrics, Abuse, Deploys, and Load Tests

The operational playbook: what to measure, how abuse actually arrives, how to drain connections, and how to load-test before users do it for you.

Realtime systems do not fail like request/response systems. They fail by accumulating: connections, queues, memory, subscriptions, presence entries, and mystery. Operations is where architecture either becomes observable or becomes folklore.

Measure the lifecycle, not just throughput

Core metrics:

Track connections.open by instance and transport, connections.close by close code, messages.in/out by type and bytes, outbound_queue.depth, broker.publish_latency, time_to_first_event, reconnects.rate by client version, and presence.lease.count by room-size bucket.

Close-code distribution is one of the highest-signal realtime dashboards. 1006 spikes mean abnormal network loss. 1011 means server errors. 1001 during deploys is expected — unless it persists afterward.

Logs need correlation IDs for connection, user, room, and protocol version. Do not log every message payload at scale; sample or hash identifiers and keep payloads out of logs unless privacy review says otherwise.

Abuse looks ordinary until it multiplies

Realtime endpoints are attractive because one cheap connection can consume resources for hours. Protect the edges:

Enforce per-IP and per-account connection limits, per-connection and per-room message rate limits, maximum frame and outbound-queue sizes, subscription count limits, and bounded cache TTLs for expensive room authorization checks.

Token abuse matters too. If a query token never expires, a leaked URL is permanent access. Mint short-lived tickets, bind them to user and purpose, and rotate them on reconnect.

Drain before you kill

A production shutdown should look like this:

systemctl stop realtime.service

And inside the service:

Inside the service: mark the instance draining, stop accepting upgrades, send close 1001 or an application server-draining code, stop publishing new work locally, wait for clients to leave up to a deadline, then close the remainder and exit.

Systemd only helps if TimeoutStopSec= is long enough for the drain and KillSignal= behavior is understood. Test the service unit, not just the Node process in a terminal.

Load-test realistic shapes

k6 can open WebSockets, but a thousand connections from one machine with instant consumers is a fantasy test. Model read speed, message mix, room sizes, reconnect churn, and network jitter. Include one hot room and many small rooms.

import ws from "k6/ws";
export default function () {
  ws.connect("wss://staging.example/realtime", {}, function (socket) {
    socket.on("open", () => socket.send(JSON.stringify({ type: "subscribe", room: "loadtest" })));
    socket.setTimeout(() => socket.close(), 30000);
  });
}

Watch server memory after connections close. A system that passes throughput but never releases queues or listeners is still a failure.

Operational realities nobody puts in the tutorial

Corporate proxies break upgrades, extensions block sockets, mobile OSes suspend timers, CDN pricing is often connection-minute based, and certificate renewal happens exactly during mass reconnects. These are not exotic cases; they are Tuesday. Design degradation: if realtime dies, users should still read or post through normal HTTP, and the UI should say degraded instead of lying.

Field notes

  • Alert on symptoms users feel: reconnect rate, time to first event, gap/resync rate — not merely CPU.
  • Keep a manual kill switch for fan-out-heavy features such as typing indicators and presence.
  • Run one small chaos exercise: kill a realtime instance during active traffic and observe client behavior.
  • Write the incident query before the incident: close codes by version, room, ASN, and instance.

Was this lesson helpful?

Sign in to rate