Search code examples
amazon-web-servicesload-balancingamazon-ecsaws-fargate

Connect to the same instance in AWS ECS Fargate Cluster


I'm developing an application where two clients need to connect to the same instance in the cluster over the Websocket. It has to be a Websocket and the session need to be alive for a long period of time (several hours). The question is how do I connect two clients to the same container in ECS Fargate? It is possible to tell both clients which instance to use, I'm just not sure how to route them properly. Do I need to set something in the ALB? Maybe it is possible to do by setting a host header?

Here is the scenario I'm trying to solve:

  • User A connects to Server A.
  • User B connects to Server A.

Once connected to the same instance they start to exchange data between each other only. So it is 1-to-1 communication. Ideally we should avoid any requests delegation as the data exchange might be a pretty intense.


Solution

  • You probably need to enable sticky sessions on the load balancer. Although, in that documentation it says you may not need it, as WebSockets are already sticky:

    WebSocket connections are inherently sticky. If the client requests a connection upgrade to WebSockets, the target that returns an HTTP 101 status code to accept the connection upgrade is the target used in the WebSockets connection. After the WebSockets upgrade is complete, cookie-based stickiness is not used.

    But if you want WebSocket connections to be sticky across multiple WebSocket connections from the same web browser, then enabling sticky sessions on the load balancer will make that happen.