skip to content

In ASP.NET Core 10, how do you serve separate liveness and readiness endpoints for a Kubernetes-hosted API using check tags and HealthCheckOptions.Predicate?

level: middleimportance: must knowfreq 60%

answer

  1. tags on registration, filter on endpoint
  2. Predicate is Func<HealthCheckRegistration, bool>
  3. Tags is a case-insensitive set
  4. _ => false runs no checks
  5. database check tagged ready only

basics

~10 s

Tag each registration, for example AddCheck("self", ..., tags: ["live"]) and AddDbContextCheck<ParkingDbContext>(tags: ["ready"]), then map two endpoints whose HealthCheckOptions.Predicate selects by tag: r => r.Tags.Contains("live") and r => r.Tags.Contains("ready").

solid answer

~40 s

Every registration method takes a `tags` argument, stored on `HealthCheckRegistration.Tags` as a case-insensitive set. I tag a cheap in-process check `live` and the database and sensor-feed checks `ready`. Then I map two endpoints: `app.MapHealthChecks("/health/live", new HealthCheckOptions { Predicate = r => r.Tags.Contains("live") })` and the same for `/health/ready` with `ready`. `Predicate` is a `Func<HealthCheckRegistration, bool>` evaluated per registration, so each endpoint runs only its subset. `Predicate = _ => false` runs nothing and always answers `Healthy`, which is a common liveness endpoint: it proves the process serves HTTP without touching dependencies. The Kubernetes probes then point at the two paths. Keeping the database off the liveness path means a database outage takes pods out of rotation instead of restarting them all.

code

csharp · 18 lines
csharp
using Microsoft.AspNetCore.Diagnostics.HealthChecks;
using Microsoft.Extensions.Diagnostics.HealthChecks;

builder.Services.AddHealthChecks()
    .AddCheck("self", () => HealthCheckResult.Healthy(), tags: ["live"])
    .AddCheck<SensorFeedHealthCheck>("sensor-feed", tags: ["ready"])
    .AddDbContextCheck<ParkingDbContext>(tags: ["ready"]);

var app = builder.Build();

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

go deeper

for a junior

Know that checks can carry tags and that each MapHealthChecks endpoint can pick its checks with Predicate.

for a middle

Build the live and ready endpoints from tags, explain the _ => false liveness trick, and say why the database check belongs on readiness.

for a senior

Spot the outage pattern where a shared dependency on liveness restarts every pod, and keep liveness in-process and cheap.

for a principal

Set a fleet convention for tag names and endpoint paths so platform probes and dashboards work the same across every service.

## The scenario A **parking-sensor API** runs as several pods on Kubernetes. It reads bay occupancy from PostgreSQL through an EF Core `ParkingDbContext` and receives readings from street sensors. The platform wants two endpoints: - a **liveness** endpoint that answers "is this process stuck?" and should stay green while the process can serve HTTP at all; - a **readiness** endpoint that answers "can this pod serve real traffic right now?" and should go red while the database is unreachable. What each probe *does* on failure is the platform's concern. The ASP.NET Core question is how one app exposes two different selections of checks. ## Step 1: tag the registrations Every registration method on `IHealthChecksBuilder` accepts `tags`: - `AddCheck("self", () => HealthCheckResult.Healthy(), tags: ["live"])` (a delegate check); - `AddCheck<SensorFeedHealthCheck>("sensor-feed", tags: ["ready"])` (a class check); - `AddDbContextCheck<ParkingDbContext>(tags: ["ready"])` (from `Microsoft.Extensions.Diagnostics.HealthChecks.EntityFrameworkCore`). Tags end up on **`HealthCheckRegistration.Tags`**, an `ISet<string>` built with `StringComparer.OrdinalIgnoreCase`, so `"Ready"` and `"ready"` match. A registration can carry several tags. ## Step 2: filter per endpoint with `Predicate` `HealthCheckOptions.Predicate` is a **`Func<HealthCheckRegistration, bool>`**. The health check service evaluates it for every registration and runs only those that return `true`: | Endpoint | Predicate | Runs | |---|---|---| | `/health/live` | `r => r.Tags.Contains("live")` | only the `self` check | | `/health/ready` | `r => r.Tags.Contains("ready")` | database and sensor feed | | `/health/live` (alternative) | `_ => false` | nothing; always `Healthy` | | `/health` (diagnostic) | `null` (the default) | every check | Two facts make the `_ => false` variant work: a report with no entries is `Healthy`, and the endpoint still exercises routing and the request pipeline, so a deadlocked or crashed process still fails it. The predicate can test anything on the registration, not only tags: `r.Name`, `r.FailureStatus` or `r.Timeout`. ## Step 3: point the probes at the paths The pod spec's `livenessProbe` uses `/health/live` and its `readinessProbe` uses `/health/ready`. ASP.NET Core does not know or care which probe calls which path; the only contract is the HTTP status, 200 for `Healthy` or `Degraded` and 503 for `Unhealthy` by default. ## Why the database belongs on readiness only If the `ParkingDbContext` check were tagged `live`, a database outage would fail liveness on **every** pod at once, and the platform would restart all of them, which cannot fix a database and adds startup load when it returns. Tagged `ready`, the outage only takes pods out of the load balancer until the database is back. ## Trying the split locally With the database stopped, the two endpoints disagree on purpose: - `curl -i http://localhost:8080/health/live` returns `200` with `Healthy`, because only the in-process `self` check runs; - `curl -i http://localhost:8080/health/ready` returns `503` with `Unhealthy`, because the `ParkingDbContext` check fails and the aggregate is the worst result. Start the database again and readiness returns to 200 without a restart. That is exactly the behaviour the platform needs: the process kept running, and only its traffic eligibility changed. ## Variations worth knowing - A registration can carry **several tags**, for example `["ready", "db"]`, so a diagnostic endpoint can select `db` checks alone. - A **startup** tag is a common third group: checks that only matter until warm-up finishes, such as loading the bay map into memory, served on a `/health/startup` endpoint. - Predicates can combine conditions: `r => r.Tags.Contains("ready") && r.Name != "sensor-feed"`. ## Common mistakes 1. **Forgetting the predicate.** `MapHealthChecks("/health/live")` with no options runs every check, database included. 2. **Tagging by accident.** A check registered with no tags is invisible to both tag predicates; that is fine for a diagnostic-only check, but surprising when it was meant to gate readiness. 3. **Expensive liveness checks.** Liveness runs often; keep it in-process. 4. **Assuming tags filter the publisher too.** `IHealthCheckPublisher` has its own `HealthCheckPublisherOptions.Predicate`; endpoint predicates do not affect it. ## Where Aspire fits Aspire's service-defaults project ships its own helper for health endpoints, which is a separate topic; this answer covers the plain ASP.NET Core mechanism that helper builds on.

  • What does an endpoint mapped with Predicate = _ => false prove, and what does it not?
    It runs no checks, so the report is empty and therefore `Healthy`. A 200 still proves Kestrel accepted the connection and the request went through routing to the endpoint, so a crashed or hung process fails it. It proves nothing about the database, the sensor feed or any other dependency.
  • A check is tagged "Ready" but the predicate tests r.Tags.Contains("ready"). Does it run?
    Yes. `HealthCheckRegistration` builds its `Tags` set with `StringComparer.OrdinalIgnoreCase`, so `Contains("ready")` matches `Ready`. Spelling mistakes still break it, since there is no validation that a tag used in a predicate was ever registered.

  • Tag matching in a health check Predicate is case-sensitive.
  • MapHealthChecks with no options runs only checks that have no tags.
  • Putting the database check on the liveness endpoint makes recovery faster.
  • Predicate = _ => false makes the endpoint return 503 because nothing ran.
  • Endpoint predicates also decide which checks IHealthCheckPublisher runs.