Skip to main content
PreparationZone Logo
PrepZone

Graceful Shutdown and Lifecycle

Drain in-flight requests, SmartLifecycle hooks, and Kubernetes termination grace.

Pass checkpoint below

Read these first

Why this matters

  • Kubernetes sends SIGTERM — apps must drain in-flight HTTP and stop accepting new work.
  • Hard kills mid-checkout cause duplicate charges and angry customers.
  • Actuator and Spring Boot 3 lifecycle hooks are the production answer interviewers want.

Graceful shutdown

L1 · Fresher

server.shutdown=graceful stops accepting new HTTP work and waits for in-flight requests to finish.

Kubernetes termination

L2 · Intermediate

Set terminationGracePeriodSeconds longer than your longest request plus shutdown hooks.

SmartLifecycle

L3 · Advanced

Beans implement ordered startup/shutdown for Kafka consumers and batch schedulers.

SIGTERM handling

L4 · Expert

Coordinate with load balancers to drain connections before SIGKILL — readiness probe should fail first.

SIGTERM
Stop accept
Drain requests
Context closed
SIGTERM stops accepting new work, drains in-flight requests, then closes the context.

Hook

Kubernetes sends SIGTERM during a BookStore rollout. In-flight POST /api/orders must finish, message listeners should stop accepting new work, and the JVM exits before the pod kill timeout — graceful shutdown wires Spring’s lifecycle to platform signals.

Server and context shutdown

Java
server:
  shutdown: graceful
spring:
  lifecycle:
    timeout-per-shutdown-phase: 30s

Boot 3.5 stops accepting new HTTP connections, waits for active requests, then closes the application context in defined phases.

The rest of this guide is part of Pro

Free covers every fresher guide in full. Pro unlocks Senior, Staff and Architect guides across every track.

Still to come · 6 sections

  • SmartLifecycle beans
  • RabbitMQ and Kafka listeners
  • BookStore in action
  • Runnable BookStore lab
  • Failure modes in production
  • PreStop hook

Test yourself

Answer these before moving on — recall is what makes it stick.