Routes writes to a Redis primary and eligible reads to replicas.
Redis.ReplicaSet can use explicitly configured replicas:
{:ok, redis} = Redis.ReplicaSet.start_link(
primary: {"redis-primary", 6379},
replicas: [{"redis-replica-1", 6379}, {"redis-replica-2", 6379}],
read_preference: :prefer_replica
)When :replicas is omitted, the process discovers online replicas from the
primary's INFO REPLICATION response and periodically refreshes them:
Redis.ReplicaSet.start_link(
primary: "redis://redis-primary:6379",
read_preference: :replica
)Redis' COMMAND metadata determines which commands are read-only. Unknown
commands are routed to the primary. Module or application-specific commands
can be added with :read_only_commands.
Read preferences
:master(default) - route every command to the primary:replica- route reads to replicas. If replicas are configured but all fail, return the replica error. If none are available, use the primary.:prefer_replica- route reads to replicas and fall back to the primary when replica execution fails
Writes, mixed pipelines, transactions, and unknown commands always use the primary. Reads are distributed round-robin across connected replicas.
Options
:primary- primary node as{host, port},"host:port", Redis URI, or connection keyword list (required):replicas- explicit list of replica nodes; omit for INFO discovery:read_preference-:master,:replica, or:prefer_replica:read_only_commands- extra command names that are safe on replicas:topology_refresh_interval- INFO refresh interval in milliseconds (default: 30_000; applies only to discovered topology)
Normal Redis.Connection options such as authentication, TLS, protocol,
database, credential provider, and timeout are shared by all nodes. A node
expressed as a keyword list or URI may override shared options.
Command, pipeline, and transaction calls accept response: :typed; see
Redis.Response.
Summary
Functions
Returns a child specification for a replica-set router.
Routes a command according to its mutability and the configured read preference.
Returns the current routing and topology state.
Routes an all-read pipeline to a replica and every other pipeline to the primary.
Refreshes replica connections and INFO-discovered topology.
Stops the replica-set router and all of its connections.
Executes a transaction on the primary.
Replaces the primary and replica addresses without restarting the router.
Types
@type node_spec() :: {String.t(), non_neg_integer()} | String.t() | keyword()
Functions
@spec child_spec(keyword()) :: Supervisor.child_spec()
Returns a child specification for a replica-set router.
@spec command(GenServer.server(), [term()], keyword()) :: {:ok, term()} | {:error, term()}
Routes a command according to its mutability and the configured read preference.
@spec info(GenServer.server()) :: map()
Returns the current routing and topology state.
@spec pipeline(GenServer.server(), [[term()]], keyword()) :: {:ok, [term()]} | {:error, term()}
Routes an all-read pipeline to a replica and every other pipeline to the primary.
@spec refresh(GenServer.server()) :: :ok | {:error, term()}
Refreshes replica connections and INFO-discovered topology.
@spec start_link(keyword()) :: GenServer.on_start()
@spec stop(GenServer.server()) :: :ok
Stops the replica-set router and all of its connections.
@spec transaction(GenServer.server(), [[term()]], keyword()) :: {:ok, [term()]} | {:error, term()}
Executes a transaction on the primary.
@spec update_topology(GenServer.server(), node_spec(), [node_spec()]) :: :ok | {:error, term()}
Replaces the primary and replica addresses without restarting the router.