Search code examples
amazon-web-servicesasp.net-coreredis

Fail to Connect to Serverless Redis Cache from ASP.NET Core ECS Task


Receiving the following error when attempting to access a serverless Redis cache:

StackExchange.Redis.RedisConnectionException: The message timed out in the backlog attempting to send because no connection became available (5000ms) - Last Connection Exception: UnableToConnect

My high-level setup is:

  • VPC
  • An ECS Fargate Task in the VPC.
  • A serverless Redis Cache in the VPC.
  • The cache and task are on the same subnets.
  • The cache security group has an inbound rule allowing access from the task.

I am attempting to access it from an ASP.NET Core application using the AddStackExchangeRedisCache extension from the Microsoft.Extensions.Caching.StackExchangeRedis package.

Not sure what else is missing for the task to be able to reach the cache.


Solution

  • After a lot of tweaking and looking around I finally noticed that the serverless caches have Encryption in Transit enabled by default.

    At the same time, I was not explicitly setting Ssl to true in my ConfigurationOptions when adding the Redis service in my application startup. By default this is false, and so there was a mismatch between the cache and my server in regards to encryption.

    The final solution for adding the service resembles:

    services.AddStackExchangeRedisCache(delegate (RedisCacheOptions options)
    {
        options.ConfigurationOptions = new ConfigurationOptions()
        {
            EndPoints = new EndPointCollection { "your-cache.serverless.use1.cache.amazonaws.com:6379" },
            Ssl = true,
            ConnectRetry = 3,
            ReconnectRetryPolicy = new LinearRetry(250),
            AbortOnConnectFail = false,
            ConnectTimeout = 500,
            SyncTimeout = 500
        };
    });