.NET › Diagnostics & Performance

What is the purpose of health check response caching and how does frequent health check polling itself impact application performance?

Intermediate

Answer

Health checks, if not designed carefully, can themselves become a performance concern under frequent polling (e.g., a Kubernetes liveness probe hitting the endpoint every few seconds) -- especially if a health check performs an expensive operation like a full database query on every invocation, so it's common to cache health check results for a short period or design checks to be lightweight (a simple connectivity ping rather than a full query) to avoid the diagnostic mechanism itself becoming a load concern.
builder.Services.AddHealthChecks()
    .AddCheck<DatabaseHealthCheck>("database", tags: new[] { "ready" });

// A well-designed health check avoids expensive operations:
public class DatabaseHealthCheck : IHealthCheck {
    public async Task<HealthCheckResult> CheckHealthAsync(HealthCheckContext context, CancellationToken ct) {
        await _db.Database.CanConnectAsync(ct);  // cheap connectivity check, not a full query
        return HealthCheckResult.Healthy();
    }
}
Real-world example A team discovers their Kubernetes liveness probe polling every 5 seconds was inadvertently generating significant additional database load because the health check performed a full table scan query instead of a lightweight connectivity check.

Common follow-ups: How would you cache health check results to reduce polling overhead?;What's the right balance between health check thoroughness and its own performance cost?