Redis DB is not respected when running in sentinel mode

Author: shkarfaceCreated Mar 16, 2023Updated Sep 5, 2026

Expected Behavior

When providing --redis-sentinel-connection-urls, the db parameter must be picked up from the connection strings and used on the NewFailoverClient func

Current Behavior

Currently, the db parameter is not respected when using redis sentinel and always defaults to 0, this value is respected for standalone redis and is not needed in redis cluster.

Possible Solution

Pass the DB param when initializing the FailoverClient:

go
// buildSentinelClient makes a redis.Client that connects to Redis Sentinel
// for Primary/Replica Redis node coordination
func buildSentinelClient(opts options.RedisStoreOptions) (Client, error) {
	addrs, opt, err := parseRedisURLs(opts.SentinelConnectionURLs)
	if err != nil {
		return nil, fmt.Errorf("could not parse redis urls: %v", err)
	}

	if err := setupTLSConfig(opts, opt); err != nil {
		return nil, err
	}

	client := redis.NewFailoverClient(&redis.FailoverOptions{
		MasterName:       opts.SentinelMasterName,
		SentinelAddrs:    addrs,
		SentinelPassword: opts.SentinelPassword,
		DB:               opt.DB,
		Password:         opts.Password,
		TLSConfig:        opt.TLSConfig,
		ConnMaxIdleTime:  time.Duration(opts.IdleTimeout) * time.Second,
               
               // pass down the DB from the redis.Options instance to fix the issue
		DB: 	           opt.DB,
	})
	return newClient(client), nil
}
  • Version used: 7.4.0

Source: oauth2-proxy/oauth2-proxy