I have this redis cache where values are set about 100 times each day. After running for some days perfectly I am getting the connection error "maximum number of clients reached". After restarting the server it is working fine now, however I want to avoid the issue in the future.
It seems to me once I create a client object, it is staying the connection pool and is never killed or removed.
Here is my code
r = redis.StrictRedis(host= host, port=6379, db=0)
r.set(key_name, data)
This is within an iteration. And, I am using redis in python.
Reliable and powerful Redis as a service. Starting at $0/mo.
Maximum Concurrent Connected Clients In Redis 2.6 and newer, this limit is dynamic: by default it is set to 10000 clients, unless otherwise stated by the maxclients directive in redis. conf .
Redis can handle many connections, and by default, Redis has a maximum number of client connections set at 10,000 connections. You can set the maximum number of client connections you want the Redis server to accept by altering the maxclient from within the redis.
To improve performance, go-redis automatically manages a pool of network connections (sockets). By default, the pool size is 10 connections per every available CPU as reported by runtime.
I think your redis connection is instantiating on every request causing it to reach the max connection limit, you should keep your redis instance in the global this will share the same redis instance, this should not cause too many connections anymore. The redis instance will have its own connection pool, you can limit your connection nums by set max_connections parameter to redis.ConnectionPool. If max_connections is set, then this object raises redis.ConnectionError when the pool's limit is reached.
POOL = redis.ConnectionPool(host= host, port=6379, db=0)
r = redis.StrictRedis(connection_pool=POOL)
A client connection will remain open forever if you don't close it.
https://redis.io/topics/clients
Client timeouts By default recent versions of Redis don't close the connection with the client if the client is idle for many seconds: the connection will remain open forever.
However if you don't like this behavior, you can configure a timeout, so that if the client is idle for more than the specified number of seconds, the client connection will be closed.
You can configure this limit via redis.conf or simply using CONFIG SET timeout .
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With