I am using a Redis server with python.
My application is multithreaded (I use 20 to 32 threads for each process), and I also run the application on different machines.
I noticed that sometimes using Redis cpu is 100% and the Redis server stops responding / slows down.
I would like to use for each application 1 connection pool of only 4 connections. So, for example, if I run my application on 20 machines at most, there should be 20 * 4 = 80 connections to the redis server.
POOL = redis.ConnectionPool(max_connections=4, host='192.168.1.1', db=1, port=6379) R_SERVER = redis.Redis(connection_pool=POOL) class Worker(Thread): def __init__(self): self.start() def run(self): while True: key = R_SERVER.randomkey() if not key: break value = R_SERVER.get(key) def _do_something(self, value):
In the above code, 20 threads must be started that receive the connection from the max size 4 connection pool when the command is executed.
When is the connection released?
According to this code ( https://github.com/andymccurdy/redis-py/blob/master/redis/client.py ):
After each command, the connection is released and returned to the pool.
Can someone verify that I understood the idea correctly and the above sample code will work as described?
Because when I see redis connections, there are always more than 4.
EDIT: I just noticed in the code that the function has a return statement before ending. What is the goal finally?