Skip to content

Dynamically route commands to master or replica #3591

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
travisofthenorth opened this issue Apr 9, 2025 · 4 comments
Open

Dynamically route commands to master or replica #3591

travisofthenorth opened this issue Apr 9, 2025 · 4 comments

Comments

@travisofthenorth
Copy link

Hello. I'm trying to understand if there's any way to programmatically send a command to a replica node using a RedisCluster. I'm aware of the read_from_replicas configuration, but there are a few issues:

  • I can't send all reads to the replicas. There are a few use cases in my application that have strong consistency requirements, and others that don't.
  • Alternatively, I could have one client that uses read_from_replicas=True and one that uses read_from_replicas=False. However, now every process in my fleet has 2 connections to every node, doubling overall connections. For a large fleet, this is a problem as you can easily exceed the max number of connections.

Is there any workaround for this? If not, is it something you'd be open to supporting in future versions?

@travisofthenorth travisofthenorth changed the title Dynamically send command to replica Dynamically route commands to master or replica Apr 9, 2025
@petyaslavova
Copy link
Collaborator

Hi @travisofthenorth, currently the only option for your issue is to have two clients - one with a load balancing strategy introduced recently - that will allow you to execute reads from replicas with three different strategies supported and one for the read commands that should not go to replicas.
But still none of the strategies supports a list of commands that should not be sent to replicas.

It sounds like an interesting idea for a new feature, but I can't provide any estimate of when it could be implemented.

@travisofthenorth
Copy link
Author

@petyaslavova thank you. I'll take a look.

Just to clarify, I'm not looking to send a subset of Redis commands to replicas. Essentially I want to enable/disable read_from_replicas depending on the use case.

The problem is pretty simply that the client has a single global config. If it could be controlled on a per-command basis, it would be solved.

Pseudo-code:

my_redis_client = RedisCluster(..., read_from_replicas=False)

def function_that_can_be_eventually_consistent():
  return redis.get("key", read_from_replicas=True)

def function_that_must_hit_leader():
  return redis.get("key")

@petyaslavova
Copy link
Collaborator

@travisofthenorth thanks for the clarification :) - I have understood your question correctly.
And currently, the only way to achieve it is with two separate clients - one with forbidden reading from replicas that will ensure your strong consistency requirements and one that is allowed to read from replicas - which has several possible strategies for how reading from replicas should be distributed.
The main reason I mentioned the LoadBalancing strategy is that the read_from_replicas property is now deprecated, and providing the LoadBalancingStrategy is the preferred way to configure the client (keep in mind that this approach is still in beta release - will be in GA release with the next stable version release).

@travisofthenorth
Copy link
Author

@petyaslavova thanks for all your help.

I'm curious if you'd be interested in a change like this: #3598

I'm happy to keep going with this if the interface makes sense to you and is something you'd want to support. If not, I'll just close it. Let me know.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging a pull request may close this issue.

2 participants