1

I want to implement redis-sentinel. Following code snippet shows what I have done:

import redis

from redis.sentinel import Sentinel
from .config import redis_config

redis_client = None

def create_redis_client():

    global redis_client
    SENTINEL_PORT = int(redis_config["redis.sentinel.port"])
    sentinel = Sentinel([(redis_config["redis.server.master"], SENTINEL_PORT),
                    (redis_config["redis.server.slave1"], SENTINEL_PORT),
                    (redis_config["redis.server.slave2"], SENTINEL_PORT)])
                    # sentinel_kwargs={"password": redis_config["redis.server.password"]})
    # you will need to handle yourself the connection to pass again the password
    # and avoid AuthenticationError at redis queries
    host, port = sentinel.discover_master(redis_config["redis.master.name"])
    print(f"Redis Sentinel Host: {host}")
    print(f"Redis Sentinel Port: {port}")
    redis_client = redis.StrictRedis(
            host=host,
            port=port,
            password= redis_config["redis.server.password"]
        )

def get_redis_client():
    if redis_client is None:
        create_redis_client()
        return redis_client
    else:
        return redis_client

Here redis_config is nothing but a dictionary like this:

# sentinel
redis_config = {
    "redis.server.master": "MASTER_IP",
    "redis.server.slave1": "SLAVE_IP",
    "redis.server.slave2": "SLAVE_IP",
    "redis.sentinel.port": "26379",
    "redis.master.name": "mymaster",
    "redis.server.password": "SAMPLE_PASSWORD"
}

I followed this solution to implement redis-sentinel. But surprising enough suddenly I got the following error:

redis.exceptions.ReadOnlyError: You can't write against a read only replica.

It seems to be that the sentinel is not changing the master when any failure occurs. Why this is happening? It is not the first time this happened, this happened once more before. Is the implementation okay?

hafiz031
  • 2,236
  • 3
  • 26
  • 48

0 Answers0