Skip to content
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

How to configure and connect redis cluster correctly? #710

Open
zhang-haiyang opened this issue Nov 22, 2023 · 2 comments
Open

How to configure and connect redis cluster correctly? #710

zhang-haiyang opened this issue Nov 22, 2023 · 2 comments
Labels

Comments

@zhang-haiyang
Copy link

zhang-haiyang commented Nov 22, 2023

Version information:
django-redis: 5.4.0
redis(py): 5.0.1
redis(server): 7.0

I wrote my own configuration as described in the diango-redis documentation for redis clusters, which is as follows:

django1

Then, I use the following method in the code to get one of the data in the redis cluster:

django2

But an error message appeared:

Error: MOVED 5798 my_ip:my_port

I think this is probably due to not connecting in a cluster mode when the client connects, I know that the redis client's command when connecting to the cluster is: redis-cli -c ....

But in diango-redis, how can I modify my configuration so that I can properly connect to the redis cluster?

Looking forward to your reply, thank you!

@jonprindiville
Copy link

@zhang-haiyang As far as I am aware there is no official support for Redis Cluster here in django-redis.

For a time the actual https://github.com/redis/redis-py client had no support for it either. One way to approach it was https://github.com/Grokzen/redis-py-cluster + https://github.com/jazzband/django-redis/ ...

Recently, though, https://github.com/Grokzen/redis-py-cluster became deprecated because most of that functionality landed in https://github.com/redis/redis-py (🎉)

So far, django-redis doesn't support the new cluster functionality of redis-py. There's some chatter about that over in #606 but mostly just snippets and brainstorms.

I've been meaning to push on that a bit more, but haven't had bandwidth. (But I think I need to figure that out before I can get to Django 4+ so probably can't wait toooooo long.)

I think it's doable... probably need to understand how django-redis wants to integrate cluster stuff into its test suite, though, and I think there might be some typing stuff to think about because RedisCluster and Redis client are separate class hierarchies, but I don't think there's any real big barriers.

@stinovlas
Copy link

What are the caveats of using this approach?

from django_redis.client import DefaultClient
from redis.cluster import RedisCluster

class ClusterClient(DefaultClient):
    """Cluster enabled client."""

    def connect(self, index=0):
        """Override to connect properly."""
        return RedisCluster.from_url(self._server[index])

I guess it should work for commands that behave the same in single instance and cluster mode. Or will it break horribly in some cases?

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

No branches or pull requests

3 participants