How would you design a health check for a service with multiple independent downstream dependencies where you want the overall status to reflect the worst individual result, but also need each dependency's specific status individually visible for diagnostics?
Short answer
Register each dependency as a separate named IHealthCheck (database, cache, message queue, external API), letting the built-in HealthCheckService automatically aggregate their statuses into a single overall status using worst-case precedence (Unhealthy > Degraded > Healthy) for…
Advertisement
AModel answer
Register each dependency as a separate named IHealthCheck (database, cache, message queue, external API), letting the built-in HealthCheckService automatically aggregate their statuses into a single overall status using worst-case precedence (Unhealthy > Degraded > Healthy) for the top-level response code, while a custom ResponseWriter exposes the full per-check breakdown in the JSON body -- giving both a simple binary signal for infrastructure (load balancer) and detailed diagnostics for humans investigating an issue, from the exact same underlying check registrations.
builder.Services.AddHealthChecks()
.AddCheck<DatabaseHealthCheck>("database")
.AddCheck<RedisHealthCheck>("cache")
.AddCheck<MessageQueueHealthCheck>("queue");
// Overall status = worst of the three; JSON body via custom ResponseWriter shows each individually
app.MapHealthChecks("/health", new HealthCheckOptions { ResponseWriter = WriteDetailedJson });Real-world example
An operations dashboard shows an API's overall status as Unhealthy at a glance (driven by worst-case aggregation), while a detailed drill-down view built from the same health check registrations pinpoints that specifically the message queue check is failing while database and cache remain healthy.
Likely follow-ups
- How does the aggregation logic handle a mix of Degraded and Unhealthy results across different checks?
- Would you ever want a non-worst-case aggregation strategy, and when?
Related topics