Search code examples
amazon-web-serviceswebsocketarchitectureamazon-elbaws-application-load-balancer

How to design a system with consistent websockets to clients behind an aws load balancer


I have a scalable number of websocket hosts, for the simplicity of this example let them be a simple ec2 instances, each such host is a websocket server and can have multiple connections connected to.
I also have a scalable number of clients, let's call them C1...Cn and they all have a persistent websocket connection to a websocket server on one of the websocket hosts. There is an aws LB(ELB or ALB) between the clients and the websocket hosts.
How can I design a system, based on aws, that when a new client is added, a new websocket connection is created between this client and a websocket server for the lifetime of this client. All traffic from and to this client should move through this connection.
Also, the websocket hosts group needs to be scalable, in case of high load, a new websocket host should be added and vice versa. enter image description here


Solution

  • An AWS Application Load Balancer, with sticky sessions enabled, will accomplish exactly what you describe.

    The problem is going to be the auto-scaling of your instances. When an instance is removed due to a scale-down, all clients that had a websocket connection to that instance will now have their websocket connection broken, and have to start over with a new connection to a different server. If that is a problem, then you should consider possibly using Amazon MQ (Active MQ) as a websocket broker, which would allow your EC2 servers to be stateless. I've also seen that problem solved via Redis instead of ActiveMQ.