PART 2 · MODULE S5 · PRODUCTION SYSTEM

Federated Subscriptions & Real-Time Event Fan-Out

Scale 250K+ concurrent GraphQL subscriptions across federated subgraphs using stateless HTTP Callbacks, router-side 1:N multiplexing, Kafka/NATS event bridges, and conflating backpressure rings.

Act I · Scale Physics & Fan-Out Math

Socket Memory, 1:N Multiplexing & Stateless Drain Physics

Act 1 of 5
Rust Tokio Router Pod
250K Concurrent WS

~18 KB/socket ≈ 4.5 GB heap per node with zero-copy broadcast channels and bounded per-client ring buffers.

Router Stream Deduplication
1 : 10,000 Multiplex

Collapses identical (opHash, varsHash) watchers into 1 upstream stream and 1 shared _entities enrichment call.

Broker → Enrich → Client
< 35 ms p99 Fan-Out

Kafka/NATS event dispatch + micro-coalesced Shipping._entities join + multiplexed WebSocket frame push.

Stateless HTTP Callback
0% Restart Drops

Subgraph pods drain and roll freely without severing long-lived client WebSockets held at the Router tier.

Act II · Wire Protocols & Federation Contracts

HTTP Callback Protocol & Federated Subscription SDL / Config

Act 2 of 5
HTTP Callback Protocol (Router ↔ Orders Subgraph)
Federation v2 Subscription SDL & Router Config (YAML)
Act III · Interactive 6-Node Federated Subscriptions & Event Fan-Out Simulator

1:N Multiplexer, HTTP Callbacks & Backpressure Chaos Lab

FAN-OUT NOMINAL · 0% DROPS
Protocol:
Multiplexing:
Backpressure:
Active Client WS 10,000 graphql-transport-ws / SSE
Upstream Subgraph Streams 1 Stream (1:10,000) Orders + 1 Shipping join
Fan-Out p99 Latency 28 ms SLO < 45 ms end-to-end
Router Heap / Client Drops 4.4 GB · 0% Drops Ceiling ≤ 6.0 GB · Stable
Subscription Pipeline · WS Handshake & Multiplexing
PHASE 1 · MULTIPLEX
Apollo Router (Multiplexer) 1:N Stream Deduplicator & Conflating Ring Fan-Out
Live Wire Frame Inspector POST /callback/sub_8f2a
Per-Client Backpressure & Buffer Fill Conflate Ring (Cap 64)
Chaos Switchboard
0 Active
Live Fan-Out Event Console
Act IV · Auto-Synced Zero-Dependency TypeScript Reference Blueprints

Multiplexer, HTTP Callback Verifier & Entity Enrichment Fan-Out

Live-Injected Simulator State
Act V · Production Stack Mapping, Architectural Tradeoffs & SLOs

Realtime Ecosystem, Transport Tradeoffs & Production SLOs

Act 5 of 5
Production Stack Mapping
6 Realtime Subscription Layers
Layer / Role Production Standard Operational Responsibility
Federated Subscription Router Apollo Router (Rust) / Cosmo Router Terminates 250K+ client sockets, deduplicates identical streams (1:N), and runs entity enrichment.
Client Transport Protocol graphql-transport-ws (WS) / GraphQL-over-SSE Bidirectional WebSockets for high-frequency streams or HTTP/2 SSE for firewall-friendly unidirectional push.
Router ↔ Subgraph Protocol Apollo HTTP Callback Protocol (Stateless) Replaces fragile upstream WebSockets with stateless HTTP POST callbacks surviving rolling pod restarts.
Event Backbone Apache Kafka / NATS JetStream Decouples mutation writers from subscription publishers with durable partitioned event streams.
Cursor Replay & Presence Redis Streams / Valkey Buffers recent event IDs so reconnecting mobile clients resume cleanly without missing state transitions.
Realtime Telemetry OpenTelemetry Metrics + Prometheus WS Gauges Tracks active socket count, upstream mux ratio, ring conflations, and per-pod heap headroom.
Tradeoff 01 Upstream Transport

HTTP Callback vs. Upstream WebSocket

HTTP Callback (Stateless)

Subgraphs stay stateless—Kubernetes rolling deploys drain pods cleanly with 0% client WS drops and standard HTTP load balancing.

Upstream WS (Stateful)

Slightly lower framing overhead per tick, but every subgraph pod restart snaps thousands of stateful pipes and triggers reconnect storms.

Verdict Default to Apollo HTTP Callback Protocol between Router and subgraphs so client WebSockets stay isolated from backend deploys.
Tradeoff 02 Client Transport

WebSockets vs. Server-Sent Events (SSE)

GraphQL-over-SSE (HTTP/2)

Multiplexes over existing HTTP/2 connections, passes corporate proxies effortlessly, and natively supports Last-Event-ID resume.

graphql-transport-ws

Full-duplex framing ideal for interactive collaborative sessions and multiplexing many subscriptions on one TCP socket.

Verdict Prefer SSE over HTTP/2 for unidirectional status/order feeds; use graphql-transport-ws when clients multiplex 10+ live subscriptions.
Tradeoff 03 Fan-Out Enrichment

Event-Time Entity Enrichment Caching

Micro-Coalesced Enrichment

Router calls Shipping._entities once per (orderId, eventVersion) and fans the enriched JSON out to all 50,000 watchers.

Per-Watcher Enrichment

Simple to implement without muxing, but 50,000 watchers on a viral order trigger 50,000 redundant _entities calls per tick.

Verdict Always run router-side enrichment once per deduplicated event before cloning the serialized frame to subscriber rings.
Tradeoff 04 Backpressure Policy

Conflation vs. Lossless Ordered Delivery

Conflate Latest (Ring)

Overwrites stale intermediate GPS/price ticks for stalled mobile sockets, capping Router heap at ≤ 6.0 GB with zero OOM kills.

Lossless Ordered Queue

Required for chat messages or ledger deltas, but must enforce a hard max-queue limit and disconnect slow consumers to resume via Redis Streams.

Verdict Use Conflate Latest for state snapshots (location, pricing, status) and bounded queues + Redis cursor replay for event logs.
Production Realtime Subscription SLOs
Target Reliability Envelope
WS Availability 99.99% Stateless callback isolation
Fan-Out p99 < 45 ms Broker → enrich → client push
Rolling Restart Drops 0% Zero client WS disconnects
Pod Heap Ceiling ≤ 6.0 GB Conflate-latest ring cap (64)