Interview question
Implement and test a production-safe readiness check
Builds a bounded readiness check for one required dependency while keeping liveness independent and responses non-sensitive.
TL;DR
Builds a bounded readiness check for one required dependency while keeping liveness independent and responses non-sensitive.
ASP.NET Core health checks, readiness versus liveness, dependency timeouts, tags, and safe responses.
Practice the problem like a real interview: restate, reason, implement, and test.
Create a health check that calls a narrow CanConnectAsync-style probe through an abstraction and respects cancellation. Register it with the ready tag. Liveness must not call the database. Tests use a fake probe, never the real database.
When the required database is unavailable, readiness is unhealthy so the instance receives no traffic, while liveness still proves the process itself is running.
I keep the check small: one required dependency, cancellation, and a generic description. Route predicates select tagged readiness checks while liveness has no dependency checks. Operational thresholds still need tuning so one shared outage does not cause destructive restart loops.
public sealed class DatabaseReadinessCheck(IDatabaseProbe probe) : IHealthCheck
{
public async Task<HealthCheckResult> CheckHealthAsync(
HealthCheckContext context,
CancellationToken cancellationToken = default)
{
try
{
return await probe.CanConnectAsync(cancellationToken)
? HealthCheckResult.Healthy("Database is reachable.")
: HealthCheckResult.Unhealthy("Database is unavailable.");
}
catch (OperationCanceledException) when (cancellationToken.IsCancellationRequested)
{
return HealthCheckResult.Unhealthy("Database check timed out.");
}
catch
{
return HealthCheckResult.Unhealthy("Database check failed.");
}
}
}
services.AddHealthChecks()
.AddCheck<DatabaseReadinessCheck>("database", tags: ["ready"]);
app.MapHealthChecks("/health/ready", new HealthCheckOptions
{
Predicate = registration => registration.Tags.Contains("ready")
});
app.MapHealthChecks("/health/live", new HealthCheckOptions
{
Predicate = _ => false
});
Unit tests cover fake probe states; one deployment smoke check can verify route wiring. The readiness endpoint should be monitored for duration and flapping, not treated as a full business-flow test.
Move to the linked follow-up, next path step, prerequisite, or deeper variant.
Practice the next layer of the same subject.