7

What is the best pattern to handle Redis connections (both for interacting with Redis directly and indirectly through Python-RQ)?

Generally, database connections need to be closed / returned to a pool when done, but I don't see how to do that with redis-py. That makes me wonder if I'm doing it the wrong way.

Also, I have seen some performance dips when enqueuing jobs to RQ, which I've been told might relate to poor connection usage / reuse.

Basically, I'm interested in knowing the correct pattern, so I can either verify or correct what we have in our application.

Thanks a lot! If there's more information that would be helpful, let me know.

1 Answer 1

5

Behind the scenes, redis-py uses a connection pool to manage connections to a Redis server. By default, each Redis instance you create will in turn create its own connection pool. You can override this behavior and use an existing connection pool by passing an already created connection pool instance to the connection_pool argument of the Redis class. You may choose to do this in order to implement client side sharding or have finer grain control of how connections are managed

pool = redis.ConnectionPool(host='localhost', port=6379, db=0)
r = redis.Redis(connection_pool=pool)
Sign up to request clarification or add additional context in comments.

3 Comments

Great! However, it still isn't very clear to me if the connections returned by that connection pool should be closed at any point or if it's handled automatically. Any idea?
am not doing any closing of the connections nor did I read about it and am not seeing any performance issues yet. Maybe there is a a proper way of handling the connections
@JuanCarlosCoto: its been years now since you posted this thread, I believe you would be having tons of experience now. Can you share the best practices to follow while handling multiple connections?

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.