เซสชันแบบยึดติดและการกำหนดเส้นทาง WebSocket
กำหนดค่าโหลดบาลานเซอร์ให้กำหนดเส้นทางการเชื่อมต่อ WebSocket ที่มีสถานะได้อย่างถูกต้องด้วยเซสชันแบบยึดติด และเรียนรู้ว่าเมื่อใดนายหน้าที่ใช้ร่วมกันช่วยขจัดความจำเป็นนี้
เซสชันแบบยึดติดและการกำหนดเส้นทาง WebSocket เป็นบทเรียน WebSockets & Real-Time Systems with Spring ฟรีบน CoddyKit นี่คือบทเรียนที่ 4 จากทั้งหมด 4 บทเรียน คุณสามารถอ่านบทเรียนทั้งหมดด้านล่างฟรี — จากนั้นลองปฏิบัติด้วยตัวคุณเองในเบราว์เซอร์พร้อมตัวแก้ไขโค้ดในตัวและติวเตอร์ AI ตลอด 24/7 บทเรียนนี้เป็นส่วนหนึ่งของเส้นทางการเรียน WebSockets & Real-Time Systems with Spring และความก้าวหน้าของคุณจะซิงค์ข้ามเว็บและแอป CoddyKit คอร์ส WebSockets & Real-Time Systems with Spring มีบทเรียนทั้งหมด 4 บทเรียน
บางส่วนของบทเรียนนี้ยังไม่ได้รับการแปล และแสดงเป็นภาษาอังกฤษ
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 = 86400Forwarding 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 = 300Health 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
Upgradeheader and tolerate idle WebSockets - Use connection draining on deploys
- A shared broker removes the routing need; affinity then becomes an optimization
คำถามที่พบบ่อย
บทเรียน “เซสชันแบบยึดติดและการกำหนดเส้นทาง WebSocket” ฟรีหรือไม่
ใช่ — ข้อความเต็มของ “เซสชันแบบยึดติดและการกำหนดเส้นทาง WebSocket” ฟรีให้อ่านที่นี่บนเว็บ เพื่อปฏิบัติแบบโต้ตอบ (ตัวแก้ไขโค้ดในตัวและติวเตอร์ AI ตลอด 24/7) และปลดล็อคส่วนที่เหลือของคอร์ส WebSockets & Real-Time Systems with Spring ให้อัปเกรดเป็น CoddyKit PRO คอร์ส WebSockets & Real-Time Systems with Spring มีบทเรียนทั้งหมด 4 บทเรียน
คุณจะเรียนรู้อะไรในบทเรียน “เซสชันแบบยึดติดและการกำหนดเส้นทาง WebSocket”
กำหนดค่าโหลดบาลานเซอร์ให้กำหนดเส้นทางการเชื่อมต่อ WebSocket ที่มีสถานะได้อย่างถูกต้องด้วยเซสชันแบบยึดติด และเรียนรู้ว่าเมื่อใดนายหน้าที่ใช้ร่วมกันช่วยขจัดความจำเป็นนี้ คุณปฏิบัติ WebSockets & Real-Time Systems with Spring ด้วยโค้ดที่ใช้งานได้จริงที่คุณเรียกใช้โดยตรงในเบราว์เซอร์ และติวเตอร์ AI ตลอด 24/7 ตอบคำถามของคุณขณะที่คุณไปผ่านบทเรียน
คุณต้องมีประสบการณ์ก่อนที่จะเริ่มเรียน WebSockets & Real-Time Systems with Spring หรือไม่
ไม่จำเป็นต้องมีประสบการณ์มาก่อน WebSockets & Real-Time Systems with Spring บน CoddyKit ออกแบบมาสำหรับผู้เริ่มต้นไปจนถึงผู้เรียนขั้นสูง คุณสามารถเริ่มต้นที่นี่หรือเริ่มจากตัวแรกและเรียนด้วยความเร็วของคุณเอง นี่คือบทเรียนที่ 4 จากทั้งหมด 4 บทเรียน
บทเรียน “เซสชันแบบยึดติดและการกำหนดเส้นทาง WebSocket” ใช้เวลานานแค่ไหน
บทเรียน CoddyKit ส่วนใหญ่ใช้เวลาประมาณ 5–10 นาที แต่ละบทเรียนจึงสั้นและเป็นแบบโต้ตอบ คุณสามารถก้าวหน้าอย่างต่อเนื่องและกลับมาเรียนต่อจากตรงที่เพิ่งหยุดบนเว็บและแอปได้เลย
ฉันเขียนและรันโค้ดในบทเรียน WebSockets & Real-Time Systems with Spring นี้ได้ไหม
ได้ บทเรียน WebSockets & Real-Time Systems with Spring ทุกบทมีตัวแก้ไขโค้ดในตัว คุณจึงเขียนและรันโค้ดจริงได้เลยในเบราว์เซอร์ และได้รับข้อเสนอแนะจาก AI ในทันที — ไม่ต้องติดตั้งในเครื่องของคุณ
บทเรียนทั้งหมดในหลักสูตรนี้
- WebSockets ในสถาปัตยกรรมไมโครเซอร์วิส
- กลยุทธ์การนำระบบขึ้นคลาวด์ (AWS/GCP)
- การกระจายภาระงานและความพร้อมใช้งานสูง
- เซสชันแบบยึดติดและการกำหนดเส้นทาง WebSocket