The Redis backend enables confd to retrieve configuration data from Redis. It supports string keys, hash fields, and pattern-based key retrieval.
Connect to Redis without authentication:
confd redis --node 127.0.0.1:6379 --onetimeconfd redis --node 127.0.0.1:6379 --password secret --onetimeOr via environment variable:
export REDIS_PASSWORD=secret
confd redis --node 127.0.0.1:6379 --onetimeSpecify a database number (default is 0):
confd redis --node 127.0.0.1:6379/4 --onetimeConnect via Unix socket:
confd redis --node /var/run/redis/redis.sock --onetimeBy default, confd uses / as the key separator. Use --separator to change this:
confd redis --node 127.0.0.1:6379 --separator : --onetimeThis transforms /myapp/database/url to myapp:database:url when querying Redis.
| Flag | Description | Default |
|---|---|---|
-n, --node |
Redis server address (host:port or socket path) | - |
--password |
Redis password | - |
--separator |
Character to replace / in keys |
/ |
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 |
redis-cli SET /myapp/database/url "db.example.com"Access as /myapp/database/url.
redis-cli HSET /myapp/database url "db.example.com" user "admin" password "secret"Access fields as /myapp/database/url, /myapp/database/user, etc.
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 --onetimeStore 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"}}
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 : --onetimeconfd will transform /myapp/database/url to myapp:database:url.
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> --watchWatch mode is supported for the Redis backend. confd uses Redis keyspace notifications via PubSub.
confd redis --node 127.0.0.1:6379 --watchRedis must be configured to emit keyspace notifications:
redis-cli CONFIG SET notify-keyspace-events AKEOr in redis.conf:
notify-keyspace-events AKE
A- Alias for all eventsK- Keyspace eventsE- Keyevent events
confd watches for these events: set, del, append, rename_from, rename_to, expire, incrby, incrbyfloat, hset, hincrby, hincrbyfloat, hdel.
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 passwordseparator- Character to replace/in keys (default:/)
- 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)