Skip to content
Written with Claude
IMPORTANT

As you may notice, this page and pretty much the entire website were obviously created with the help of AI. I wonder how you could tell? Was it a big "Written With Claude" badge on every page? I moved it to the top now (with the help of AI of course) to make it even more obvious. There are a few blogposts that were written by me manually, the old-fashioned way, I hope there will be more in the future, and those have a similar "Human Written" badge. This project (not the website), on the other hand, is a very, very different story. It took me more than two years of painstaking and unpaid work in my own free time. A story that, hopefully, I will tell someday. But meanwhile, what would you like me to do? To create a complex documentation website with a bunch of highly technical articles with the help of AI and fake it, to give you an illusion that I also did that manually? Like the half of itnernet is doing at this point? How does that makes any sense? Is that even fair to you? Or maybe to create this website manually, the old-fashioned way, just for you? While working a paid job for a salary, most of you wouldn't even get up in the morning. Would you like me to sing you a song while we're at it? For your personal entertainment? Seriously, get a grip. Do you find this information less valuable because of the way this website was created? I give my best to fix it to keep the information as accurate as possible, and I think it is very accurate at this point. If you find some mistakes, inaccurancies or problems, there is a comment section at the bottom of every page, which I also made with the help of the AI. And I woould very much appreciate if you leave your feedback there. Look, I'm just a guy who likes SQL, that's all. If you don't approve of how this website was constructed and the use of AI tools, I suggest closing this page and never wever coming back. And good riddance. And I would ban your access if I could know how. Thank you for your attention to this matter.

Health Checks

New in 3.6.0

Health Checks middleware was added in version 3.6.0.

Health check endpoints for container orchestration (Kubernetes, Docker Swarm) and monitoring systems to determine if the application is running correctly.

Overview

json
{
  "HealthChecks": {
    "Enabled": false,
    "CacheDuration": "5 seconds",
    "Path": "/health",
    "ReadyPath": "/health/ready",
    "LivePath": "/health/live",
    "IncludeDatabaseCheck": true,
    "ConnectionName": null
  }
}

Settings Reference

SettingTypeDefaultDescription
EnabledboolfalseEnable health check endpoints.
CacheDurationstring"5 seconds"Cache health check responses for the specified duration. PostgreSQL interval format. Set to null to disable caching.
Pathstring"/health"Path for the main health check endpoint that reports overall status.
ReadyPathstring"/health/ready"Path for the readiness probe endpoint.
LivePathstring"/health/live"Path for the liveness probe endpoint.
IncludeDatabaseCheckbooltrueInclude PostgreSQL database connectivity in health checks.
ConnectionNamestringnullUse a specific named connection for health checks. When null, uses the default connection.

Health Check Types

NpgsqlRest provides three types of health check endpoints:

Main Health (/health)

Reports the overall health status by combining all checks.

Response:

  • 200 OK with "Healthy" or "Degraded" status
  • 503 Service Unavailable with "Unhealthy" status

Readiness Probe (/health/ready)

Indicates whether the application is ready to receive traffic. Used by Kubernetes to know when a pod is ready to be added to the service load balancer.

Includes:

  • Database connectivity check (when IncludeDatabaseCheck is true)

Response:

  • 200 OK if ready to accept traffic
  • 503 Service Unavailable if not ready (e.g., database unreachable)

Liveness Probe (/health/live)

Indicates whether the application process is running. Used by Kubernetes to know when to restart a pod.

Does NOT include:

  • Database checks (a slow database shouldn't trigger a container restart)

Response:

  • 200 OK if the application process is responding

Cache Duration

Health check responses are cached server-side to prevent excessive database queries:

json
{
  "HealthChecks": {
    "Enabled": true,
    "CacheDuration": "5 seconds"
  }
}

The value uses PostgreSQL interval format:

  • "5 seconds" or "5s"
  • "1 minute" or "1min"
  • "30s"

Set to null to disable caching:

json
{
  "HealthChecks": {
    "CacheDuration": null
  }
}

TIP

Query strings are ignored to prevent cache-busting attacks.

Database Health Check

When IncludeDatabaseCheck is true, the readiness probe verifies PostgreSQL connectivity:

json
{
  "HealthChecks": {
    "Enabled": true,
    "IncludeDatabaseCheck": true
  }
}

If the database is unreachable:

  • /health/ready returns 503 Service Unavailable
  • /health/live still returns 200 OK (the app is running, just can't reach the database)

Using a Different Connection

Use a specific named connection for health checks:

json
{
  "ConnectionStrings": {
    "Default": "Host=primary;Database=myapp;...",
    "HealthCheck": "Host=replica;Database=myapp;..."
  },
  "HealthChecks": {
    "Enabled": true,
    "ConnectionName": "HealthCheck"
  }
}

This is useful when you want to:

  • Use a read-only connection for health checks
  • Query a different database server
  • Use credentials with limited permissions

Kubernetes Integration

Deployment Configuration

yaml
apiVersion: apps/v1
kind: Deployment
metadata:
  name: npgsqlrest-app
spec:
  template:
    spec:
      containers:
      - name: app
        image: vbilopav/npgsqlrest:latest
        ports:
        - containerPort: 8080
        livenessProbe:
          httpGet:
            path: /health/live
            port: 8080
          initialDelaySeconds: 5
          periodSeconds: 10
          failureThreshold: 3
        readinessProbe:
          httpGet:
            path: /health/ready
            port: 8080
          initialDelaySeconds: 5
          periodSeconds: 5
          failureThreshold: 3

Probe Behavior

ProbeChecksFailure Action
LivenessApp process respondingRestart container
ReadinessApp + DatabaseRemove from load balancer

WARNING

Don't use /health/ready for liveness probes. A database outage would cause all pods to restart, making recovery harder.

Docker Compose Health Check

yaml
services:
  api:
    image: vbilopav/npgsqlrest:latest
    healthcheck:
      test: ["CMD", "curl", "-f", "http://localhost:8080/health"]
      interval: 30s
      timeout: 10s
      retries: 3
      start_period: 10s

Custom Paths

Customize the health check paths:

json
{
  "HealthChecks": {
    "Enabled": true,
    "Path": "/api/health",
    "ReadyPath": "/api/health/readiness",
    "LivePath": "/api/health/liveness"
  }
}

Example Configurations

Basic Configuration

json
{
  "HealthChecks": {
    "Enabled": true
  }
}

Uses all defaults: database check enabled, 5-second cache, standard paths.

Production with Caching

json
{
  "HealthChecks": {
    "Enabled": true,
    "CacheDuration": "10 seconds",
    "IncludeDatabaseCheck": true
  }
}

API Gateway Integration

json
{
  "HealthChecks": {
    "Enabled": true,
    "Path": "/healthz",
    "ReadyPath": "/readyz",
    "LivePath": "/livez",
    "CacheDuration": "3 seconds"
  }
}

Uses Kubernetes-style paths (/healthz, /readyz, /livez).

Without Database Check

json
{
  "HealthChecks": {
    "Enabled": true,
    "IncludeDatabaseCheck": false
  }
}

All probes return healthy if the app process is responding. Useful if you have separate database monitoring.

Response Format

Health check endpoints return plain text responses:

Healthy

Or:

Unhealthy

With corresponding HTTP status codes:

  • 200 OK - Healthy or Degraded
  • 503 Service Unavailable - Unhealthy

Next Steps

Comments

Released under the MIT License.