Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
58 changes: 58 additions & 0 deletions content/develop/clients/dotnet/connect.md
Original file line number Diff line number Diff line change
Expand Up @@ -67,6 +67,64 @@ db.StringSet("foo", "bar");
Console.WriteLine(db.StringGet("foo")); // prints bar
```

## Connect to Redis Sentinel

To connect through [Redis Sentinel](/content/operate/oss_and_stack/management/sentinel.md),
list the Sentinels in `EndPoints` and set `ServiceName` to the name of the primary that
the Sentinels monitor. The client asks the Sentinels for the current primary.

```csharp
using StackExchange.Redis;

ConfigurationOptions conf = new ConfigurationOptions {
EndPoints = { "localhost:26379", "localhost:26380", "localhost:26381" },
ServiceName = "mymaster"
};

ConnectionMultiplexer redis = ConnectionMultiplexer.Connect(conf);
IDatabase db = redis.GetDatabase();

db.StringSet("foo", "bar");
Console.WriteLine(db.StringGet("foo")); // prints bar
```

You can also use a configuration string, such as
`"localhost:26379,localhost:26380,localhost:26381,serviceName=mymaster"`.

If the Sentinels and the data nodes have different credentials, connect to the Sentinels
first with `SentinelConnect()`, using the Sentinel credentials. Then call
`GetSentinelMasterConnection()` with the credentials for the primary and replicas:

```csharp
using StackExchange.Redis;

// Connect to the Sentinels with the Sentinel credentials.
ConfigurationOptions sentinelConf = new ConfigurationOptions {
EndPoints = { "localhost:26379", "localhost:26380", "localhost:26381" },
User = "yourSentinelUsername",
Password = "yourSentinelPassword"
};
ConnectionMultiplexer sentinel = ConnectionMultiplexer.SentinelConnect(sentinelConf);

// Connect to the primary with the Redis credentials.
ConfigurationOptions conf = new ConfigurationOptions {
ServiceName = "mymaster",
User = "yourUsername",
Password = "yourPassword"
};
ConnectionMultiplexer redis = sentinel.GetSentinelMasterConnection(conf);
IDatabase db = redis.GetDatabase();
```

The connection also includes the replicas that the Sentinels report. To read from a
replica, pass `CommandFlags.PreferReplica` (or `CommandFlags.DemandReplica`) to a read-only
command. Replication is asynchronous, so a replica can return stale data.

```csharp
// Read from a replica if one is available, otherwise from the primary.
Console.WriteLine(db.StringGet("foo", CommandFlags.PreferReplica)); // prints bar
```

## Connect to your production Redis with TLS

When you deploy your application, use TLS and follow the [Redis security](/content/operate/oss_and_stack/management/security/_index.md) guidelines.
Expand Down
39 changes: 39 additions & 0 deletions content/develop/clients/go/connect.md
Original file line number Diff line number Diff line change
Expand Up @@ -80,6 +80,45 @@ client := redis.NewClusterClient(&redis.ClusterOptions{
})
```

## Connect to Redis Sentinel

To connect through [Redis Sentinel](/content/operate/oss_and_stack/management/sentinel.md),
use `NewFailoverClient()`. Set `MasterName` to the name of the primary that the Sentinels
monitor, and list one or more Sentinels in `SentinelAddrs`. The client asks the Sentinels
for the current primary and switches to the new one after a failover.

```go
client := redis.NewFailoverClient(&redis.FailoverOptions{
MasterName: "mymaster",
SentinelAddrs: []string{"localhost:26379", "localhost:26380", "localhost:26381"},
})
```

The Sentinels and the data nodes have separate credentials. Set `SentinelPassword` for the
Sentinels, and `Username` and `Password` for the primary and replicas:

```go
client := redis.NewFailoverClient(&redis.FailoverOptions{
MasterName: "mymaster",
SentinelAddrs: []string{"localhost:26379", "localhost:26380", "localhost:26381"},
SentinelPassword: "sentinel-secret", // use your Sentinel password
Username: "default", // use your Redis user
Password: "secret", // use your Redis password
})
```

To send commands to a replica, set `ReplicaOnly`. The client sends every command to a
random replica, including writes, which fail, so use this option only for a client that
reads:

```go
replica := redis.NewFailoverClient(&redis.FailoverOptions{
MasterName: "mymaster",
SentinelAddrs: []string{"localhost:26379", "localhost:26380", "localhost:26381"},
ReplicaOnly: true,
})
```

## Connect to your production Redis with TLS

When you deploy your application, use TLS and follow the
Expand Down
6 changes: 6 additions & 0 deletions content/develop/clients/hiredis/connect.md
Original file line number Diff line number Diff line change
Expand Up @@ -145,3 +145,9 @@ pending commands execute and activate their callbacks.
Use `redisAsyncFree()` to disconnect immediately. If you do this then
any pending callbacks from commands that have already executed will be
called with a `NULL` reply pointer.

## Redis Sentinel

`hiredis` doesn't support [Redis Sentinel](/content/operate/oss_and_stack/management/sentinel.md).
It has no API to find the current primary from a set of Sentinels or to reconnect after a
failover.
62 changes: 62 additions & 0 deletions content/develop/clients/ioredis/connect.md
Original file line number Diff line number Diff line change
Expand Up @@ -66,6 +66,68 @@ const redis = new Redis.Cluster([
]);
```

## Connect to Redis Sentinel

To connect through [Redis Sentinel](/content/operate/oss_and_stack/management/sentinel.md),
pass a list of Sentinels in the `sentinels` option and the name of the primary that the
Sentinels monitor in `name`. The client asks the Sentinels for the current primary.

```js
const redis = new Redis({
sentinels: [
{ host: "localhost", port: 26379 },
{ host: "localhost", port: 26380 },
{ host: "localhost", port: 26381 },
],
name: "mymaster",
});

await redis.set('foo', 'bar');
const value = await redis.get('foo');
console.log(value); // >>> bar
```

The Sentinels and the data nodes have separate credentials. Set `sentinelPassword` (and
`sentinelUsername`, if needed) for the Sentinels, and `username` and `password` for the
primary and replicas:

```js
const redis = new Redis({
sentinels: [
{ host: "localhost", port: 26379 },
{ host: "localhost", port: 26380 },
{ host: "localhost", port: 26381 },
],
name: "mymaster",
// Credentials for the Sentinel instances.
sentinelPassword: "my-sentinel-password",
// Credentials for the primary and replica data nodes.
username: "default",
password: "my-password",
});
```

If the Sentinel password is wrong or missing, the error says that all Sentinels are
unreachable.

By default, ioredis doesn't listen for failover announcements from the Sentinels. After a
failover, the client keeps using the old primary until Sentinel reconfigures it as a
replica and closes the connection. Any writes that the old primary accepts during that time
are lost. Set `failoverDetector: true` to switch to the new primary as soon as the Sentinels
announce it.

To connect to a replica instead of the primary, set `role: "slave"`. The client connects to
one replica, chosen at random. Replication is asynchronous, so a replica can return stale
data.

```js
const replica = new Redis({
sentinels: [{ host: "localhost", port: 26379 }],
name: "mymaster",
role: "slave",
});
```

## Connect to your production Redis with TLS

When you deploy your application, use TLS and follow the [Redis security](/content/operate/oss_and_stack/management/security/_index.md) guidelines.
Expand Down
62 changes: 62 additions & 0 deletions content/develop/clients/jedis/connect.md
Original file line number Diff line number Diff line change
Expand Up @@ -76,6 +76,68 @@ jedisClusterNodes.add(new HostAndPort("127.0.0.1", 7380));
RedisClusterClient jedis = RedisClusterClient.create(jedisClusterNodes);
```

### Connect to Redis Sentinel

To connect through [Redis Sentinel](/content/operate/oss_and_stack/management/sentinel.md),
use `RedisSentinelClient`. Pass the name of the primary that the Sentinels monitor to
`masterName()`, and the Sentinel addresses to `sentinels()`. The client asks the Sentinels
for the current primary and switches to the new one after a failover.

```java
import redis.clients.jedis.HostAndPort;
import redis.clients.jedis.RedisSentinelClient;

import java.util.HashSet;
import java.util.Set;

//...

Set<HostAndPort> sentinels = new HashSet<>();
sentinels.add(new HostAndPort("localhost", 26379));
sentinels.add(new HostAndPort("localhost", 26380));
sentinels.add(new HostAndPort("localhost", 26381));

RedisSentinelClient jedis = RedisSentinelClient.builder()
.masterName("mymaster")
.sentinels(sentinels)
.build();

jedis.set("foo", "bar");
System.out.println(jedis.get("foo")); // bar

jedis.close();
```

The Sentinels and the data nodes have separate credentials. Pass a client configuration for
the primary to `clientConfig()`, and one for the Sentinels to `sentinelClientConfig()`:

```java
import redis.clients.jedis.DefaultJedisClientConfig;
import redis.clients.jedis.JedisClientConfig;

//...

// Credentials for the primary and replica nodes.
JedisClientConfig masterConfig = DefaultJedisClientConfig.builder()
.password("secret!") // use your Redis password
.build();

// Credentials for the Sentinel nodes.
JedisClientConfig sentinelConfig = DefaultJedisClientConfig.builder()
.password("sentinelSecret!") // use your Sentinel password
.build();

RedisSentinelClient jedis = RedisSentinelClient.builder()
.masterName("mymaster")
.sentinels(sentinels)
.clientConfig(masterConfig)
.sentinelClientConfig(sentinelConfig)
.build();
```

`RedisSentinelClient` always sends commands to the primary. It doesn't support reading
from replicas.

### Connect to your production Redis with TLS

When you deploy your application, use TLS and follow the [Redis security](/content/operate/oss_and_stack/management/security/_index.md) guidelines.
Expand Down
76 changes: 76 additions & 0 deletions content/develop/clients/lettuce/connect.md
Original file line number Diff line number Diff line change
Expand Up @@ -66,6 +66,82 @@ try (RedisClusterClient clusterClient = RedisClusterClient.create(redisURI)) {

Learn more about Cluster connections and how to configure them in [the reference guide](https://redis.github.io/lettuce/ha-sharding/#redis-cluster).

## Connect to Redis Sentinel

To connect through [Redis Sentinel](/content/operate/oss_and_stack/management/sentinel.md),
build a `RedisURI` with `RedisURI.Builder.sentinel()`, passing the address of one Sentinel
and the name of the primary that the Sentinels monitor. Add the other Sentinels with
`withSentinel()`. The client asks the Sentinels for the current primary.

```java
import io.lettuce.core.RedisClient;
import io.lettuce.core.RedisURI;
import io.lettuce.core.api.StatefulRedisConnection;
import io.lettuce.core.api.sync.RedisCommands;

//...

RedisURI redisUri = RedisURI.Builder
.sentinel("localhost", 26379, "mymaster")
.withSentinel("localhost", 26380)
.withSentinel("localhost", 26381)
.build();

try (RedisClient client = RedisClient.create(redisUri)) {
StatefulRedisConnection<String, String> connection = client.connect();
RedisCommands<String, String> commands = connection.sync();

commands.set("foo", "bar");
System.out.println(commands.get("foo")); // >>> bar

connection.close();
}
```

The Sentinels and the data nodes have separate credentials. Pass the Sentinel password with
each Sentinel address, and set the credentials for the primary and replicas with
`withAuthentication()`:

```java
RedisURI redisUri = RedisURI.Builder
// The password argument authenticates to each Sentinel.
.sentinel("localhost", 26379, "mymaster", "yourSentinelPassword")
.withSentinel("localhost", 26380, "yourSentinelPassword")
.withSentinel("localhost", 26381, "yourSentinelPassword")
// withAuthentication() authenticates to the primary and replicas.
.withAuthentication("default", "yourPassword")
.build();
```

A `RedisClient` connection doesn't listen for failover announcements from the Sentinels.
After a failover, it keeps using the old primary until Sentinel reconfigures it as a
replica and closes the connection. Any writes that the old primary accepts during that time
are lost.

To read from replicas, open a connection with `MasterReplica.connect()` and set a
`ReadFrom` policy. This connection also listens for failover announcements from the
Sentinels, so it switches to a new primary sooner. Replication is asynchronous, so a
replica can return stale data.

```java
import io.lettuce.core.ReadFrom;
import io.lettuce.core.codec.StringCodec;
import io.lettuce.core.masterreplica.MasterReplica;
import io.lettuce.core.masterreplica.StatefulRedisMasterReplicaConnection;

//...

try (RedisClient client = RedisClient.create()) {
StatefulRedisMasterReplicaConnection<String, String> connection =
MasterReplica.connect(client, StringCodec.UTF8, redisUri);
connection.setReadFrom(ReadFrom.REPLICA);

System.out.println(connection.sync().get("foo")); // >>> bar

connection.close();
}
```

## Asynchronous connection

```java
Expand Down
Loading
Loading