Grokzen / redis-py-cluster

Python cluster client for the official redis cluster. Redis 3.0+.
https://redis-py-cluster.readthedocs.io/
MIT License
1.1k stars 316 forks source link

Py-Redis-Cluster unable redirect read to replica(s) #487

Closed pnthai88 closed 2 years ago

pnthai88 commented 2 years ago

Hello everybody,

I'm making a cluster with single replica for each master. For write, I will redirect it to master nodes; for read I redirect to replica nodes. For standalone redis, it's very simple. But in cluster, how do I perform it since all read and write always redirect to master nodes

cluster_replicaNodes = [a list slave's ips...]

def get_slaves():
    return RedisCluster(
        password="mypasswordisweak",
        startup_nodes=cluster_replicaNodes,
        readonly_mode=True,
        decode_responses=True,
    )
cluster_masterNodes = [a list master's ips...]

def get_master():
    return RedisCluster(
        password="mypasswordisweak",
        startup_nodes=cluster_masterNodes,
        decode_responses=True,
    )

The result of several hundred reads

image

Grokzen commented 2 years ago

@pnthai88 I do not even get what you want to do with your initial code, reading from replica nodes is not how that feature works, you can't just send in a list of the slave nodes into the class constructor and expect it to only use those nodes. Even in that case the client class will query all the nodes you provided and still figure out all the master nodes and still do what the client is designed to do. If you want a working example of how reading from replicas work you should look at this example instead https://github.com/Grokzen/redis-py-cluster/blob/master/examples/pipeline-readonly-replicas.py as it shows how it really works. There is a mess in how there is essentially two options with read_from_replicas and readonly_mode and how to use them. But follow that example and it should start working for you.

The mentioned code change will not be implemented tho as that is not the solution to your problem, using read_from_replicas=True is.

pnthai88 commented 2 years ago

Oh yea, I just read carefully the library and just found the parameter for pipeline

Just simple add .pipeline(read_from_replicas=True)

Then it's working! Hahaha