redis最大连接数

Posted cheyunhua

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了redis最大连接数相关的知识,希望对你有一定的参考价值。

Well, it‘s a bit late for this post, but since I just spent a lot of time(the whole night) to configure a new redis server 3.0.6 on ubuntu 16.04. I think I should just write down how I do it so others don‘t have to waste their time...

For a newly installed redis server, you are probably going to see the following issues in redis log file which is /var/log/redis/redis-server.log

Maximum Open Files

3917:M 16 Sep 21:59:47.834 # You requested maxclients of 10000 requiring at least 10032 max file descriptors.
3917:M 16 Sep 21:59:47.834 # Redis can‘t set maximum open files to 10032 because of OS error: Operation not permitted.
3917:M 16 Sep 21:59:47.834 # Current maximum open files is 4096. maxclients has been reduced to 4064 to compensate for low ulimit. If you need higher maxclients increase ‘ulimit -n‘.

I have seen a lot of posts telling you to modify

/etc/security/limits.conf
redis soft nofile 10000
redis hard nofile 10000

or

/etc/sysctl.conf
fs.file-max = 100000

That might work in ubuntu 14.04, but it certainly not works in ubuntu 16.04. I guess it has something to do with changing from upstart to systemd, but I am no expert of linux kernel!

To fix this you have to do it the systemd way

/etc/systemd/system/redis.service
[Service]
...
User=redis
Group=redis
# should be fine as long as you add it under [Service] block
LimitNOFILE=65536
...

Then you must daemon reload and restart the service

sudo systemctl daemon-reload
sudo systemctl restart redis.service

To check if it works, try to cat proc limits

cat /run/redis/redis-server.pid
cat /proc/PID/limits

and you will see

Max open files            65536                65536                files     
Max locked memory         65536                65536                bytes   

At this stage, the maximum open file is solved.

Socket Maximum Connection

2222:M 16 Sep 20:38:44.637 # WARNING: The TCP backlog setting of 511 cannot be enforced because /proc/sys/net/core/somaxconn is set to the lower value of 128.

Memory Overcommit

2222:M 16 Sep 20:38:44.637 # Server started, Redis version 3.0.6
2222:M 16 Sep 20:38:44.637 # WARNING overcommit_memory is set to 0! Background save may fail under low memory condition. To fix this issue add ‘vm.overcommit_memory = 1‘ to /etc/sysctl.conf and then reboot or run the command ‘sysctl vm.overcommit_memory=1‘ for this to take effect.

Since these two are related, we will solve it at once.

sudo vi /etc/sysctl.conf

# Add at the bottom of file
vm.overcommit_memory = 1
net.core.somaxconn=1024

Now for these configs to work, you need to reload the config

sudo sysctl -p

Transparent Huge Pages

1565:M 16 Sep 22:48:00.993 # WARNING you have Transparent Huge Pages (THP) support enabled in your kernel. This will create latency and memory usage issues with Redis. To fix this issue run the command ‘echo never > /sys/kernel/mm/transparent_hugepage/enabled‘ as root, and add it to your /etc/rc.local in order to retain the setting after a reboot. Redis must be restarted after THP is disabled.

To permanently solve this, follow the log‘s suggestion, and modify rc.local

sudo vi /etc/rc.local

if test -f /sys/kernel/mm/transparent_hugepage/enabled; then
    echo never > /sys/kernel/mm/transparent_hugepage/enabled
fi

This require you to reboot, backup your data or do anything you need before you actually do it!!

sudo reboot

Now check you redis log again, you should have a redis server without any errors or warnings.

 

0

You just need this command in console:

sudo ulimit -n 65535

以上是关于redis最大连接数的主要内容,如果未能解决你的问题,请参考以下文章

redis客户端连接,最大连接数查询与设置

redis的连接数的大小,跟什么有关系?并发量!和已有的连接数

redis客户端连接,最大连接数查询与设置

redis客户端连接,最大连接数查询与设置

redis最大连接数

生产redis client 链接报:ERR max number of clients reached 含义: 达到最大客户端数错误