Websocket Load Balancing
DodaTech
2 min read
In this tutorial, you will learn about Websocket Load Balancing. We cover key concepts, practical examples, and best practices to help you master this topic.
apiVersion: apps/v1 kind: Deployment metadata: name: websocket-server spec: replicas: 5 selector: matchLabels: app: websocket-server template: metadata: labels: app: websocket-server spec: containers: - name: server image: websocket-server:latest ports: - containerPort: 8080 resources: requests: memory: "256Mi" cpu: "250m" limits: memory: "512Mi" cpu: "500m" readinessProbe: httpGet: path: /health port: 8080 initialDelaySeconds: 5 periodSeconds: 10 livenessProbe: tcpSocket: port: 8080 initialDelaySeconds: 15 periodSeconds: 20
apiVersion: autoscaling/v2 kind: HorizontalPodAutoscaler metadata: name: websocket-server-hpa spec: scaleTargetRef: apiVersion: apps/v1 kind: Deployment name: websocket-server minReplicas: 3 maxReplicas: 20 metrics:
- type: Resource resource: name: cpu target: type: Utilization averageUtilization: 70
Expected output: Kubernetes service with client IP session affinity and HPA for autoscaling WebSocket servers based on CPU utilization.
## Common Mistakes
| Mistake | Explanation |
|---------|------------|
| Using HTTP-only load balancers for WebSocket | Some HTTP load balancers strip Upgrade headers or buffer WebSocket frames |
| Not setting long timeouts | Default HTTP timeouts (30-60s) will kill WebSocket connections prematurely |
| Ignoring connection limits | Each WebSocket connection consumes a file descriptor; plan capacity accordingly |
| Not using health checks for WebSocket | WebSocket health checks need protocol-specific endpoints, not just TCP port checks |
| Overloading a single server | Without proper load balancing, some servers handle all connections while others sit idle |
## Practice Questions
1. Why does round-robin not work well for WebSocket load balancing?
2. How does consistent hashing improve WebSocket scaling?
3. What is the difference between L4 and L7 WebSocket load balancing?
4. How do you configure WebSocket health checks?
5. How does Kubernetes handle WebSocket load balancing?
## Challenge
Set up a horizontally scaled WebSocket application on Kubernetes with HPA, client IP session affinity, and a shared Redis session store. Load test with 10,000 concurrent connections and demonstrate autoscaling behavior under load.
## FAQ
<details style="margin-bottom:12px;border:1px solid #e2e8f0;border-radius:10px;overflow:hidden"><summary style="cursor:pointer;padding:14px 18px;font-weight:600;font-size:1.05rem;background:#f8fafc;border-bottom:1px solid #e2e8f0;color:#1e293b">Can I use DNS round-robin for WebSocket load balancing?</summary><div style="padding:14px 18px;color:#475569;line-height:1.7;background:#fff"><p>DNS round-robin distributes connections at connection time but does not handle server failures or connection draining.</p>
</div></details>
<details style="margin-bottom:12px;border:1px solid #e2e8f0;border-radius:10px;overflow:hidden"><summary style="cursor:pointer;padding:14px 18px;font-weight:600;font-size:1.05rem;background:#f8fafc;border-bottom:1px solid #e2e8f0;color:#1e293b">What is the maximum concurrent WebSocket connections per server?</summary><div style="padding:14px 18px;color:#475569;line-height:1.7;background:#fff"><p>This depends on resources. A typical Node.js server handles 10,000-20,000 connections. Go servers can handle 100,000+ with proper configuration.</p>
</div></details>
<details style="margin-bottom:12px;border:1px solid #e2e8f0;border-radius:10px;overflow:hidden"><summary style="cursor:pointer;padding:14px 18px;font-weight:600;font-size:1.05rem;background:#f8fafc;border-bottom:1px solid #e2e8f0;color:#1e293b">How do I drain WebSocket connections during deployment?</summary><div style="padding:14px 18px;color:#475569;line-height:1.7;background:#fff"><p>Implement a graceful shutdown: stop accepting new connections, notify clients, wait for active connections to close, then shut down.</p>
</div></details>
<details style="margin-bottom:12px;border:1px solid #e2e8f0;border-radius:10px;overflow:hidden"><summary style="cursor:pointer;padding:14px 18px;font-weight:600;font-size:1.05rem;background:#f8fafc;border-bottom:1px solid #e2e8f0;color:#1e293b">Should I use a dedicated WebSocket load balancer?</summary><div style="padding:14px 18px;color:#475569;line-height:1.7;background:#fff"><p>Many organizations use NGINX, HAProxy, or cloud LB services. Choose based on your existing infrastructure and scaling needs.</p>
</div></details>
<details style="margin-bottom:12px;border:1px solid #e2e8f0;border-radius:10px;overflow:hidden"><summary style="cursor:pointer;padding:14px 18px;font-weight:600;font-size:1.05rem;background:#f8fafc;border-bottom:1px solid #e2e8f0;color:#1e293b">How do I handle cross-region WebSocket connections?</summary><div style="padding:14px 18px;color:#475569;line-height:1.7;background:#fff"><p>Use global load balancers with geo-routing to direct clients to the nearest region. Coordinate state across regions using a global pub/sub system.</p>
</div></details>
<details style="margin-bottom:12px;border:1px solid #e2e8f0;border-radius:10px;overflow:hidden"><summary style="cursor:pointer;padding:14px 18px;font-weight:600;font-size:1.05rem;background:#f8fafc;border-bottom:1px solid #e2e8f0;color:#1e293b">What metrics should I monitor for WebSocket load balancing?</summary><div style="padding:14px 18px;color:#475569;line-height:1.7;background:#fff"><p>Monitor connection count per server, connection rate, message throughput, latency, error rates, and server resource utilization.</p>
</div></details>
## Mini Project
Build a WebSocket load testing tool that creates thousands of concurrent connections, measures distribution across servers, and charts connection counts per server over time. Use it to test different load balancing algorithms and find the optimal configuration.
## What's Next
Learn about WebSocket reconnection strategies
Built by the developers of DodaTech
Doda Browser, DodaZIP & Durga Antivirus Pro