Ronaldo Lanhellas
Ronaldo Lanhellas

Reputation: 3356

Trying to use Redis Sentinel with Net Core 2.1

Well, I'm using Net Core 2.1 with lib Microsoft.Extensions.Caching.Redis ver 2.1.2, and when I'm working with only one Redis node, everything works fine.

Working with single node:

services.AddDistributedRedisCache(ops =>
            {
                ops.Configuration = "localhost:6379";
                ops.InstanceName = "master";
            });

But now, I'm trying to use Redis with Sentinel, so I changed my configuration to (following this guide https://stackexchange.github.io/StackExchange.Redis/Configuration.html):

 services.AddDistributedRedisCache(ops =>
            {
                ops.Configuration = "localhost,serviceName:mymaster";
                ops.InstanceName = "master";
            });

So, I have a docker-compose.yml to startup redis nodes with sentinel:

version: '2'
services:
  redis-master:
    image: bitnami/redis:5.0
    environment:
      ALLOW_EMPTY_PASSWORD: 'yes'
      REDIS_REPLICATION_MODE: 'master'
    ports:
      - '6379:6379'
  redis-slave1:
    image: bitnami/redis:5.0
    environment:
      ALLOW_EMPTY_PASSWORD: 'yes'
      REDIS_REPLICATION_MODE: 'slave'
      REDIS_MASTER_HOST: redis-master
    depends_on:
      - redis-master
    ports:
      - '6380:6380'
  redis-slave2:
    image: bitnami/redis:5.0
    environment:
      ALLOW_EMPTY_PASSWORD: 'yes'
      REDIS_REPLICATION_MODE: 'slave'
      REDIS_MASTER_HOST: redis-master
    depends_on:
      - redis-master
    ports:
      - '6381:6381'
  redis-sentinel:
    image: bitnami/redis-sentinel:5.0
    environment:
      REDIS_MASTER_HOST: redis-master
      REDIS_SENTINEL_DOWN_AFTER_MILLISECONDS: 10000
    ports:
      - '26379:26379'

When I startup my application everything works fine, so I shutdown the master node to test sentinel and got the following error in Asp Net Core:

RedisConnectionException: SocketClosed on localhost:6379/Subscription, origin: ProcessReadBytes, input-buffer: 0, outstanding: 0, last-read: 14s ago, last-write: 14s ago, unanswered-write: 1998113s ago, keep-alive: 60s, pending: 0, state: ConnectedEstablished, in: 0, ar: 0, last-heartbeat: 0s ago, last-mbeat: 0s ago, global: 0s ago
Unknown location

RedisConnectionException: No connection is available to service this operation: EVAL; SocketClosed on localhost:6379/Subscription, origin: ProcessReadBytes, input-buffer: 0, outstanding: 0, last-read: 14s ago, last-write: 14s ago, unanswered-write: 1998113s ago, keep-alive: 60s, pending: 0, state: ConnectedEstablished, in: 0, ar: 0, last-heartbeat: 0s ago, last-mbeat: 0s ago, global: 0s ago
StackExchange.Redis.ConnectionMultiplexer.ThrowFailed<T>(TaskCompletionSource<T> source, Exception unthrownException) in ConnectionMultiplexer.cs, line 2000

Well, if I start master node again, everything works, but this is not expected behavior. I expected that after shutdown master node, sentinel promote a slave to master, and I can continue using redis without errors.

Am I missing somehting ?

Upvotes: 2

Views: 2879

Answers (1)

grimmdp
grimmdp

Reputation: 271

Sorry for the late reply, but we're just getting this going ourselves. When connecting to Sentinel, you need to specify the port in your connection string:

services.AddDistributedRedisCache(ops =>
{
    ops.Configuration = "localhost:26379,serviceName:mymaster";
    ops.InstanceName = "master";
});

Once the ConnectionMultiplexer connects to the Sentinel node, it gets the master node and uses it. Also, I have found that several settings are necessary to make this work:

redisOptions.TieBreaker = "";
redisOptions.AbortOnConnectFail = false;
redisOptions.AllowAdmin = true;

We are also using StackExchange.Redis v2.2.4 on .NET Core 3.1

Upvotes: 1

Related Questions