redis: max number of clients reached
Asked Answered
M

3

18

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.

Modernize answered 18/6, 2018 at 13:31 Comment(2)
Are both lines of your code within the iteration? If so, could you not move the first outside, so the connection is only opened once? I know it doesn't explain why the connection is not being closed, but would help alleviate opening unnecessary connections.Ido
you are right. i did it after noticing it. but i guess the problem is something else.Modernize
A
18

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)
Adapa answered 18/6, 2018 at 13:47 Comment(0)
E
14

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 .

Edi answered 18/6, 2018 at 13:39 Comment(0)
C
4

There is a temporary fix for the issue. Set timeout for client connections

Close the connection after a client is idle for N seconds (0 to disable)

timeout 0

if we set timeout 120, it will close all the idle connections and we can resolve this error config set timeout 120

After login to redis cli

enter image description here

Cramer answered 10/9, 2021 at 4:12 Comment(2)
It's a great idea, but the problem is you cannot connect redis-cli because the max number of clients has been reached 🥺. It's a chicken and egg problem.Recent
this is right!👍 I tried redis both in python and golang and have to close the connection each time I ended using the connection. setting the timeout is the handy way to manage the number of alive connection (or clients).Nitrochloroform

© 2022 - 2024 — McMap. All rights reserved.