redis

package
v0.40.0-rc.2 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Jan 26, 2026 License: MIT Imports: 13 Imported by: 0

README

Redis Backend

The Redis backend enables confd to retrieve configuration data from Redis. It supports string keys, hash fields, and pattern-based key retrieval.

Configuration

Basic Connection

Connect to Redis without authentication:

confd redis --node 127.0.0.1:6379 --onetime
Authentication
Password
confd redis --node 127.0.0.1:6379 --password secret --onetime

Or via environment variable:

export REDIS_PASSWORD=secret
confd redis --node 127.0.0.1:6379 --onetime
Database Selection

Specify a database number (default is 0):

confd redis --node 127.0.0.1:6379/4 --onetime
Unix Socket

Connect via Unix socket:

confd redis --node /var/run/redis/redis.sock --onetime
Key Separator

By default, confd uses / as the key separator. Use --separator to change this:

confd redis --node 127.0.0.1:6379 --separator : --onetime

This transforms /myapp/database/url to myapp:database:url when querying Redis.

Options

Flag Description Default
-n, --node Redis server address (host:port or socket path) -
--password Redis password -
--separator Character to replace / in keys /

Data Types

The Redis backend supports multiple data types:

Redis Type confd Behavior
String Returns value directly
Hash Returns all fields as nested keys
Keys (pattern) Scans matching keys
String Keys
redis-cli SET /myapp/database/url "db.example.com"

Access as /myapp/database/url.

Hash Fields
redis-cli HSET /myapp/database url "db.example.com" user "admin" password "secret"

Access fields as /myapp/database/url, /myapp/database/user, etc.

Basic Example

Set keys in Redis:

redis-cli SET /myapp/database/url "db.example.com"
redis-cli SET /myapp/database/user "admin"
redis-cli SET /myapp/database/password "secret123"

Create template resource (/etc/confd/conf.d/myapp.toml):

[template]
src = "myapp.conf.tmpl"
dest = "/etc/myapp/config.conf"
keys = [
  "/myapp/database",
]

Create template (/etc/confd/templates/myapp.conf.tmpl):

[database]
url = {{getv "/myapp/database/url"}}
user = {{getv "/myapp/database/user"}}
password = {{getv "/myapp/database/password"}}

Run confd:

confd redis --node 127.0.0.1:6379 --onetime

Advanced Example

Using Hash for Grouped Config

Store related config in a hash:

redis-cli HSET /myapp/database url "db.example.com" user "admin" password "secret"
redis-cli HSET /myapp/cache host "redis.example.com" port "6379"

Template:

[database]
url = {{getv "/myapp/database/url"}}
user = {{getv "/myapp/database/user"}}

[cache]
host = {{getv "/myapp/cache/host"}}
port = {{getv "/myapp/cache/port"}}
Using Custom Separator

If your Redis keys use : as separator:

redis-cli SET myapp:database:url "db.example.com"
confd redis --node 127.0.0.1:6379 --separator : --onetime

confd will transform /myapp/database/url to myapp:database:url.

Redis Sentinel (Manual)

Connect to a Redis instance behind Sentinel by specifying the master's address:

# Get master address from Sentinel
redis-cli -h sentinel1.example.com -p 26379 SENTINEL get-master-addr-by-name mymaster

# Use that address with confd
confd redis --node <master-ip>:<master-port> --watch

Watch Mode Support

Watch mode is supported for the Redis backend. confd uses Redis keyspace notifications via PubSub.

confd redis --node 127.0.0.1:6379 --watch
Enable Keyspace Notifications

Redis must be configured to emit keyspace notifications:

redis-cli CONFIG SET notify-keyspace-events AKE

Or in redis.conf:

notify-keyspace-events AKE
  • A - Alias for all events
  • K - Keyspace events
  • E - Keyevent events

confd watches for these events: set, del, append, rename_from, rename_to, expire, incrby, incrbyfloat, hset, hincrby, hincrbyfloat, hdel.

Per-Resource Backend Configuration

Instead of using the global backend, individual template resources can specify their own Redis backend configuration. This allows mixing backends within a single confd instance.

Add a [backend] section to your template resource file:

[template]
src = "myapp.conf.tmpl"
dest = "/etc/myapp/config.conf"
keys = [
  "/myapp/database",
]

[backend]
backend = "redis"
nodes = ["redis.example.com:6379"]
client_key = "secretpassword"
separator = ":"

Available backend options:

  • backend - Must be "redis"
  • nodes - Array of Redis server addresses (host:port or socket path)
  • client_key - Redis password
  • separator - Character to replace / in keys (default: /)

Connection Behavior

  • Connection timeout: 1 second
  • Automatic reconnection: Connections are tested with PING before use
  • Multiple nodes: Tries each node in order until one connects (no clustering support)

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

This section is empty.

Types

type Client

type Client struct {
	// contains filtered or unexported fields
}

Client is a wrapper around the redis client

func NewRedisClient

func NewRedisClient(machines []string, password string, separator string, dialTimeout, readTimeout, writeTimeout time.Duration, retryMaxAttempts int, retryBaseDelay, retryMaxDelay time.Duration) (*Client, error)

NewRedisClient returns an *redis.Client with a connection to named machines. It returns an error if a connection to the cluster cannot be made.

func (*Client) Close

func (c *Client) Close() error

Close closes the Redis client connections.

func (*Client) GetValues

func (c *Client) GetValues(ctx context.Context, keys []string) (map[string]string, error)

GetValues queries redis for keys prefixed by prefix.

func (*Client) HealthCheck

func (c *Client) HealthCheck(ctx context.Context) error

HealthCheck verifies the backend connection is healthy. It attempts to connect and ping the Redis server.

func (*Client) HealthCheckDetailed

func (c *Client) HealthCheckDetailed(ctx context.Context) (*types.HealthResult, error)

HealthCheckDetailed provides detailed health information for the redis backend.

func (*Client) WatchPrefix

func (c *Client) WatchPrefix(ctx context.Context, prefix string, keys []string, waitIndex uint64, stopChan chan bool) (uint64, error)

type RetryConfig

type RetryConfig struct {
	MaxRetries   int           // Maximum number of retry attempts (0 = no retries)
	BaseDelay    time.Duration // Initial backoff delay
	MaxDelay     time.Duration // Maximum backoff delay
	JitterFactor float64       // Jitter factor (0.0-1.0) to prevent thundering herd
}

RetryConfig contains configuration for connection retry behavior

func DefaultRetryConfig

func DefaultRetryConfig() RetryConfig

DefaultRetryConfig returns sensible default retry configuration

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL