文章系转载,方便整理和归纳,源文地址:https://cloud.tencent.com/developer/article/1803944

1. 前言

上一篇文章《你的Redis集群撑得住吗?》讲了应用增加pod时,有一个应用最大连接数计算公式为:maxTotal * pod数 < Redis cluster的最大连接数(单个分片的max_clients * 分片数) * 80% (预留百分比),大家有没有考虑过以下两个问题:1.Redis max_clients 是怎么设置的?2.为什么Redis cluster的最大连接数是 : 单个分片的max_clients * 分片数? 不急,看完这篇文章,你心中就会有答案了。

2. Redis 最大客户端连接数介绍

在Redis 2.4中,对可以同时处理的最大客户端数量有一个硬编码的限制。

在Redis 2.6中,此限制是动态的:默认情况下,它设置为10000个客户端,当然,你可以在redis.conf中通过max_clients进行定制化设置。

但是,Redis会与内核一起检查我们能够打开的最大文件描述符数量(检查soft limits)。如果该限制小于我们要处理的最大客户端数量再加上32(即Redis保留供内部使用的文件描述符数量),则Redis会修改最大客户端数量以匹配我们的客户端数量是真正能够处理在当前操作系统的限制。

$ ./redis-server --maxclients 100000
[41422] 23 Jan 11:28:33.179 # Unable to set the max number of files limit to 100032 (Invalid argument), setting the max clients configuration to 10112.

当配置Redis以处理特定数量的客户端时,最好确保也相应地设置了操作系统对每个进程的最大文件描述符数量的限制。

在Linux下,可以使用以下命令在当前会话和系统范围内设置这些限制:

ulimit -Sn 100000 # This will only work if hard limit is big enough.
sysctl -w fs.file-max=100000

Redis 4.0.12 源码在下面这一段也有明确说明

 /* Try to check if the OS is capable of supporting so many FDs. */
        server.maxclients = ll;
        if (ll > orig_value) {
            adjustOpenFilesLimit();
            if (server.maxclients != ll) {
                addReplyErrorFormat(c,"The operating system is not able to handle the specified number of clients, try with %d", server.maxclients);
                server.maxclients = orig_value;
                return;
            }
            if ((unsigned int) aeGetSetSize(server.el) <
                server.maxclients + CONFIG_FDSET_INCR)
            {
                if (aeResizeSetSize(server.el,
                    server.maxclients + CONFIG_FDSET_INCR) == AE_ERR)
                {
                    addReplyError(c,"The event loop API used by Redis is not able to handle the specified number of clients");
                    server.maxclients = orig_value;
                    return;
                }
            }
        }
    }

3. 集群的最大连接数计算公式证明

针对集群的最大连接数计算公式,找了下源码,没有找到对应的计算公式。想了下,那就测试吧!建立了个3分片的集群,用阿里云的memtier_benchmark进行压测,得出结论和阿里云的测试结果一致。

测试语句:

./memtier_benchmark -s 192.168.0.12 -p 6379 -a XXX -c 20 -d 32 --threads=10 --ratio=1:1 --test-time=1800 --select-db=10

结论(以下测试租用阿里云机器社区版):

规格分片数每秒新建连接数上限连接数上限带宽(MB/s)QPS平均值
16GB集群版85000080000768640000
128GB集群版165000016000015361280000
512GB集群版325000032000020482560000

综上,可以看出Redis cluster最大连接数=单个分片的max_clients * 分片数。

4.总结

本文主要主要解决了《Redis max_clients 是怎么设置的?》,《为什么Redis cluster的最大连接数是 : **单个分片的max_clients * 分片数?》**这两个问题,虽然方法不一致,但是理论联系实际去证明,其实也是一种方法。

5.参考文献

  1. https://redis.io/topics/clients#:~:text=Maximum%20number%20of%20clients%20In%20Redis%202.4%20there,otherwise%20stated%20by%20the%20maxclients%20directive%20in%20Redis.conf.
  2. https://www.alibabacloud.com/help/doc-detail/145231.htm?spm=a2c63.p38356.b99.30.4a9c5a9fbmgjML
Logo

为开发者提供学习成长、分享交流、生态实践、资源工具等服务,帮助开发者快速成长。

更多推荐