It’s not easy to horizontally scale applications with WebSocket !

Ganesh Sahu
3 min readAug 9, 2023

--

Introduction:
In the realm of modern web development, real-time communication has become a crucial aspect of user engagement and interactivity. Websockets have emerged as a powerful solution for enabling instant updates and interactive experiences in applications like chat messengers, collaborative tools, and live data feeds. However, scaling applications that rely heavily on Websockets introduces unique challenges that developers must carefully address. In this article, we’ll delve into the intricacies of scaling Websocket-based applications, exploring the underlying challenges and discussing strategies to overcome them.

The Power and Pitfalls of Web Sockets

Websockets are designed to provide persistent, bidirectional communication channels between clients and servers. This capability is particularly valuable for applications that require real-time interactions and immediate updates. However, this power comes with its own set of pitfalls when it comes to scaling.

Challenge 1: Persistent Connections

Websockets maintain long-lived connections, resulting in increased resource consumption on both the server and the infrastructure components. As the number of connected clients grows, maintaining a large number of open connections can strain server resources and complicate load balancing.

Challenge 2: Statefulness

Websockets introduce a level of statefulness to applications, as servers must track the connections and their associated states. This stands in contrast to the statelessness of REST, making it more complex to distribute state and requests across multiple servers in a load-balanced environment.

Challenge 3: Load Balancing Complexity

Traditional load balancers optimized for short-lived HTTP requests may struggle to efficiently distribute long-lived Websocket connections. Session affinity (sticky sessions) becomes essential to ensure that a client’s connection is consistently routed to the same server.

Challenge 4: Resource Utilization

The nature of Websockets requires more resources to maintain open connections compared to short-lived HTTP connections used in REST. This resource consumption can limit the number of concurrent connections a server can handle.

Strategies for Scaling Websocket-Based Applications

While scaling Websocket-based applications presents challenges, adopting thoughtful strategies can mitigate these issues and pave the way for a scalable and performant system.

Strategy 1: Connection Pools

Efficiently managing and reusing Websocket connections can help reduce the overhead associated with maintaining open connections. Connection pooling libraries and techniques can optimize resource utilization.

Strategy 2: Specialized Load Balancers

Utilizing load balancers specifically designed to handle Websockets can provide session-aware load balancing, ensuring even distribution of connections across multiple servers. These load balancers are equipped to manage long-lived connections effectively.

Strategy 3: Optimized Server Design

Architecting servers to handle a large number of connections efficiently is crucial. Techniques such as asynchronous I/O and event-driven programming can enhance scalability and resource utilization.

Strategy 4: Hybrid Approaches

Consider adopting a hybrid approach that combines Websockets for real-time interactions with REST for other parts of the application. This allows you to leverage the benefits of both approaches and optimize performance and scalability accordingly.

Question : If http uses keep alive header is there a need for web sockets ?

Yes, there is still a need for WebSockets even if HTTP uses keep-alive connections. While both technologies enable efficient communication between clients and servers, they serve different purposes and are suitable for different use cases:

1. HTTP Keep-Alive
— HTTP keep-alive allows multiple HTTP requests and responses to be sent over a single TCP connection, reducing the overhead of establishing new connections for each request.
— It improves the efficiency of HTTP communication, especially for requesting resources like images, stylesheets, and scripts in web pages.
— However, HTTP is still fundamentally a request-response protocol, where the client initiates a request and waits for a response from the server. This can lead to delays in real-time communication and updates.

2. WebSockets
— WebSockets provide full-duplex communication channels over a single TCP connection, enabling real-time, bidirectional communication between clients and servers.
— Unlike HTTP, which follows a request-response pattern, WebSockets allow the server to push data to the client without the client explicitly requesting it. This is crucial for real-time applications like chat, live notifications, and online gaming.
— WebSockets are ideal for scenarios where low-latency communication and instant updates are required.

In summary, HTTP keep-alive is beneficial for improving the efficiency of traditional request-response interactions, such as fetching web page resources. However, when it comes to building real-time applications that demand continuous, low-latency communication and updates (like messaging apps), WebSockets offer a more suitable solution. WebSockets enable the establishment of persistent, bidirectional connections, making them better suited for scenarios where immediate and ongoing data exchange is essential.

--

--

Ganesh Sahu

Senior engineer at VMware.Passionate about building elegant solutions to complex problems.