You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

.NET 6升级后连接AWS Redis遇写入超时问题求助

连接AWS Redis实例时出现写入超时问题(.NET 6升级后)

问题重现代码

var option = new Microsoft.Extensions.Caching.StackExchangeRedis.RedisCacheOptions
{
    ConfigurationOptions = ConfigurationOptions.Parse("master.test-redis-cluster.89run.use1.cache.amazonaws.com:6379,ssl=true,sslProtocols=Tls,abortConnect=false"),
    InstanceName = "testapi",
};
IDistributedCache distributedCache = new RedisCache(option);

distributedCache.SetAsync("testKey", System.Text.Encoding.UTF8.GetBytes("testData"));

错误日志

{
    "Redis-Message": "HMGET testapi1fb31d10-ebee-4f39-9a73-9bc8901461e5",
    "Redis-Timeout": "5000",
    "Redis-OpsSinceLastHeartbeat": "0",
    "Redis-Queue-Awaiting-Write": "0",
    "Redis-Queue-Awaiting-Response": "0",
    "Redis-Active-Writer": "False",
    "Redis-Backlog-Writer": "CheckingForTimeout",
    "Redis-Server-Endpoint": "master.test-redis-cluster.89run.use1.cache.amazonaws.com:6379",
    "Redis-Multiplexer-Connects": "1/1/0",
    "Redis-Manager": "10 of 10 available",
    "Redis-Client-Name": "testapi-app-deployment-7674cf88nnl5(SE.Redis-v2.6.70.49541)",
    "Redis-ThreadPool-IO-Completion": "(Busy=0,Free=1000,Min=16,Max=1000)",
    "Redis-ThreadPool-Workers": "(Busy=0,Free=32767,Min=16,Max=32767)",
    "Redis-ThreadPool-Items": "(Threads=16,QueuedItems=0,CompletedItems=495)",
    "Redis-Busy-Workers": "0",
    "Redis-Version": "2.6.70.49541",
    "redis-command": "HMGET testapi1fb31d10-ebee-4f39-9a73-9bc8901461e5",
    "redis-server": "master.test-redis-cluster.89run.use1.cache.amazonaws.com:6379",
    "ex_msg": "The timeout was reached before the message could be written to the output buffer, and it was not sent, command=HMGET, timeout: 5000, inst: 0, qu: 0, qs: 0, aw: False, bw: CheckingForTimeout, serverEndpoint: master.test-redis-cluster.89run.use1.cache.amazonaws.com:6379, mc: 1/1/0, mgr: 10 of 10 available, clientName: testapi-app-deployment-7674cf88nnl5(SE.Redis-v2.6.70.49541), IOCP: (Busy=0,Free=1000,Min=16,Max=1000), WORKER: (Busy=0,Free=32767,Min=16,Max=32767), POOL: (Threads=16,QueuedItems=0,CompletedItems=495), v: 2.6.70.49541 (Please take a look at this article for some common client-side issues that can cause timeouts: https://stackexchange.github.io/StackExchange.Redis/Timeouts)"
}

已排查情况

  • 无连接错误,确认已成功连接Redis
  • 排除大负载可能性(错误日志无Payload大小相关信息)
  • 已尝试Stack Overflow同类解决方案但无效
  • API从.NET 3.1升级到6后出现问题,服务完全无法运行
  • 线程池资源充足,增加超时时间无效果

解决方案

1. 升级StackExchange.Redis版本

当前使用的2.6.70版本对.NET 6的兼容性不足,建议升级到2.7.x及以上稳定版,新版本修复了多个与.NET 6适配的连接问题。

2. 调整Redis配置选项(适配AWS环境)

修改配置,明确指定TLS协议版本、优化超时设置,并添加AWS Redis所需的解析配置:

var configOptions = ConfigurationOptions.Parse("master.test-redis-cluster.89run.use1.cache.amazonaws.com:6379");
configOptions.Ssl = true;
// .NET 6推荐使用TLS1.2或TLS1.3
configOptions.SslProtocols = SslProtocols.Tls12;
configOptions.AbortOnConnectFail = false;
// 延长连接和同步超时时间
configOptions.ConnectTimeout = 10000;
configOptions.SyncTimeout = 10000;
// 启用DNS解析,适配AWS Redis集群的域名解析
configOptions.ResolveDns = true;

var redisCacheOptions = new RedisCacheOptions
{
    ConfigurationOptions = configOptions,
    InstanceName = "testapi"
};

3. 使用DI正确注册RedisCache

.NET 6中建议通过依赖注入注册Redis服务,避免手动实例化RedisCache导致的连接复用问题:

// Program.cs中注册服务
builder.Services.AddStackExchangeRedisCache(options =>
{
    options.ConfigurationOptions = ConfigurationOptions.Parse("master.test-redis-cluster.89run.use1.cache.amazonaws.com:6379");
    options.ConfigurationOptions.Ssl = true;
    options.ConfigurationOptions.SslProtocols = SslProtocols.Tls12;
    options.ConfigurationOptions.AbortOnConnectFail = false;
    options.ConfigurationOptions.ConnectTimeout = 10000;
    options.ConfigurationOptions.SyncTimeout = 10000;
    options.ConfigurationOptions.ResolveDns = true;
    options.InstanceName = "testapi";
});

// 在需要使用的类中注入IDistributedCache
public class YourService
{
    private readonly IDistributedCache _distributedCache;

    public YourService(IDistributedCache distributedCache)
    {
        _distributedCache = distributedCache;
    }

    public async Task TestCache()
    {
        await _distributedCache.SetAsync("testKey", System.Text.Encoding.UTF8.GetBytes("testData"));
    }
}

4. 验证服务器端状态

  • 检查AWS Redis实例的CPU、内存使用率,确认服务器未过载
  • 确认安全组规则允许当前应用所在的EC2/ECS实例访问Redis的6379端口
  • 如果是Redis集群模式,确保客户端配置启用集群支持(添加configOptions.ClusterConfig = new ClusterConfiguration();)

内容的提问来源于stack exchange,提问作者Mahesh Jadhav

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.08.13 23:10:25