简体   繁体   English

ASP.NET 核心分布式 Redis 缓存:断开

[英]ASP.NET Core Distributed Redis Cache: Disconnect

I'am using Redis cache as distributed cache in ASP.NET app.我在 ASP.NET 应用程序中使用 Redis 缓存作为分布式缓存。 It works until Redis server becomes unavailable and the question is: How to properly handle disconnection issues?它一直有效,直到 Redis 服务器不可用,问题是:如何正确处理断开连接问题?

Redis is configured this way (Startup.cs): Redis 是这样配置的(Startup.cs):

services.AddDistributedRedisCache(...)

Option AbortOnConnectFail is set to false选项 AbortOnConnectFail 设置为 false

Injected in service via constructor:通过构造函数注入服务:

...
private IDistributedCache _cache

public MyService(IDistributedCache cache)
{
  _cache = cache;
}

When Redis is down the following code throws an exception (StackExchange.Redis.RedisConnectionException: SocketFailure on 127.0.0.1:6379/Subscription...):当 Redis 关闭时,以下代码会引发异常(StackExchange.Redis.RedisConnectionException: SocketFailure on 127.0.0.1:6379/Subscription...):

var val = await _cache.GetAsync(key, cancellationToken);

I don't think that using reflection to inspect a connection state inside _cache object is a good way.我不认为使用反射来检查_cache object 内部的连接 state 是一个好方法。 So are there any 'right' options to handle it?那么是否有任何“正确”的选择来处理它?

Maybe you can check Polly Project .也许您可以查看Polly Project It has Retry/WaitAndRetry/RetryForever and Circuit Breakers that can be handy.它有 Retry/WaitAndRetry/RetryForever 和可以方便的断路器。 So you can catch that RedisConnectionException And then retry or fallback to other method.所以你可以捕捉到那个RedisConnectionException ,然后重试或者回退到其他方法。

You have Plugin for Microsoft DistributedCache Provider.您有 Microsoft DistributedCache Provider 插件。

Check it out.一探究竟。

First of all, why is your Redis server becoming unavailable?首先,为什么您的 Redis 服务器不可用? And for how long?并且持续多久? You should minimize these kinds of situations.您应该尽量减少此类情况。 Do you use Redis as a service from AWS ie ElasticCache?您是否使用 Redis 作为 AWS 的服务,即 ElasticCache? If so you can configure it to promote a new Redis slave /read-replice server to become a master if the first master fails.如果是这样,您可以将其配置为在第一个主服务器出现故障时将新的 Redis 从 /read-replice 服务器升级为主服务器。

To improve fault tolerance and reduce write downtime, enable Multi-AZ with Automatic Failover for your Redis (cluster mode disabled) cluster with replicas.要提高容错能力并减少写入停机时间,请为具有副本的 Redis(已禁用集群模式)集群启用具有自动故障转移的多可用区。 For more information, see Minimizing downtime in ElastiCache for Redis with Multi-AZ.有关更多信息,请参阅使用多可用区的 Redis 最大限度地减少 ElastiCache 中的停机时间。

https://docs.aws.amazon.com/AmazonElastiCache/latest/red-ug/AutoFailover.html https://docs.aws.amazon.com/AmazonElastiCache/latest/red-ug/AutoFailover.html

Apart from that, a fallback solution to an unresponsive Redis server would be just to retrieve the objects/entities that your a caching in Redis from the database if the Redis server is down.除此之外,如果 ZE111446745A1825B862F8727AE63BCE 服务器关闭,则对无响应的 Redis 服务器的后备解决方案将只是从数据库中检索您缓存在 Redis 中的对象/实体。 You can retry the Redis call two times with 5 seconds between each retry and if the server is still down you should just query the database.您可以重试 Redis 调用两次,每次重试之间间隔 5 秒,如果服务器仍然关闭,您应该只查询数据库。 This would result in a performance hit but it is a better solution than throwing an error.这会导致性能下降,但它是比抛出错误更好的解决方案。

T val = null;
int retryCount = 0;

do 
{
      try 
      {
          val = await _cache.GetAsync(key, cancellationToken);
      }
      catch(Exception ex) 
      {  
         retryCount++;
         Thread.Sleep(retryCount * 2000)
      }
 }
 while(retryCount < 3 && val == null);
 
 if (val == null) 
 {
    var = call to database
 }

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM