That’s why it’s so important to not only understand the concept of load balancing but to also utilize it. Load balancing refers to the act of distributing network traffic across multiple services.
This makes sure that there's not too much load on a single server which could cause it to crash.
There are many benefits to utilizing load balancing. When the load is distributed across multiple services, it helps improve the responsiveness of your application. It also limits the chances of servers crashing as they're not being subjected to loads beyond what they're capable of withstanding.
Are there problems with WebSocket’s load balancing?
Despite its obvious benefits, there remain some concerns with utilizing WebSockets for load balancing.
For example, if a WebSocket-based app is running on two servers and the load balancer is distributing connections evenly across them, it won't distribute any of the existing traffic when a third server is added.
This is because by their very nature, WebSocket connections are persistent so they remain on whatever node that they were first connected to.
This then requires the first two servers to be restarted in the hopes that incoming clients would reconnect and the load will then be distributed across the three servers evenly.
If both the initial servers do restart at the exact same time, the load balancer will have already directed all connections to the third server, meaning that the initial two now have no connections.
The persistent behavior of WebSockets can often take people by surprise even when they're carrying out simple tasks like a rolling upgrade of nodes. Traffic inadvertently ends up being sent to just one node, leading to scalability issues.
How to create a persistent connection with WebSocket
There’s a clear benefit that establishing a persistent connection between the client and the server provides. This can be done in a variety of ways but WebSockets are considered to be the best way to create a persistent connection between a client like a web browser and the server.
A persistent connection allows for two-way data exchange between the client and the server. This results in lower latency and a better use of the available resources.
The WebSockets protocol, defined in specification RFC 6455, allows for a full-duplex communication channel between the client and the server. This channel is initiated from an HTTP(s) request that has an "upgrade" header.
The data is sent in both directions as packets without additional HTTP-requests. This persistent connection is very useful for services like real-time trading systems or online games that require continuous data exchange.
A new WebSocket needs to be created in order to open a WebSocket connection that allows for bidirectional communication. Once the socket has been created, events need to be listened on it.
The events include open, message, error, and close. When a new WebSocket URL is created, it starts connecting immediately. WebSockets can also have additional headers that define extensions and subprotocols.