Skip to content

WebSockets behaviour with ALB

0

I have Websocket server in EC2 fronted by an ALB (not NLB) to handle TLS. And Stickiness is not enabled. In this setup, there would be 2 sockets: one from client to ALB and then another from ALB to EC2.

I wanted to understand the behaviour in case of disconnections from ALB to EC2 . For example, if the websocket server is deployed. In this case, is it guaranteed that the ALB will drop the other side socket as well (i.e., ALB<->Client) forcing a client to reconnect??

This is important for me since the websocket is stateful without a db or anything. Each web-server is maintaining certain subscriptions in-memory for the clients. So If the ALB does a transparent reconnect without dropping the client socket, it would break the behaviour.

asked 10 months ago576 views
1 Answer
0

In the setup you've described with WebSocket connections through an Application Load Balancer (ALB) to EC2 instances without stickiness enabled, there are indeed two separate socket connections: one from the client to the ALB, and another from the ALB to the EC2 instance.

When it comes to disconnections between the ALB and the EC2 instance, such as during a deployment of the WebSocket server, the behavior is as follows:

  1. WebSocket connections are inherently sticky, even if you haven't explicitly enabled stickiness at the ALB level. Once a WebSocket connection is established, the ALB maintains that connection to the same target (EC2 instance) for the duration of the WebSocket session.

  2. If the connection between the ALB and the EC2 instance is lost (e.g., due to a server deployment or failure), the ALB will indeed terminate the corresponding client connection. The ALB does not attempt to transparently reconnect or maintain the client connection when the backend target becomes unavailable.

  3. This behavior ensures that the client is made aware of the disconnection and can initiate a new WebSocket connection. When the client attempts to reconnect, the ALB will route the new connection to a healthy target based on its routing algorithm.

  4. The termination of the client connection is crucial for maintaining the consistency of your stateful WebSocket server, where each server instance maintains subscriptions in-memory for its connected clients.

It's important to note that while the ALB will terminate the client connection in case of backend disconnections, you should also implement proper error handling and reconnection logic on the client side. This will ensure a smooth user experience in case of temporary disconnections or server deployments.

Additionally, for WebSocket applications that require maintaining session state, you might want to consider implementing a shared state mechanism (like a distributed cache or database) to allow for more seamless failover between instances. This would help in scenarios where a client reconnects and is routed to a different EC2 instance.
Sources
Edit target group attributes for your Application Load Balancer - Elastic Load Balancing
Using WebSockets to receive messages in Amazon Chime SDK messaging - Amazon Chime SDK

answered 10 months ago

You are not logged in. Log in to post an answer.

A good answer clearly answers the question and provides constructive feedback and encourages professional growth in the question asker.