Sentinel-aware Redis connection.
Queries sentinels to discover the current primary or replica, then maintains a connection. On disconnection, re-queries sentinels to find the (possibly new) primary.
With :read_preference, the client maintains primary and replica
connections, routes eligible reads to replicas, and keeps writes and
transactions on the primary:
{:ok, conn} = Redis.Sentinel.start_link(
sentinels: [{"sentinel1", 26379}, {"sentinel2", 26379}],
group: "mymaster",
read_preference: :prefer_replica
)Usage
{:ok, conn} = Redis.Sentinel.start_link(
sentinels: [{"sentinel1", 26379}, {"sentinel2", 26379}],
group: "mymaster",
role: :primary,
password: "secret"
)
# Use like a normal connection — failover is transparent
{:ok, "OK"} = Redis.Sentinel.command(conn, ["SET", "key", "value"])Options
:sentinels- list of sentinel addresses as{host, port}tuples or"host:port"strings (required):group- sentinel group name (required):role-:primaryor:replica(default::primary):read_preference-:master(default),:replica, or:prefer_replica. Cannot be combined withrole: :replica.:read_only_commands- extra command names that are safe to run on replicas:password- password for the Redis server (not the sentinel):sentinel_password- password for sentinel connections:sentinel_username- username for sentinel connections:username- username for the Redis server:database- database number:timeout- connection timeout ms (default: 5_000):sentinel_timeout- sentinel query timeout ms (default: 500):topology_refresh_interval- Sentinel topology refresh interval in milliseconds for replica routing (default: 30_000;nildisables it):name- GenServer name registration:protocol-:resp3or:resp2(default::resp3)
Command, pipeline, and transaction calls accept response: :typed; see
Redis.Response.
Summary
Functions
Returns a specification to start this module under a supervisor.
Sends a command through the sentinel-managed connection.
Returns info about the current connection.
Refreshes primary and replica topology from Sentinel.
Functions
Returns a specification to start this module under a supervisor.
See Supervisor.
@spec command(GenServer.server(), [String.t()], keyword()) :: {:ok, term()} | {:error, term()}
Sends a command through the sentinel-managed connection.
@spec info(GenServer.server()) :: map()
Returns info about the current connection.
@spec pipeline(GenServer.server(), [[String.t()]], keyword()) :: {:ok, [term()]} | {:error, term()}
@spec refresh(GenServer.server()) :: :ok | {:error, term()}
Refreshes primary and replica topology from Sentinel.
@spec start_link(keyword()) :: GenServer.on_start()
@spec stop(GenServer.server()) :: :ok
@spec transaction(GenServer.server(), [[String.t()]], keyword()) :: {:ok, [term()]} | {:error, term()}