Redis Cache Connection Resiliency – Best Practices

Redis Cache Connection Resiliency

Today in this article we shall see how to implement Redis Cache Connection Resiliency with examples in the .NET application.

We will use StackExchange.Redis NuGet package for getting supported connection manager class for performing connection or re-connections or timeout configuration.

Connection resiliency is a critical component of application communication that uses shared resources (particularly in the cloud) because these resources are more vulnerable to transient faults.

The StackExchange.Redis client uses a connection manager class that is configured through a set of options, including:

Configuring the ConnectionMultiplexer with resiliency options

Below is a sample example of how to configure the ConnectionMultiplexer with resiliency options. Below we have used an instance of the configuration options class, with required options defined and passing it to the Connect method.

Retry policies are configured before connecting to the cache.

Connection Resiliency for Redis- Retry with exponential backoff

The below example shows a retry strategy using exponential backoff.

  


                var options = new ConfigurationOptions
                {
                    EndPoints = { $"{Configuration.GetValue<string>("RedisCache:Host")}: 
                                {Configuration.GetValue<int>("RedisCache:Port")}" },

                    ConnectRetry = 5,

                    ReconnectRetryPolicy = new ExponentialRetry(deltaBackOffms , 
                                                                maxdeltaBackOffms),

                    ConnectTimeout = 1000

                };

                var redisMultiplexer = ConnectionMultiplexer.Connect(options);

           

Above time in milliseconds can be defined as the below example,

                var deltaBackOffms = 

                Convert.ToInt32(TimeSpan.FromSeconds(5).TotalMilliseconds);

                var maxdeltaBackOffms = 

                Convert.ToInt32(TimeSpan.FromSeconds(20).TotalMilliseconds);

ConfigurationOptions discussed above are defined as below,

  • ConnectRetry – The number of times to repeat connect attempts during the initial connection operation. The default value is 3.
  • ReconnectRetryPolicy. The retry strategy to use. The default value is LinearRetry 5000 ms
  • ConnectTimeout. The maximum waiting time is in milliseconds.

Connection Resiliency for Redis – Retry using string options

Alternatively, we can define configuration via string declaration as below

         
           var configOptions = $"{host},connectRetry=3,connectTimeout=1000";
           var redisMultiplexer = ConnectionMultiplexer.Connect(configOptions);

           services.AddSingleton<IConnectionMultiplexer>(redisMultiplexer);

Where ‘host‘ is defined as below,

var host = $"{Configuration.GetValue("RedisCache:Host")}:
{Configuration.GetValue("RedisCache:Port")}";

Above we are making use of the configuration to read the apsetting.json file,

For more details: Configuration in ASP.NET Core

Using ConnectionMultiplexer via Dependency Injection

if you are using ASP.NET Core or MVC application, then ConnectionMultiplexer via Dependency Injection can be injected by defining the service scope in the API pipeline as below,

services.AddSingleton(redisMultiplexer);

For more details: Redis-Dependency Injection of the ConnectionMultiplexer

Redis Retry guidelines

Consider the following guidelines when using Azure Cache for Redis,

  • Retry policies become effective when establishing a connection to the cache (when the application first starts.)

  • It’s recommended to use falling back by accessing an original data source. This is preferred over a number of retry attempts.

Other useful references :

Do you have any comments or ideas or any better suggestions to share?

Please sound off your comments below.

Happy Coding !!



Please bookmark this page and share it with your friends. Please Subscribe to the blog to receive notifications on freshly published(2024) best practices and guidelines for software design and development.



Leave a Reply

Your email address will not be published. Required fields are marked *