実行環境:CentOS 6.7、Redis 3.0

Redisのconnected_clientsがデフォルトの上限10000に達していたので、maxclientsを上げようとしたが、エラーが出てしまった。

$ redis-cli info | grep connected_clients
connected_clients:10000
$ redis-cli
127.0.0.1:6379> CONFIG GET maxclients
1) "maxclients"
2) "10000"
127.0.0.1:6379> CONFIG SET maxclients 60000
(error) ERR The operating system is not able to handle the specified number of clients, try with 10208
127.0.0.1:6379>

redis-serverプロセスのmax open filesが10240となっており、Redisが内部で使う32個を除いた10208までしか設定できない。

$ ps -ef | grep redi[s] | awk '{print $2}' | xargs -I% sudo cat /proc/%/limits | grep 'Max open files'
Max open files            10240                10240                files

limits.confでは65536に設定していたので、Redisが10240になってしまっている理由を調べた。

$ sudo cat /etc/security/limits.conf
* soft nofile 65536
* hard nofile 65536

/etc/init.d/redisで直接ulimit -n 65536を実行するといい等の記述をネットで見たので試してみたがうまくいかなかったが、原因と解決法は単純で、/etc/security/limits.confがlimits.d以下の個別設定によって上書きされていたので、Redis用の設定ファイルを変更した。

$ cat /etc/security/limits.d/95-redis.conf
# If you need to change max open file limit
# for example, when you change maxclient in configuration
# you can change the value below
# see "man limits.conf" for information
redis soft nofile 10240
redis hard nofile 10240

$ sudo sed -i -r 's/^(redis.*10240)/#\1/' /etc/security/limits.d/95-redis.conf

$ tail -2 /etc/security/limits.d/95-redis.conf
#redis soft nofile 10240
#redis hard nofile 10240

Redisを再起動し、maxclientsを変更することができた。

$ sudo service redis restart
$ redis-cli
127.0.0.1:6379> CONFIG SET maxclients 60000
OK
127.0.0.1:6379> CONFIG GET maxclients
1) "maxclients"
2) "60000"

もちろん、redis.confに書いても上記エラーと解決法は同様である。

$ grep maxclients /etc/redis.conf
maxclients 60000