question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

how to use connection pool in cluster mode

See original GitHub issue

Hello, I have some problem on how to use Connection Pool while using redis-py-cluster, Could you give me some help, thanks^_^

I know that we can use Connection Pool while connect to a single redis node, below is the code snippet:

pool = redis.ConnectionPool(host='127.0.0.1', port=6379)
r = redis.Redis(connection_pool=pool)

In the above code, redis client use a connection pool, to avoid extra network overhead.

But it seems there is no document talking about how to use connection pool in redis-py-cluster.

r = StrictRedisCluster(startup_nodes=redis_nodes, decode_responses=True)
r.get("aaa")      // create a new connection and then close it while request is finished.
r.get("bbb")      // create a new connection and then close it while request is finished.
...
r.get("zzz")      // create a new connection and then close it while request is finished.

Can anybody supply some explain or simple demo. Also, how to use both Pipeline and Connection Pool in redis-py-cluster.

Thanks.

Issue Analytics

  • State:closed
  • Created 3 years ago
  • Comments:5 (3 by maintainers)

github_iconTop GitHub Comments

1reaction
Grokzencommented, May 8, 2020

@vangoleo You must use one of the connectionpools defined in this file https://github.com/Grokzen/redis-py-cluster/blob/master/rediscluster/connection.py as you need a proper cluster compatible connection pool in order to use it whit this lib. So create an object instance of ClusterConnectionPool then pass that object into RedisCluster(connection_pool=your_cluster_pool) and then you can use anything and everything as you want.

But do note that there is no need to create an connectionpool from outside the RedisCluster object as if you pass in startup_nodes to your cluster client, it will automatically create a pool for you in the same way that redis-py creates a pool for you. Unless you subclass the pool class and change any functionality in your pool, this step is not needed and redundant.

Pipelines works the exact same way in a cluster as they do in a non-clustered environment, create your client class and then create a pipeline object and then send your commands into it and execute. You can use basically any normal non-clustered example of a pipeline inside a clustered environment. Do note that there is a ton of internal and technical differences when using a clustered pipeline compared to a normal pipeline, and there is plenty of docs that you should read up on inside the docs/ folder in this project that can lead you to what you need to know and understand about using them in a cluster.

0reactions
Grokzencommented, Mar 1, 2021

There will be no specific example made here by my at least for this. ConnectionPools works basically the same between redis-py and this lib. You can make your own class if you want to implement custom logic (which is already a super advance use-case and should not be done unless you really need to or know exactly what you are doing) so there is not really a general example i would provide that would be used by the general public any way. So if you want a example of this in any way, then you will have to dig into the code here, or in redis-py to understand how custom ConnectionPools work, and i reitterate, there is tons of examples and code and threads that is easily searchable on google or stackoverflow in how to do and work with this feature.

If you really want to have a example file, then make one and submit it through a PR here and i will have a look at it.

Read more comments on GitHub >

github_iconTop Results From Across the Web

JDBC Connection Pools
Click the Target and Deploy tab and select the servers or clusters on which you want to deploy the connection pool. Click Apply...
Read more >
How to handle connection pooling in cluster mode of nodejs
I am using MySQL and creating MySQL connection pool once and reusing in all other modules where needed. When my code is running...
Read more >
How to Manage Connection Pools for PostgreSQL ...
To add a connection pool to a database cluster, from the Databases page, click the name of the cluster to go to its...
Read more >
What is Connection Pooling, and Why Should You Care
Database connections can get expensive at scale. Pooling connections can help, so here's what connection pooling is and how to do it!
Read more >
Improve database performance with connection pooling
Although most databases do not have an in-built connection pooling system, there are middleware solutions that we can use to pool connections ...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found