0
0
Redisquery~5 mins

Automatic failover in Redis

Choose your learning style9 modes available
Introduction

Automatic failover helps keep your Redis database working even if one server stops. It switches to a backup server without you doing anything.

You want your Redis service to stay online during server problems.
You have multiple Redis servers and want one to take over if the main one fails.
You want to avoid manual fixes when a Redis server crashes.
You run a website or app that needs fast data access all the time.
You want to improve reliability without extra work.
Syntax
Redis
Use Redis Sentinel configuration files and commands to set up automatic failover.

Basic Sentinel command example:
SENTINEL MONITOR <master-name> <ip> <port> <quorum>

Where:
- <master-name> is a name you choose for your master server
- <ip> and <port> point to the master server
- <quorum> is the number of Sentinels that must agree a failover is needed

Redis Sentinel is the tool that manages automatic failover.

You need at least 3 Sentinel instances for reliable failover decisions.

Examples
This tells Sentinel to watch a master Redis server named 'mymaster' at IP 127.0.0.1 and port 6379. It requires 2 Sentinels to agree before failover.
Redis
SENTINEL MONITOR mymaster 127.0.0.1 6379 2
This command forces Sentinel to start a failover for the master named 'mymaster'.
Redis
SENTINEL FAILOVER mymaster
Sample Program

This example shows how Redis Sentinel watches a master and its replica. When the master stops, Sentinel promotes the replica to master automatically.

Redis
# Start Sentinel with config file that includes:
# sentinel monitor mymaster 127.0.0.1 6379 2
# Then simulate master failure and observe automatic failover

# This is a conceptual example, actual commands run in terminal

# Step 1: Start Redis master on port 6379
redis-server --port 6379

# Step 2: Start Redis replica on port 6380
redis-server --port 6380 --replicaof 127.0.0.1 6379

# Step 3: Start Sentinel with config sentinel.conf
redis-sentinel sentinel.conf

# Step 4: Stop master to trigger failover
redis-cli -p 6379 SHUTDOWN

# Step 5: Sentinel promotes replica to master automatically

# Step 6: Check new master
redis-cli -p 6380 INFO replication
OutputSuccess
Important Notes

Automatic failover requires careful setup of Sentinel and Redis replicas.

Failover can cause a short pause while the new master is chosen.

Test failover in a safe environment before using in production.

Summary

Automatic failover keeps Redis running by switching to a backup server if the main one fails.

Redis Sentinel manages failover by monitoring servers and deciding when to switch.

You need multiple Sentinel instances and replicas for reliable failover.