1

I want to implement a simple code, to save and retrieve some data from a local redis database running in docker.

My code to get and set the data is:

package main

import (
    "context"
        "fmt"
    "time"

    _ "github.com/lib/pq"

    "github.com/redis/go-redis/v9"
)

var redisClient = redis.NewFailoverClient(&redis.FailoverOptions{
    MasterName:    "mymaster",
    SentinelAddrs: []string{":26379"},
    DB:            0,
})

var ctx = context.Background()

func main() {
    _ = redisClient.Set(ctx, "123", "value123", 10*time.Second).Err()
    _ = redisClient.Set(ctx, "1234", "value1234", 10*time.Second).Err()
        fmt.Println("Finished")
}

My redis database has 1 master, 2 slaves and 3 sentinels:

version: "3.9"

services:
  redis-master:
    image: redis:latest
    container_name: redis-master
    hostname: redis-master
    command: redis-server --appendonly yes
    ports:
      - "6379:6379"
    volumes:
      - redis-master-data:/data
    networks:
      redis-network:
        aliases:
          - redis-master
    deploy:
      replicas: 1
      restart_policy:
        condition: any
    healthcheck:
      test: [ "CMD", "redis-cli", "ping" ]
      interval: 10s
      timeout: 5s
      retries: 3

  redis-slave:
    image: redis:latest
    command: redis-server --appendonly yes --slaveof redis-master 6379
    volumes:
      - redis-slave-data:/data
    networks:
      redis-network:
        aliases:
          - redis-slave
    deploy:
      replicas: 2
      restart_policy:
        condition: any
    ports:
      - "6380-6381:6379"

  redis-sentinel-1:
    image: redis:latest
    container_name: redis-sentinel-1
    hostname: redis-sentinel-1
    command: redis-sentinel /usr/local/etc/redis/sentinel.conf
    volumes:
      - ./sentinel.conf:/usr/local/etc/redis/sentinel.conf
    networks:
      redis-network:
        aliases:
          - redis-sentinel-1
    depends_on:
      - redis-master
      - redis-slave
    deploy:
      restart_policy:
        condition: any
    ports:
      - "26379:26379"

  redis-sentinel-2:
    image: redis:latest
    container_name: redis-sentinel-2
    hostname: redis-sentinel-2
    command: redis-sentinel /usr/local/etc/redis/sentinel.conf
    volumes:
      - ./sentinel.conf:/usr/local/etc/redis/sentinel.conf
    networks:
      redis-network:
        aliases:
          - redis-sentinel-2
    depends_on:
      - redis-master
      - redis-slave
    deploy:
      restart_policy:
        condition: any
    ports:
      - "26380:26379"

  redis-sentinel-3:
    image: redis:latest
    container_name: redis-sentinel-3
    hostname: redis-sentinel-3
    command: redis-sentinel /usr/local/etc/redis/sentinel.conf
    volumes:
      - ./sentinel.conf:/usr/local/etc/redis/sentinel.conf
    networks:
      redis-network:
        aliases:
          - redis-sentinel-3
    depends_on:
      - redis-master
      - redis-slave
    deploy:
      restart_policy:
        condition: any
    ports:
      - "26381:26379"

networks:
  redis-network:
    driver: bridge

volumes:
  redis-master-data:
  redis-slave-data:

Once I run the golang code, I get this output:

redis: 2023/05/29 15:14:38 sentinel.go:724: sentinel: discovered new sentinel="172.18.0.7:26379" for master="mymaster"
redis: 2023/05/29 15:14:38 sentinel.go:724: sentinel: discovered new sentinel="172.18.0.6:26379" for master="mymaster"
redis: 2023/05/29 15:14:38 sentinel.go:688: sentinel: new master="mymaster" addr="172.18.0.2:6379"

Looking good so far, as it received the master name and address. But I never get to the Set command, nor the fmt.Println("Finished") part.

Does somebody know, why I never the further than the first Set? Thank you very much for your input

  • You are probably having runtime failures but since you are ignoring errors it is not helpful helpful for debugging your code. Try instead `err = redisClient.Set(ctx, "123", "value123", 10*time.Second).Err()` then `if err != nil { panic(err) }` – Mit94 May 29 '23 at 15:01

0 Answers0