2 Node Redis HA
Asked Answered
I

6

10

I have two nodes which I want to run as servers in active-active mode and also have HA capability i.e if one is down, the other one should start receiving all the requests but while both are up, both should be taking all the requests. Now since Redis doesn't allow active-active mode for the same hash set and I don't have option to run Sentinel as I can't have a third node, my idea is to run the two nodes in replication and myself decide if master node is down and promote the slave to master. Are there any issues with this? When the original master comes back up, is there a way to configure it as slave?

Does this sound like a good idea? I am open to suggestions other than Redis.

Indigestive answered 22/12, 2016 at 7:40 Comment(1)
I'm interested in that one to. It looks like there's no way to configure two nodes to failover reliably even on redis 4, however on redis4 this can be done manually.Ionogen
M
7

Generally running two node is never a good idea because it is bound to have split brain problem: When the network between the two node is down for a moment or two, the two node will inevitably think each other is offline and will promote/keep itself to be master and start accepting requests from other services. Then the split brain happens.

And if you are OK with this possible situation, then you can look into setup a master-slave with help of a script file and a HA service like pacemaker or keepalived.

Typically you have to tell the cluster manager through a predefined rule that when two machine rejoins under split brain condition, which one is your preferred master.

When a master is elected, execute the script and basically it execute slaveof no one on itself and execute slaveof <new-master-ip> <port> on the other node.

You could go one step further in your script file and try to merge the two data sets together but whether that's achievable or not is entirely down to how you have organized your data in Redis and how long you are prepared to wait for to have all the data in sync.

I have done this way myself before through pacemaker+corosync.

Murex answered 26/7, 2017 at 20:52 Comment(0)
I
4

Ok, partial solution with SLAVEOF:

You can manually promote slave to master by running:

SLAVEOF NO ONE

You can manually transition master to slave by running:

SLAVEOF <HOST> <port>

Clustering should be disabled.

Ionogen answered 20/7, 2017 at 20:55 Comment(0)
R
1

If you brought the replica online manually by changing it to replicaof no one, you need to be careful to bring the failed master back online as a replicaof the new node so you dont overwrite more recent data. I would not recommend doing this manually. You want to minimize downtime so automated failover is ideal

You mention being open to other products. Check out KeyDB which has the exact configuration you are looking for. It is a maintained multi-threaded fork of redis which offers the active-replica scenario you are looking for. Check out an example of it here.

Run both nodes as replicas of each other accepting reads and writes simultaneously (depending on upfront proxy config). If one fails the other continues to take the full load and is already sync'd.

Regarding the split brain concern, KeyDB can handle split brain scenarios where the connection between masters is severed, but writes continue to be made. Each write is timestamped and when the connection is restored each master will share their new data. The newest write will win. This prevents stale data from overwriting new data written after the connection was severed.

Rossierossing answered 29/5, 2019 at 2:15 Comment(0)
F
0

I would recommendation to have at least 3 nodes with Sentinel Setup for enabling gossip/quorum for auto promotion of slave to master when current master node goes down.

Flavorful answered 27/7, 2017 at 14:15 Comment(0)
R
0

I believe it is possible to create a cluster with two nodes with the commands below:

$ redis-cli --cluster create <ip-node1>:7000 <ip-node1>:7001 <ip-node2>:7000 <ip-node2>:7001 --cluster-replicas 1

To resolve the split-brain problem. you can add a third node without data:

$ cluster meet #IP_node3#:7000

$ cluster nodes

I think it works.

Ribonuclease answered 21/6, 2022 at 13:37 Comment(0)
D
0

Python example using redis module, note I haven't tested, but I think this should work:

import redis
import random

def get_redis_connection():
    # List of Redis nodes
    redis_nodes = [
        {'host': 'redis1.example.com', 'port': 6379},
        {'host': 'redis2.example.com', 'port': 6379}
    ]

    # Shuffle the nodes to distribute the load
    random.shuffle(redis_nodes)

    # Attempt connection to each node until successful or all fail
    for node in redis_nodes:
        try:
            r = redis.Redis(host=node['host'], port=node['port'])
            r.ping()  # Test the connection
            return r
        except redis.exceptions.ConnectionError:
            continue

    raise Exception("Failed to connect to any Redis node")

# Usage example
redis_connection = get_redis_connection()
redis_connection.get('key')
Desireedesiri answered 6/5, 2023 at 11:11 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.