Medium15 minKoa.js Fundamentals
UpdatedAug 5, 2026
Edit

Koa Health Checks

Question Variations

  • "What is the difference between liveness and readiness in Koa?"
  • "Why fail readiness before calling `server.close()`?"
  • "Should a health check make an unbounded database query?"

Why This Is Asked

Health endpoints determine whether orchestrators restart a process or send it traffic. This question tests whether you understand readiness, liveness, and graceful shutdown from an HTTP service’s perspective.

Key Concepts

  • Liveness detects an unhealthy process and should be lightweight.
  • Readiness determines whether the instance can accept new traffic.
  • Readiness should fail during initialization and draining.
  • Dependency probes should be bounded and selected intentionally.

Question Variations

  • “What is the difference between liveness and readiness in Koa?”
  • “Why fail readiness before calling server.close()?”
  • “Should a health check make an unbounded database query?”

Answers by Technology

+ Add Variant
Koa.jsImprove this answer ✏️

Expected Answer (Koa 3.2.1 / Node.js 18+)

Expose a lightweight liveness endpoint to show the process is running and a readiness endpoint to show the instance may receive traffic. Set readiness false during initialization and before graceful shutdown starts. Keep dependency probes focused and bounded: an unbounded fan-out can turn a downstream incident into a restart storm.

Why It Matters

Accurate probe semantics keep load balancers from routing traffic to processes that are starting or draining.

Code Example

import Koa, { Context } from 'koa';

const app = new Koa(); let ready = false;
app.use((ctx: Context) => {
  if (ctx.path === '/livez') { ctx.status = 200; ctx.body = { ok: true }; return; }
  if (ctx.path === '/readyz') { ctx.status = ready ? 200 : 503; ctx.body = { ready }; return; }
  ctx.status = 404;
});
ready = true; app.listen(3000);

Common Mistakes

  • Using a deep database check for liveness: Transient dependency outages can restart healthy processes.
  • Leaving readiness true while stopping: New requests arrive during drain.

Follow-up Questions

  • When should readiness be true? (Answer: After required initialization completes.)
  • Why keep liveness cheap? (Answer: It should detect a wedged process without amplifying failures.)