使用粘性会话和 websockets 进行扩展 [英] Scaling with sticky sessions and websockets

查看:31
本文介绍了使用粘性会话和 websockets 进行扩展的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

最初我们有两个 AWS EC2 实例,node.js 在负载均衡器后面运行,带有粘性会话.随着负载的增加,会添加更多实例.

但是我们在使用这种方法时遇到了问题.由于out应用主要用于研讨会,负载通常会在短时间内增加(研讨会开始),每个研讨会参与者对前两个实例都有一个粘性会话,而新实例几乎没有.因此,性能一直很差.

首先想到的是:让我们禁用粘性会话.但这会破坏我们的 websockets,因为它们需要粘性会话(至少这是我读过的).另一个问题是减少负载.实例关闭,套接字连接也会丢失.

有没有一种方法可以在实例之间转移用户会话,或者让 websockets 在没有粘性会话的情况下工作(也许使用 Redis)?

解决方案

解决方案是

  1. 最后一个问题是扩展:如果其中一个实例因集群连接负载较低而关闭,则可能会丢失.在客户端(在我们的示例中为角度应用程序)断开连接后,通过简单的重新连接即可解决此问题:

    <代码>[...]this.socket.on('disconnect', () => {//连接丢失后重新连接this.connect();});[...]

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)?

解决方案

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();
    });
    [...]
    

这篇关于使用粘性会话和 websockets 进行扩展的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆