Redis health check in ASP.NET Core WebApi

Checking availability of distributed Redis cache instances in ASP.NET Core WebApi

Even before popularity of micro service approach, for even monolithic applications you needed to have health-checks (Google Compute Engine) or health-probes (Microsoft Azure) for load balancing.

Basically in load balancing scenario, these endpoints are used to tell load balancer whether instance can handle requests or not. Since handling of requests may consider several components, these endpoints either return 200 OK HTTP response or any other response like 500 Internal Server Error with optional payload message describing the issue and each component status.

Microsoft has it's repository on GitHub https://github.com/dotnet-architecture/HealthChecks which is designed for .NET Core. Apart from basic interface infrastructure, it has couple of health-check implementations like SqlServer and URL health-check.

Note

This repository, although quite useful it is still not part of the official .NET Core nuget packages gallery. If you want to use it you can get if from GitHub and add as projects or reference the build output libraries

One of the common components of web services is distributed cache and it is very common that Redis is used for this purpose. For this kind of check I wrote and implementation which is trying to get redis server info using ServiceStack.Redis.Core nuget package

using System;
using ServiceStack.Redis;
using System.Linq;
namespace Microsoft.Extensions.HealthChecks
{
    public static class HealthCheckBuilderRedisExtensions
    {
        public static HealthCheckBuilder AddRedisCheck(this HealthCheckBuilder builder, string name, string host, int port, string password = null)
        {
            Guard.ArgumentNotNull(nameof(builder), builder);
            return AddRedisCheck(builder, name, builder.DefaultCacheDuration, host, port, password);
        }


        public static HealthCheckBuilder AddRedisCheck(this HealthCheckBuilder builder, string name, TimeSpan cacheDuration, string host, int port, string password = null)
        {
            builder.AddCheck($"RedisCheck({name})", () =>
            {
                try
                {
                    using (var client = new RedisClient(host, port, password))
                    {
                        var response = client.Info;

                        if (response != null && response.Any())
                        {
                            return HealthCheckResult.Healthy($"RedisCheck({name}): Healthy");
                        }
                        return HealthCheckResult.Unhealthy($"RedisCheck({name}): Unhealthy");

                    }
                }
                catch (Exception ex)
                {
                    return HealthCheckResult.Unhealthy($"RedisCheck({name}): Exception during check: {ex.GetType().FullName}");
                }
            }, cacheDuration);

            return builder;
        }
    }
}

    

This health-check is part of repository which I cloned from dontnet-architecture/HealthChecks repository https://github.com/dejanstojanovic/HealthChecks where you can find the full the whole project with this redis health-check class class implementation.

Note

For this health-check I used ServiceStack.Redis.Core nuget package, but if you are using any other library for communicating with redis in your .NET project. List of available redis libraries for C# you can find at https://redis.io/clients#c

References

Disclaimer

Purpose of the code contained in snippets or available for download in this article is solely for learning and demo purposes. Author will not be held responsible for any failure or damages caused due to any other usage.


About the author

DEJAN STOJANOVIC

Dejan Stojanovic is a passionate Software Architect/Developer. He is highly experienced in .NET programming platform includion ASP.NET MVC and WebApi. He likes working on new technologies and exciting challenging projects

CONNECT WITH DEJAN  Loginlinkedin Logintwitter Logingoogleplus Logingoogleplus

JavaScript

read more

SQL/T-SQL

read more

Umbraco CMS

read more

PowerShell

read more

Comments for this article