Fading Coder

One Final Commit for the Last Sprint

Home > Tech > Content

Redis High Availability with Master-Replica Replication and Sentinel Monitoring

Tech May 12 2

Prepare all nodes in the cluster by installing build dependencies and extracting the Redis source distribution:

yum install -y gcc make tcl
curl -fsSL https://download.redis.io/releases/redis-7.0.12.tar.gz -o /usr/src/redis.tar.gz
tar -xzf /usr/src/redis.tar.gz -C /usr/src/
cd /usr/src/redis-7.0.12
make && make install PREFIX=/opt/redis

Create the runtime directories and install the service script:

mkdir -p /opt/redis/{conf,data,logs}
cp utils/redis_init_script /etc/init.d/redis
sed -i 's/REDISPORT=6379/REDISPORT=6380/' /etc/init.d/redis
chmod +x /etc/init.d/redis
ln -s /opt/redis/conf /etc/redis

Generate the base configuraton file on each server:

cat > /etc/redis/6380.conf << 'EOF'
daemonize yes
port 6380
bind 0.0.0.0
dir /opt/redis/data
logfile /opt/redis/logs/redis-server.log
protected-mode no
supervised auto
EOF

Initialize the Redis service across all nodes:

/etc/init.d/redis start
ss -tlnp | grep 6380

Configure the replica instances to synchronize with the primary. On secondary nodes (192.168.1.11 and 192.168.1.12), append the replication directive:

echo "replicaof 192.168.1.10 6380" >> /etc/redis/6380.conf
/etc/init.d/redis restart

Verify the replication topology by writing test data on the primary node:

redis-cli -h 192.168.1.10 -p 6380 SET cluster:validation "master-write-test"
redis-cli -h 192.168.1.10 -p 6380 GET cluster:validation

Confirm data propagation on replicas:

redis-cli -h 192.168.1.11 -p 6380 GET cluster:validation
redis-cli -h 192.168.1.12 -p 6380 GET cluster:validation

Deploy Redis Sentinel for automatic failover monitoring. Establish the configuration on all nodes:

cat > /etc/redis/sentinel.conf << 'EOF'
port 26380
daemonize yes
logfile "/opt/redis/logs/sentinel.log"
dir /tmp
sentinel monitor primary-node 192.168.1.10 6380 2
sentinel down-after-milliseconds primary-node 5000
sentinel parallel-syncs primary-node 1
sentinel failover-timeout primary-node 120000
EOF

Initialize the Setninel processes:

redis-sentinel /etc/redis/sentinel.conf
redis-cli -p 26380 INFO Sentinel

The output should indicate the primary node with two replicas attached.

To validate automatic failover, terminate the Redis process on the current primary (192.168.1.10):

redis-cli -h 192.168.1.10 -p 6380 DEBUG SEGFAULT

Monitor the election process via Sentinel:

redis-cli -p 26380 INFO Sentinel

Look for the address field changing to 192.168.1.11 or 192.168.1.12 in the output.

Once promotion comlpetes, verify write operations on the new primary:

redis-cli -h 192.168.1.11 -p 6380 SET failover:check "promoted-node"
redis-cli -h 192.168.1.12 -p 6380 GET failover:check

Confirm the old primary reconnects as a replica when restarted.

Tags: Redis

Related Articles

Understanding Strong and Weak References in Java

Strong References Strong reference are the most prevalent type of object referencing in Java. When an object has a strong reference pointing to it, the garbage collector will not reclaim its memory. F...

Comprehensive Guide to SSTI Explained with Payload Bypass Techniques

Introduction Server-Side Template Injection (SSTI) is a vulnerability in web applications where user input is improper handled within the template engine and executed on the server. This exploit can r...

Implement Image Upload Functionality for Django Integrated TinyMCE Editor

Django’s Admin panel is highly user-friendly, and pairing it with TinyMCE, an effective rich text editor, simplifies content management significantly. Combining the two is particular useful for bloggi...

Leave a Comment

Anonymous

◎Feel free to join the discussion and share your thoughts.