Kest

Search

Search the documentation

Reliability

Health checks

Tell the load balancer and Kubernetes whether this instance can serve traffic.

A health check is a URL that answers one question: is this instance OK? Whoever runs your application (Kubernetes, a load balancer, a monitoring tool) calls it every few seconds. It answers 200 when healthy and 503 when not, and the caller acts: it stops sending traffic to the sick instance, or restarts it.

app.http(adapter) {
    useHealth("/health") {
        check("database") { orm.connection("main").ping() }          // throws or returns false = down
        memoryHeap(800_000_000)                                      // down when more than 800 MB of heap is in use
        disk("/", maxUsedFraction = 0.9)                             // down when the disk is more than 90% full
        httpPing("payments", "https://pay.example/status")           // down unless it answers with a status below 500
    }
}
GET /health   ->  200
{ "status": "ok", "info":    { "database": { "status": "up" }, "disk": { "status": "up", "usedFraction": 0.41 } },
  "error": {}, "details": { ... } }
 
GET /health   ->  503     (the database is down)
{ "status": "error", "error": { "database": { "status": "down", "message": "Connection refused" } }, ... }

The route skips your global guards, so probes need no token. It is not cached.

The two questions: alive and ready

Kubernetes (and most platforms) ask two different things, and mixing them up causes outages:

ProbeQuestionIf it failsWhat to check
liveness"Is the process stuck beyond repair?"the instance is restartedalmost nothing: just that the process answers
readiness"Can it serve requests right now?"the instance gets no traffic (but is not restarted)the database, caches, required services

Do not put the database in the liveness check. If the database has a ten-minute outage, every instance would be restarted over and over, which makes recovery slower. Use two endpoints:

app.http(adapter) {
    useHealth("/health/live") { }                                              // no checks: answers 200 while the process runs
    useHealth("/health/ready") {
        ormConnections(orm)                                                    // one check per configured database connection (package health-orm)
        check("redis") { redis.ping() }
    }
}
# Kubernetes
livenessProbe:  { httpGet: { path: /health/live,  port: 8080 }, periodSeconds: 10, failureThreshold: 3 }
readinessProbe: { httpGet: { path: /health/ready, port: 8080 }, periodSeconds: 5,  failureThreshold: 2 }
startupProbe:   { httpGet: { path: /health/ready, port: 8080 }, periodSeconds: 2,  failureThreshold: 60 }   # waits for slow start-up

Writing your own check

useHealth("/health/ready") {
    check("queue-backlog") {                    // return false or throw to be down
        queue.waiting("emails") < 10_000
    }
    indicator {                                  // build the whole result, with details
        val lag = consumer.lagSeconds()
        HealthIndicatorResult("consumer-lag", lag < 60, mapOf("lagSeconds" to lag))
    }
}

A check that throws is reported as down with the exception message, it never crashes the endpoint. Keep checks fast (a few hundred milliseconds): a probe that takes longer than its timeout counts as a failure. Checks run one after the other on each request, so many slow checks add up.

Good practice

  • Check dependencies you cannot work without. A broken optional dependency (a recommendation service) should degrade a feature, not take the instance out of rotation.
  • Keep the endpoint cheap and safe to call often: a ping query, not a count over a big table.
  • Do not expose details publicly. The body lists dependency names and error messages. Put the health port behind your network, or serve only the status to the outside.
  • Call a health report from code with HealthCheckService.check { ... }, for example to refuse to start a job when a dependency is down.

Testing

HealthCheckService.check { check("x") { false } }.ok is false and does not need a server.

Read more