Redis py: when to use connection pool?
Asked Answered
R

2

11
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?

Rita answered 10/2, 2016 at 13:52 Comment(0)
D
6

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!

Dissection answered 10/2, 2016 at 14:10 Comment(4)
I read that doc and im still confused or i dont see why you would use it when you can just instantiate StrictRedis with host/port/db. Is it simply to not having to retype it if you had to instantiate lots of StrictRedis instances?Rita
No, it is if you need to manage connection pooling. Perhaps you have a particular setup, constrained resources, or a use case where you need to optimize for something special, or, as the example in the docs, you want to implement client side sharding.Dissection
okay maybe i just dont have a clear understanding of those concepts. idont know why you wouldnt be able to do client sharding without connection pooling when you can specify the host without the use of connection pool. sorry for not understanding. any good tutorial/doc i can read up on?Rita
Perhaps you don't. You could see it as an entry point to insert behaviour between the client and the server. Perhaps you have many redis servers where one acts as master and the other are slaves. Then you could write your own connection pool class to issue read commands to slaves, and writes to the master. Of course you could also have a sharding client/server in between, as you suggest, but if you don't want to you could also implement sharding in your custom connection pool.Dissection
F
0

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.

Example :

class RedisConnection:
    def __new__(cls):
        if not hasattr(cls, 'instance'):
            pool = redis.ConnectionPool(host='******', password='*****', port=*****, db=0)
            cls.instance = redis.Redis(connection_pool=pool)
            return cls.instance
    
obj_1=RedisConnection()
print(id(obj_1))

obj_2=RedisConnection()
print(id(obj_2))

redis connectionpooling python

Footnote answered 16/10, 2023 at 7:47 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.