Redis Sentinel Failover Client Configuration¶
Redis Sentinel Failover Client Configuration refers to the setup required on the application side (specifically using the go-redis/redis library) to connect to a Redis high-availability cluster managed by Redis Sentinel^[400-devops__09-Scripting-Language__golang__introduction__part-5.database.redis__readme.md]. This configuration involves specifying Sentinel addresses to enable automatic discovery of the current Redis master, allowing the client to handle failovers transparently^[400-devops__09-Scripting-Language__golang__introduction__part-5.database.redis__readme.md].
Configuration Parameters¶
To successfully connect to a Sentinel-managed Redis instance, the client typically requires the following connection parameters^[400-devops__09-Scripting-Language__golang__introduction__part-5.database.redis__readme.md]:
- Sentinel Addresses: A comma-separated list or string slice containing the host and port of the Sentinel nodes (e.g.,
sentinel-0:5000,sentinel-1:5000). - Master Name: The alias used to identify the master group within the Sentinel configuration (e.g.,
mymaster). - Password: The authentication password required to connect to the Redis instances.
Implementation in Go (go-redis/v8)¶
The go-redis library provides a specific function, NewFailoverClient, designed for this architecture^[400-devops__09-Scripting-Language__golang__introduction__part-5.database.redis__readme.md].
Environment Variables¶
It is recommended to externalize configuration using environment variables^[400-devops__09-Scripting-Language__golang__introduction__part-5.database.redis__readme.md]. Common variables include:
REDIS_SENTINELS: The list of Sentinel addresses.REDIS_MASTER_NAME: The name of the monitored master.REDIS_PASSWORD: The security credential.
var redis_sentinels = os.Getenv("REDIS_SENTINELS")
var redis_master = os.Getenv("REDIS_MASTER_NAME")
var redis_password = os.Getenv("REDIS_PASSWORD")
Client Initialization¶
The initialization process involves parsing the Sentinel addresses and passing them to the FailoverOptions struct^[400-devops__09-Scripting-Language__golang__introduction__part-5.database.redis__readme.md].
import (
"github.com/go-redis/redis/v8"
"strings"
"context"
)
var ctx = context.Background()
sentinelAddrs := strings.Split(redis_sentinels, ",")
rdb := redis.NewFailoverClient(&redis.FailoverOptions{
MasterName: redis_master,
SentinelAddrs: sentinelAddrs,
Password: redis_password,
DB: 0,
})
// Verify connectivity
rdb.Ping(ctx)
Networking Considerations¶
When deploying the client inside a container (e.g., Docker), it is crucial to ensure that the application container resides on the same Docker network as the Redis and Sentinel containers^[400-devops__09-Scripting-Language__golang__introduction__part-5.database.redis__readme.md]. This isolation often requires using the container names (resolved via Docker's internal DNS) as the hostnames in the REDIS_SENTINELS variable^[400-devops__09-Scripting-Language__golang__introduction__part-5.database.redis__readme.md].
Related Concepts¶
- [[Redis Clustering]]
- [[High Availability]]
- [[Go Environment Variables]]
Sources¶
^[400-devops__09-Scripting-Language__golang__introduction__part-5.database.redis__readme.md]