pool = redis.ConnectionPool(host='10.0.0.1', port=6379, db=0)
r = redis.Redis(connection_pool=pool)
vs.
r = redis.Redis(host='10.0.0.1', port=6379, db=0)
Those two works fine.
Whats the idea behind using connection pool? When would you use it?
If we create a separate Redis connection for a thread it will be overhead for the Redis server. We need to have a pool of connections in multi-threaded environments to address these challenges. This allows us to talk to Redis from multiple threads while still getting the benefits of reused connections.
Redis-py provides a connection pool for you from which you can retrieve a connection. Connection pools create a set of connections which you can use as needed (and when done - the connection is returned to the connection pool for further reuse).
Use JedisPool This allows you to talk to redis from multiple threads while still getting the benefits of reused connections. The JedisPool object is thread-safe and can be used from multiple threads at the same time. This pool should be configured once and reused.
Individual ElastiCache for Redis nodes support up to 65,000 concurrent client connections.
From the redis-py docs:
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.
So, normally this is not something you need to handle yourself, and if you do, then you know!
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With