I want to fail over Redis at Django

Asked 2 years ago, Updated 2 years ago, 116 views

I'd like to fail over Redis on Django, but I don't know exactly how...

Currently, the settings are as follows.

  • Specify Redis as the session backend
  • Prepare two Redis servers for a master slave configuration.
  • Redis automatically elevates slave to master in case of master failure
  • in Sentinel

settings.py is configured as follows

CACHES={
    'default': {
        'BACKEND': 'redis_cache.RedisCache',
        'LOCATION': [
            "127.0.0.1",
            "IP of Slave"
        ],
        'OPTIONS': {
            'PASSWORD': "xxxxxxxx",
            'DB': 0,
        }
    }
}
SESSION_ENGINE = 'django.contrib.sessions.backs.cache'
SESSION_CACHE_ALIAS="default"

Basically, Django only uses the master, and I want to connect to the slave in the event of a master failure, but I don't know how to do that...

If anyone knows, could you please let me know?
I look forward to your kind cooperation.

python django redis

2022-09-30 20:13

1 Answers

It seems possible by combining libraries called django-redis and redis-py-cluster instead of the Django standard django.contrib.sessions.backends.cache.

django-redis documentation

An example of how Redis cluster could be built has been commented on Issue, although it has not been tried on hand.

 'default': {
    'BACKEND': 'django_redis.cache.RedisCache',
    'LOCATION': 'redis://XXX.YYY.ZZZ.cache.amazonaws.com/0',
    'OPTIONS': {
      'REDIS_CLIENT_CLASS': 'rediscluster.RedisCluster',
      'CONNECTION_POOL_CLASS': 'rediscluster.connection.ClusterConnectionPool',
      'CONNECTION_POOL_KWARGS': {
        'skip_full_coverage_check': True# AWS ElasticCache has disabled CONFIG commands
      }
    }
  }
}

Redis Cluster support.·Issue#208·niwinz/django-redis


2022-09-30 20:13

If you have any answers or tips


© 2024 OneMinuteCode. All rights reserved.