Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

redis: max number of clients reached

Tags:

python

redis

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.

like image 983
sjishan Avatar asked Jun 18 '18 13:06

sjishan


People also ask

Is heroku Redis free?

Reliable and powerful Redis as a service. Starting at $0/mo.

How many concurrent connections can Redis handle?

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 .

How many clients can Redis handle?

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.

What is Redis pool size?

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.


Video Answer


2 Answers

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)
like image 87
Anand Tripathi Avatar answered Oct 16 '22 19:10

Anand Tripathi


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 .

like image 22
Jordan Avatar answered Oct 16 '22 17:10

Jordan