Answer a question

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.

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)
Logo

Python社区为您提供最前沿的新闻资讯和知识内容

更多推荐