Scaling with sticky sessions and websockets

爷,独闯天下 提交于 2019-12-04 11:58:17

问题


Initially we have two AWS EC2 instances with node.js running behind a load balancer with sticky sessions. As the load increases more instances are added.

But we are facing problems with this approach. As out application is mainly for workshops, the load usually increases within a short period of time (workshop start) and every workshop participant has a sticky session with the first two instances and the new ones have almost none. Because of this the performance stays bad.

First thought was: let's disable the sticky sessions. But that destroys our websockets because they need sticky sessions (at least this is what i've read). Another problem is with decreasing load. Instances shut down and socket-connections also get lost.

Is there an approach to shift user-sessions between instances or get websockets work without sticky sessions (maybe with Redis)?


回答1:


The solution was an Application Load Balancer (see comment).

  1. At first we had to disable polling, because this did not work with the rest. This is done by defining the transports manually.

    let ioSocket = io('', {
        path: '/socket.io-client'
        transports: ['websocket']
    
  2. After that we set up a standard application load balancer with two target groups: one for websockets and one for all other requests. The rule for the websocket target group matches a specific path via regex:

  1. Last problem was scaling: if one of the instances shuts down because of lower load on the cluster connections may get lost. This was fixed with a simple reconnect after a disconnect in the client (in our case an angular application):

    [...]
    this.socket.on('disconnect', () => {
        // Reconnect after connection loss
        this.connect();
    });
    [...]
    


来源:https://stackoverflow.com/questions/48383013/scaling-with-sticky-sessions-and-websockets

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!