WebSockets & Real-Time Systems with Spring · Lektion

Sticky Sessions und WebSocket-Routing

Konfigurieren Sie Load Balancer so, dass zustandsbehaftete WebSocket-Verbindungen mithilfe von Sticky Sessions korrekt weitergeleitet werden, und lernen Sie, wann ein gemeinsamer Broker dies überflüssig macht.

Lektion 4 von 413 Schritte

Sticky Sessions und WebSocket-Routing ist eine kostenlose WebSockets & Real-Time Systems with Spring-Lektion auf CoddyKit. Dies ist Lektion 4 von 4. Du kannst die komplette Lektion unten kostenlos lesen – dann übst du sie direkt im Browser mit einem integrierten Code-Editor und einem KI-Tutor rund um die Uhr. Sie ist Teil des WebSockets & Real-Time Systems with Spring-Lernpfads, und dein Fortschritt wird über Web und CoddyKit-App synchronisiert. Der WebSockets & Real-Time Systems with Spring-Kurs umfasst insgesamt 4 Lektionen.

Teile dieser Lektion wurden noch nicht übersetzt und werden auf Englisch angezeigt.

WebSockets Are Long-Lived

Unlike a stateless HTTP request, a WebSocket is a persistent connection pinned to one server instance for its entire life. This breaks the assumption load balancers make about HTTP.

The Routing Problem

If the in-memory simple broker holds a user's subscriptions on instance A, a message that lands on instance B has nowhere to deliver. The connection state lives only where the socket terminated.

Sticky Sessions (Session Affinity)

Sticky sessions make the load balancer always route a given client to the same backend instance. This keeps the socket and its server-side state co-located.

Affinity by Cookie

Cloud load balancers commonly implement affinity with a cookie they set on the first response and read on later requests.

# AWS ALB target group attribute
stickiness.enabled = true
stickiness.type = lb_cookie
stickiness.lb_cookie.duration_seconds = 86400

Forwarding the Upgrade

The load balancer must support and forward the HTTP Upgrade handshake. Most modern L7 balancers (ALB, GCP HTTPS LB, Nginx) do, but it must be enabled.

# Nginx
proxy_http_version 1.1;
proxy_set_header Upgrade $http_upgrade;
proxy_set_header Connection "upgrade";

Idle Timeouts Bite WebSockets

Load balancers close connections idle beyond a timeout (ALB default 60s). A mostly-quiet WebSocket gets dropped unless you send heartbeats or raise the idle timeout.

idle_timeout.timeout_seconds = 300

Health Checks and Draining

During a deploy, instances are removed. Enable connection draining so in-flight WebSockets finish (or clients reconnect) instead of being cut abruptly.

The Better Answer: Shared Broker

Sticky sessions are a workaround. A cleaner design uses a shared external broker (the STOMP relay): any instance can deliver to any subscriber, so affinity is no longer required for routing.

Combining Both

Even with a shared broker, sticky sessions can still help by keeping a client on one instance to avoid mid-stream reconnects. Use them as an optimization, not a correctness crutch.

Kubernetes Ingress Notes

On Kubernetes, the ingress controller needs WebSocket support and affinity annotations. Nginx ingress, for example, uses cookie-based affinity.

nginx.ingress.kubernetes.io/affinity: cookie
nginx.ingress.kubernetes.io/proxy-read-timeout: "300"

Scaling Out Safely

To add capacity, scale instances behind the balancer and rely on the shared broker for fan-out. Keep sessions evenly spread and use draining on scale-down.

Quick Check

Test your routing knowledge.

Recap

You routed WebSockets correctly:

  • WebSockets are long-lived and pinned to one instance
  • Sticky sessions (cookie affinity) keep a client on its instance
  • The balancer must forward the Upgrade header and tolerate idle WebSockets
  • Use connection draining on deploys
  • A shared broker removes the routing need; affinity then becomes an optimization
Kostenlos starten

Lerne WebSockets & Real-Time Systems with Spring mit einem KI-Tutor — kostenlos

Schreibe und führe echten Code in deinem Browser aus, bekomme sofortige Hilfe von einem 24/7 KI-Tutor und setze dein Lernen im Web oder in der App fort.

Kurse
12
Lektionen
48

Häufig gestellte Fragen

Ist die Lektion „Sticky Sessions und WebSocket-Routing“ kostenlos?

Ja — der vollständige Text von „Sticky Sessions und WebSocket-Routing“ ist hier im Web kostenlos zu lesen. Um sie interaktiv zu üben (integrierter Code-Editor und 24/7 KI-Tutor) und den Rest des WebSockets & Real-Time Systems with Spring-Kurses freizuschalten, upgrade auf CoddyKit PRO. Der WebSockets & Real-Time Systems with Spring-Kurs umfasst insgesamt 4 Lektionen.

Was lerne ich in „Sticky Sessions und WebSocket-Routing“?

Konfigurieren Sie Load Balancer so, dass zustandsbehaftete WebSocket-Verbindungen mithilfe von Sticky Sessions korrekt weitergeleitet werden, und lernen Sie, wann ein gemeinsamer Broker dies überflüs… Du übst WebSockets & Real-Time Systems with Spring mit praktischem Code, den du direkt im Browser ausführst, und ein 24/7 KI-Tutor beantwortet deine Fragen während du die Lektion bearbeitest.

Brauche ich Erfahrung, um WebSockets & Real-Time Systems with Spring zu starten?

Keine Vorkenntnisse erforderlich. WebSockets & Real-Time Systems with Spring auf CoddyKit ist für Anfänger bis fortgeschrittene Lernende strukturiert, sodass du hier starten oder von Anfang an beginnen und in deinem eigenen Tempo voranschreiten kannst. Dies ist Lektion 4 von 4.

Wie lange dauert die Lektion „Sticky Sessions und WebSocket-Routing“?

Die meisten CoddyKit-Lektionen dauern etwa 5–10 Minuten. Jede ist kompakt und interaktiv, sodass du stetig Fortschritte machst und genau dort weitermachst, wo du aufgehört hast – im Web und in der App.

Kann ich in dieser WebSockets & Real-Time Systems with Spring-Lektion Code schreiben und ausführen?

Ja. Jede WebSockets & Real-Time Systems with Spring-Lektion enthält einen integrierten Code-Editor, sodass du echten Code direkt in deinem Browser schreibst und ausführst und sofort KI-Feedback erhältst — ohne lokale Einrichtung erforderlich.

Alle Lektionen in diesem Kurs

  1. WebSockets in Microservice-Architekturen
  2. Strategien für die Cloud-Bereitstellung (AWS/GCP)
  3. Lastverteilung und Hochverfügbarkeit
  4. Sticky Sessions und WebSocket-Routing
← Zurück zu WebSockets & Real-Time Systems with Spring