Redis Sentinel with Go¶
Redis Sentinel with Go refers to the integration of a Go application with a high-availability Redis setup using Sentinel. This approach allows a Go service to automatically handle Redis master failures and discover new master nodes without manual intervention.^[400-devops__09-Scripting-Language__golang__introduction__part-5.database.redis__readme.md]
Configuration and Dependencies¶
To interact with Redis Sentinel in Go, the go-redis/redis library is the standard client^[400-devops__09-Scripting-Language__golang__introduction__part-5.database.redis__readme.md]. It can be installed using go get github.com/go-redis/redis/v8^[400-devops__09-Scripting-Language__golang__introduction__part-5.database.redis__readme.md].
Establishing a connection requires specific configuration details, typically stored as [[Environment Variables]]^[400-devops__09-Scripting-Language__golang__introduction__part-5.database.redis__readme.md]:
REDIS_SENTINELS: A comma-separated list of Sentinel addresses (e.g.,sentinel-0:5000,sentinel-1:5000).REDIS_MASTER_NAME: The name of the master group being monitored (e.g.,mymaster).REDIS_PASSWORD: The authentication password for the Redis instances.
Client Implementation¶
The go-redis client provides a NewFailoverClient function specifically designed for Sentinel architectures^[400-devops__09-Scripting-Language__golang__introduction__part-5.database.redis__readme.md]. This function requires a FailoverOptions struct containing the master name, sentinel addresses, password, and database number^[400-devops__09-Scripting-Language__golang__introduction__part-5.database.redis__readme.md].
import (
"github.com/go-redis/redis/v8"
"context"
"strings"
"os"
)
var ctx = context.Background()
func NewRedisClient() *redis.Client {
sentinelAddrs := strings.Split(os.Getenv("REDIS_SENTINELS"), ",")
rdb := redis.NewFailoverClient(&redis.FailoverOptions{
MasterName: os.Getenv("REDIS_MASTER_NAME"),
SentinelAddrs: sentinelAddrs,
Password: os.Getenv("REDIS_PASSWORD"),
DB: 0,
})
return rdb
}
Data Operations¶
Once the client is initialized, standard Redis commands can be executed using the context. Common operations include setting and retrieving [[JSON]] data^[400-devops__09-Scripting-Language__golang__introduction__part-5.database.redis__readme.md].
Storing Data¶
Data, such as a struct, is marshaled into JSON and stored using the Set method^[400-devops__09-Scripting-Language__golang__introduction__part-5.database.redis__readme.md].
import "encoding/json"
// Assuming 'redisClient' is the initialized *redis.Client
func saveVideo(video video) {
videoBytes, err := json.Marshal(video)
if err != nil {
panic(err)
}
err = redisClient.Set(ctx, video.Id, videoBytes, 0).Err()
if err != nil {
panic(err)
}
}
Retrieving Data¶
Data is retrieved using the Get method, followed by unmarshalling the JSON bytes back into a Go struct^[400-devops__09-Scripting-Language__golang__introduction__part-5.database.redis__readme.md].
func getVideo(id string) (video, error) {
val, err := redisClient.Get(ctx, id).Result()
if err != nil {
return video{}, err
}
var v video
err = json.Unmarshal([]byte(val), &v)
return v, err
}
Networking Considerations¶
When running the Go application within a [[Docker]] container, networking configuration is critical. The application container must be on the same network as the Redis Sentinel containers to resolve container hostnames (e.g., sentinel-0)[^[400-devops__09-Scripting-Language__golang__introduction__part-5.database.redis__readme.md]]. This is achieved using the --net flag in the docker run command^[400-devops__09-Scripting-Language__golang__introduction__part-5.database.redis__readme.md].
Sources¶
^[400-devops__09-Scripting-Language__golang__introduction__part-5.database.redis__readme.md]