Course contentsShow
← Software Engineering
Lesson 1506 of 1,93344. Cloud Architecture PatternsFree lesson

Health Checks and Graceful Shutdown

Implement readiness and liveness probes so orchestrators can manage application lifecycle and traffic routing automatically.

Health Checks and Graceful Shutdown

You'll learn how to implement readiness and liveness probes so orchestrators can automatically manage your application's lifecycle and route traffic intelligently.

The Core Idea

In cloud-native environments, your application doesn't run in isolation—orchestrators like Kubernetes constantly monitor it and make routing decisions. Health checks tell the orchestrator whether your app is alive and ready to serve traffic. Graceful shutdown ensures your app finishes processing requests cleanly before terminating, preventing dropped connections and data loss.

Think of it like a restaurant: a "liveness" check confirms the kitchen hasn't caught fire, while a "readiness" check confirms the chef is actually ready to cook orders. When closing time comes, graceful shutdown means finishing existing orders before locking the doors—not abandoning diners mid-meal.

Two Types of Health Checks

Liveness probes answer: "Is this application alive, or should I restart it?" A failing liveness check triggers a container restart. This catches deadlocks, infinite loops, or corrupted state.

Readiness probes answer: "Is this instance ready to receive traffic?" A failing readiness check removes the instance from the load balancer rotation temporarily—without restarting it. Use this during startup (loading caches, warming up) or when dependencies are unavailable.

Graceful Shutdown Pattern

When receiving a termination signal, your application should:

  1. Stop accepting new requests (fail readiness checks immediately)
  2. Complete in-flight requests (respect a reasonable timeout)
  3. Close connections cleanly (database connections, file handles)
  4. Signal completion (exit with appropriate status code)

This allows zero-downtime deployments—new instances become ready before old ones shut down, ensuring continuous service availability.

Key Takeaway: Health checks enable orchestrators to automatically manage traffic routing and restarts, while graceful shutdown prevents data loss during deployments—both are essential for reliable, horizontally-scaled cloud-native applications.