Sentinel
Connect through Redis Sentinel so the client finds the current primary and follows failovers.
Redis Sentinel monitors a primary and its replicas and promotes a replica when the primary fails. layerSentinel asks the sentinels which node is the primary for a service name, connects to it, and checks that the node reports the primary role.
Example (Connecting Through Sentinel)
import { } from "effect";
import { } from "@effect-kit/redis";
import { } from "@effect-kit/redis/node";
const = .({
: "mymaster",
: [
{ : { : "localhost", : 26379 } },
{ : { : "localhost", : 26380 } },
],
});
const = .(function* () {
const = yield* ;
return yield* .("visits");
});
export const = .(.());RedisBun.layerSentinel takes the same input, and layerSentinelConfig takes an Effect Config of it. Sentinel is not available over Upstash HTTP.
Describing the sentinels
sentinels is a non-empty list. Each entry is a connection description such as { endpoint: { host, port } }. The port defaults to 26379. Sentinel endpoints must be TCP and use database 0, otherwise the layer fails with RedisConfigError.
data holds settings for the primary the client discovers: database, protocol, tls, limits and timeouts. You cannot give data an endpoint, because the endpoint is discovered.
Sentinels and the data nodes can use different identities:
credentialsandcredentialLifetimeauthenticate to the primary and replicas.sentinelCredentialsandsentinelCredentialLifetimeauthenticate to the sentinels.
See Credentials. A discovery authentication failure is SentinelError with reason: "authentication", and plane tells you whether the sentinels or the data nodes rejected it.
Provided services
The layer provides Redis, RedisSentinel, CommandExecutor, BatchExecutor, RedisCredentials and RedisSentinelCredentials.
Example (The Services a Sentinel Layer Provides)
import type { } from "effect";
import { } from "@effect-kit/redis/node";
const = .({
: "mymaster",
: [{ : { : "localhost" } }],
});
type Provided = .<typeof >;RedisSessions, RedisStreams, RedisScripts and RedisPubSub are not provided. Reach them through RedisSentinel.
Using RedisSentinel
RedisSentinel follows the current primary. It has withSession, withBlockingSession, watch, scripts, streams, admin, pubsub and tracking. Its sentinel(index) runs sentinel commands against one sentinel, and topology and refresh return the discovered view.
Example (Reading the Current Primary and Replicas)
import { } from "effect";
import { } from "@effect-kit/redis";
export const = .(function* () {
const = yield* ;
const { , } = yield* .;
return { , : . };
});Example (Watching a Key Through Sentinel)
import { } from "effect";
import { Commands, } from "@effect-kit/redis";
export const = .(function* () {
const = yield* ;
return yield* .(["counter"], () =>
.([Commands.("counter")]),
);
});Failover
The client refreshes the primary every refreshIntervalMillis, and when a command shows the node is no longer the primary, for example a READONLY reply. After a promotion, new commands go to the new primary.
What the client does with work that spans a failover:
- A session lease taken before the failover fails. Work that depended on it, such as a
WATCH, does not continue on the new primary. - An open Pub/Sub subscription fails with
SentinelError, and so does a client-side cache read. Subscribe again after the failover. - A command whose reply was lost fails with
ConnectionErroranddelivery: "possibly-written". The client does not replay it on the new primary. See Errors and retries.
Sentinel failover is asynchronous, and a write that was acknowledged by the old primary can still be lost. That is Redis behavior, not something the client can detect.
Errors
SentinelError has delivery (unsent or replied), plane (sentinel or data) and a reason:
reason | Meaning |
|---|---|
unreachable | No sentinel answered |
unknown-service | The sentinels do not know serviceName |
invalid-reply | A sentinel returned a reply the client cannot use |
role-mismatch | The discovered node does not report the expected role |
authentication | A sentinel or data node rejected the credentials |
data-unavailable | The primary was found but cannot be used |
capacity | Too many operations are waiting for discovery |
timeout | Discovery did not finish in discoveryTimeoutMillis |
closed | The client is closed |
topology-changed | The primary changed while work was in progress |
routing | The command cannot be routed through Sentinel |
Options
Pass these as sentinel next to serviceName and sentinels.
Prop
Type