การกระจายภาระงาน WebSockets
กำหนดค่าเครื่องกระจายภาระงาน เช่น Nginx และ HAProxy ให้จัดการเซสชันแบบยึดติดและการอัปเกรด WebSocket ได้อย่างถูกต้อง
การกระจายภาระงาน WebSockets เป็นบทเรียน WebSockets & Realtime Systems Programming ฟรีบน CoddyKit นี่คือบทเรียนที่ 2 จากทั้งหมด 4 บทเรียน คุณสามารถอ่านบทเรียนทั้งหมดด้านล่างฟรี — จากนั้นลองปฏิบัติด้วยตัวคุณเองในเบราว์เซอร์พร้อมตัวแก้ไขโค้ดในตัวและติวเตอร์ AI ตลอด 24/7 บทเรียนนี้เป็นส่วนหนึ่งของเส้นทางการเรียน WebSockets & Realtime Systems Programming และความก้าวหน้าของคุณจะซิงค์ข้ามเว็บและแอป CoddyKit คอร์ส WebSockets & Realtime Systems Programming มีบทเรียนทั้งหมด 4 บทเรียน
บางส่วนของบทเรียนนี้ยังไม่ได้รับการแปล และแสดงเป็นภาษาอังกฤษ
Scaling WebSockets with Load Balancers
When your WebSocket application grows, a single server might not handle all connections efficiently. Load balancing helps by distributing these connections across multiple servers.
This improves performance, reliability, and allows your application to handle more users. It's a crucial step for high-traffic realtime systems to ensure availability and responsiveness.
The WebSocket Handshake
Unlike regular HTTP connections, WebSockets begin with a special "handshake" process that uses HTTP. Your client sends an initial HTTP request to the server, asking to "upgrade" the connection.
This request includes specific HTTP headers that signal the intent to switch protocols. If the server agrees, it responds with an HTTP 101 Switching Protocols status, and the connection then becomes a full-duplex WebSocket.
Load Balancers & Upgrade Headers
A standard HTTP load balancer might not correctly process the WebSocket upgrade request. It needs specific configuration to properly forward the special HTTP headers essential for the handshake. These headers include:
Connection: UpgradeUpgrade: websocketSec-WebSocket-KeySec-WebSocket-Version
Without proper forwarding, the WebSocket handshake will fail, preventing the connection from establishing.
Sticky Sessions: Essential for WebSockets
Once a WebSocket connection is established, it's persistent. It's often critical that all subsequent messages from a client go to the same backend server that handled the initial handshake and established the connection.
This is known as a "sticky session" or "session persistence." If a client's messages are routed to a different server, the connection will break or behave unexpectedly, as the new server won't recognize the existing WebSocket session.
How Sticky Sessions Work
Load balancers use different methods to ensure sticky sessions:
- IP Hash: The load balancer uses the client's IP address to consistently route them to the same backend server. This is simple but less effective if many users share an IP (e.g., behind a NAT).
- Cookie-Based: The load balancer sets a special cookie in the client's browser. Subsequent requests include this cookie, allowing the load balancer to route them to the correct server. This method is generally more robust.
Nginx: Proxying WebSocket Upgrades
Nginx is a popular, high-performance web server that also excels as a reverse proxy and load balancer. To handle WebSockets, Nginx needs configuration to correctly forward the upgrade headers, ensuring the initial HTTP handshake completes successfully.
The proxy_set_header directives for Upgrade and Connection are crucial. Also, a long proxy_read_timeout is recommended for persistent WebSocket connections.
http {
upstream websocket_backend {
server backend1.example.com;
server backend2.example.com;
}
server {
listen 80;
server_name example.com;
location /ws/ {
proxy_pass http://websocket_backend;
proxy_http_version 1.1;
proxy_set_header Upgrade $http_upgrade;
proxy_set_header Connection "upgrade";
proxy_read_timeout 86400s; # Long timeout for WebSockets
}
}
}Nginx: Implementing Sticky Sessions
To ensure sticky sessions with Nginx, you can use the ip_hash directive in your upstream block. This directive ensures that requests from the same client IP address are consistently routed to the same backend server.
While straightforward, remember that IP hash might not be ideal for all scenarios, especially when multiple users share a single public IP address.
http {
upstream websocket_backend {
ip_hash; # Enables sticky sessions by client IP
server backend1.example.com;
server backend2.example.com;
}
server {
listen 80;
server_name example.com;
location /ws/ {
proxy_pass http://websocket_backend;
proxy_http_version 1.1;
proxy_set_header Upgrade $http_upgrade;
proxy_set_header Connection "upgrade";
proxy_read_timeout 86400s;
}
}
}HAProxy: Basic WebSocket Configuration
HAProxy is another powerful and widely used open-source load balancer, especially known for its high availability and advanced routing capabilities. Configuring HAProxy for WebSockets involves setting the correct operating mode (mode http) and creating rules to identify and route WebSocket traffic.
An Access Control List (ACL) is often used to detect the Upgrade: websocket header and direct traffic to a specific backend server pool.
frontend http_front
bind *:80
mode http
default_backend ws_backend
backend ws_backend
mode http
option http-server-close
acl is_websocket hdr(Upgrade) -i websocket
use_backend ws_servers if is_websocket
default-server inter 1s fall 2 rise 5
backend ws_servers
mode http
balance roundrobin
server web1 192.168.1.1:8000 check
server web2 192.168.1.2:8000 checkHAProxy: Cookie-Based Sticky Sessions
For more robust sticky sessions, HAProxy can insert a cookie into the client's browser that identifies the specific backend server the client is connected to. The client then sends this cookie with all subsequent requests, ensuring they are consistently routed to the same server.
This method provides better stickiness than IP hash, especially in environments where client IPs might change or be shared.
backend ws_servers
mode http
balance roundrobin
cookie SERVERID insert indirect nocache # Insert a cookie
server web1 192.168.1.1:8000 check cookie s1
server web2 192.168.1.2:8000 check cookie s2Check Your Understanding
Which of the following are essential considerations when configuring a load balancer for WebSocket traffic?
Load Balancing WebSockets Recap
In this lesson, we explored the critical aspects of load balancing WebSocket applications:
- WebSockets initiate with an HTTP upgrade handshake, requiring specific headers to be properly forwarded by the load balancer.
- Sticky sessions are vital to ensure a client maintains its persistent connection with the same backend server throughout its lifecycle.
- Load balancers like Nginx and HAProxy can be configured to handle WebSocket upgrades and implement sticky sessions using methods such as IP hash or cookie-based routing.
Mastering these configurations is key to building scalable, resilient, and high-performance realtime systems.
คำถามที่พบบ่อย
บทเรียน “การกระจายภาระงาน WebSockets” ฟรีหรือไม่
ใช่ — ข้อความเต็มของ “การกระจายภาระงาน WebSockets” ฟรีให้อ่านที่นี่บนเว็บ เพื่อปฏิบัติแบบโต้ตอบ (ตัวแก้ไขโค้ดในตัวและติวเตอร์ AI ตลอด 24/7) และปลดล็อคส่วนที่เหลือของคอร์ส WebSockets & Realtime Systems Programming ให้อัปเกรดเป็น CoddyKit PRO คอร์ส WebSockets & Realtime Systems Programming มีบทเรียนทั้งหมด 4 บทเรียน
คุณจะเรียนรู้อะไรในบทเรียน “การกระจายภาระงาน WebSockets”
กำหนดค่าเครื่องกระจายภาระงาน เช่น Nginx และ HAProxy ให้จัดการเซสชันแบบยึดติดและการอัปเกรด WebSocket ได้อย่างถูกต้อง คุณปฏิบัติ WebSockets & Realtime Systems Programming ด้วยโค้ดที่ใช้งานได้จริงที่คุณเรียกใช้โดยตรงในเบราว์เซอร์ และติวเตอร์ AI ตลอด 24/7 ตอบคำถามของคุณขณะที่คุณไปผ่านบทเรียน
คุณต้องมีประสบการณ์ก่อนที่จะเริ่มเรียน WebSockets & Realtime Systems Programming หรือไม่
ไม่จำเป็นต้องมีประสบการณ์มาก่อน WebSockets & Realtime Systems Programming บน CoddyKit ออกแบบมาสำหรับผู้เริ่มต้นไปจนถึงผู้เรียนขั้นสูง คุณสามารถเริ่มต้นที่นี่หรือเริ่มจากตัวแรกและเรียนด้วยความเร็วของคุณเอง นี่คือบทเรียนที่ 2 จากทั้งหมด 4 บทเรียน
บทเรียน “การกระจายภาระงาน WebSockets” ใช้เวลานานแค่ไหน
บทเรียน CoddyKit ส่วนใหญ่ใช้เวลาประมาณ 5–10 นาที แต่ละบทเรียนจึงสั้นและเป็นแบบโต้ตอบ คุณสามารถก้าวหน้าอย่างต่อเนื่องและกลับมาเรียนต่อจากตรงที่เพิ่งหยุดบนเว็บและแอปได้เลย
ฉันเขียนและรันโค้ดในบทเรียน WebSockets & Realtime Systems Programming นี้ได้ไหม
ได้ บทเรียน WebSockets & Realtime Systems Programming ทุกบทมีตัวแก้ไขโค้ดในตัว คุณจึงเขียนและรันโค้ดจริงได้เลยในเบราว์เซอร์ และได้รับข้อเสนอแนะจาก AI ในทันที — ไม่ต้องติดตั้งในเครื่องของคุณ
บทเรียนทั้งหมดในหลักสูตรนี้
- กลยุทธ์การปรับขนาดแนวนอน
- การกระจายภาระงาน WebSockets
- การจัดการสถานะแบบกระจาย
- แบ็กเพลน Pub/Sub ด้วย Redis