Skip to article content
FrontendXLesson 11 / 16

Health checks and readiness

Move from a laptop shell to a safe, observable delivery pipeline: version control, Linux processes and permissions, container images, networking, configuration and secrets, deployment, health checks, observability, rollback, reproducibility and security hardening.

intermediate · about 50 minutes · 3 exercises

  • Predict
  • Implement
  • Debug

What you will be able to do

#
  • Distinguish liveness, readiness and startup checks.
  • Design health endpoints that do not cause restart storms.
  • Explain how a failing readiness check removes an instance from traffic.

Overview

#

An orchestrator decides whether to keep an instance running and whether to send it traffic based on health checks. Getting them wrong causes outages: a liveness check that depends on a database restarts every instance during a database blip.

The three checks

  • Liveness: is the process alive? A failure means restart it. It must not depend on external services.
  • Readiness: can it serve traffic right now? A failure removes it from the load balancer but does not restart it.
  • Startup: has it finished booting? It suppresses liveness failures during a slow start.

Endpoints

snippet.js
app.get('/healthz', (req, res) => res.status(200).send('ok'));       // liveness: process only
app.get('/readyz', async (req, res) => {
  const dbOk = await pool.query('SELECT 1').then(() => true).catch(() => false);
  res.status(dbOk ? 200 : 503).send(dbOk ? 'ready' : 'not ready');
});

Keep liveness shallow and readiness honest. A readiness check that always returns 200 is useless; one that checks a dependency is correct, because the instance genuinely cannot serve without it.

Removing an instance from traffic

When readiness fails, the load balancer stops routing new requests, but in-flight requests still finish. This is why a graceful shutdown drains connections after the readiness check starts failing.

Common pitfalls

  • A liveness check that queries the database, causing a restart storm when the database is slow.
  • A readiness check that is too deep, so a non-critical dependency marks the instance unhealthy.
  • No startup check, so a slow boot is killed by liveness before it finishes.

Exercises

#

Reference answers and expected output are authored, not executed in this browser. Nothing you type is run, and there is no Run or Pass control on this page.

  1. Exercise 1Predict

    A liveness probe calls a query that takes 30 seconds while the database is slow. The probe timeout is 2 seconds and failure threshold is 3. What happens?

    Read the code, then choose the best answer. The reference explanation stays hidden until you respond.
    stimulus.txt
    liveness: GET /healthz (queries DB, 30s) | timeout 2s | threshold 3
    
    Choose the best answer
  2. Exercise 2Implement

    Write a readiness handler that returns 200 only when the database responds within a short timeout, and 503 otherwise, without leaking the error detail.

    starter.javascript
    // GET /readyz handler
    

    Reveal the reference answer
    reference.javascript
    app.get('/readyz', async (req, res) => {
      try {
        await Promise.race([
          pool.query('SELECT 1'),
          new Promise((_, reject) => setTimeout(() => reject(new Error('timeout')), 500)),
        ]);
        res.status(200).send('ready');
      } catch {
        res.status(503).send('not ready');
      }
    });

    Authored expected output: 200 'ready' when the database answers in time, 503 'not ready' otherwise, with no error text in the body.

    The timeout bounds the check so a hanging dependency does not hang the probe. The response body stays generic so it does not disclose internal details.

  3. Exercise 3Debug

    During deploys, users see intermittent 502 errors even though the new instances pass readiness before receiving traffic. What is the missing step?

    starter.text
    # new instance passes readiness, then immediately gets killed on deploy
    

    Reveal the reference answer
    reference.text
    Add graceful shutdown: when the instance is told to stop, fail readiness first, wait for the load balancer to stop routing and for in-flight requests to drain, then exit. Without the drain window, the instance is removed from rotation and killed while requests are still in flight.

    Authored expected output: No 502s during rollout because connections drain before the process exits.

    Readiness on start is half the story. On stop you must also stop advertising readiness and allow existing connections to finish.

Common misconceptions

#
  • Liveness and readiness are interchangeable: liveness restarts, readiness removes from traffic.
  • A deeper health check is always better: depth in liveness turns a dependency blip into a restart storm.

Follow-up practice

#
  • devops-observability

Related resources

#

Your progress

#

Answers, drafts and completion marks are stored only in this browser. Nothing is sent to a server and no account is used.

Verification: authored. Answers and outputs on this page are authored reference material, not executed results.

© 2026 FrontendX. All rights reserved.