ASP.NET Core Interview Question #172

What is the difference between liveness and readiness checks?

Logging, Error Handling & Background Services Mid-Level Intermediate

Quick Interview Answer

A liveness check answers whether the application process is alive enough that restarting it may be appropriate when the check fails. It should usually avoid fragile dependency checks that could cause unnecessary restart loops.

Detailed Explanation

A liveness check answers whether the application process is alive enough that restarting it may be appropriate when the check fails. It should usually avoid fragile dependency checks that could cause unnecessary restart loops.

A readiness check answers whether the instance is currently able to serve normal traffic. It can consider required dependencies or startup initialization.

Container orchestrators and load balancers can use these signals differently: liveness can trigger restart behavior, while readiness can temporarily remove an instance from traffic.

Code Example

builder.Services.AddHealthChecks()
    .AddCheck<DatabaseHealthCheck>(
        "database",
        tags: new[] { "ready" });

app.MapHealthChecks("/health/live",
    new HealthCheckOptions
    {
        Predicate = _ => false
    });

app.MapHealthChecks("/health/ready",
    new HealthCheckOptions
    {
        Predicate = check => check.Tags.Contains("ready")
    });