Microservice Patterns
Health Checks
Let the platform ask each instance "can you serve traffic?" so dead or warming instances are taken out of rotation — without building a health check that takes down the whole fleet.
What you will learn
- Distinguish liveness, readiness and startup checks and who acts on each.
- See how active probes remove a hung instance that passive signals miss.
- Avoid the deep-health-check trap that ejects every instance at once.
- Configure Spring Boot Actuator probes and Kubernetes health checks.
Before you start
Overview
A restaurant manager walks past every station before the doors open and during service: "Are you ready? Are you okay?" A cook who has burned their hand is sent home; a cook still setting up is not given orders yet. Health checks are that walk-around for servers.
A health check is an endpoint (or command) that reports whether an instance can do its job. Load balancers and orchestrators call it periodically and act on the answer: stop routing traffic to the instance, or restart it.
Liveness, readiness, startup
| Check | Question | Action on failure |
|---|---|---|
| Liveness | Is the process stuck beyond repair (deadlock, broken event loop)? | Restart the instance. |
| Readiness | Should this instance receive traffic right now (warmed up, not overloaded, not shutting down)? | Remove it from the load balancer until it passes again. |
| Startup | Has a slow-starting app finished booting? | Delay liveness checks so the app is not killed while starting. |
Run it
A hung instance accepts connections but never answers, so passive error counting may never notice it. Compare A and B. Scenario C is the most common real-world mistake; scenario D shows readiness protecting a rolling deploy.
Implementation: Spring Boot Actuator + Kubernetes
management:
endpoint:
health:
probes:
enabled: true # /actuator/health/liveness and /actuator/health/readiness
group:
readiness:
include: readinessState # do NOT add db here unless the app is useless without it
endpoints:
web:
exposure:
include: healthcontainers:
- name: shop-api
image: shop-api:1.42.0
startupProbe:
httpGet: { path: /actuator/health/liveness, port: 8080 }
periodSeconds: 5
failureThreshold: 30 # allow up to 150 s to start
livenessProbe:
httpGet: { path: /actuator/health/liveness, port: 8080 }
periodSeconds: 10
failureThreshold: 3
readinessProbe:
httpGet: { path: /actuator/health/readiness, port: 8080 }
periodSeconds: 2
failureThreshold: 2Advanced Spring Boot marks readiness as REFUSING_TRAFFIC during graceful shutdown, so Kubernetes stops routing before in-flight requests are cut. Warm caches and JIT before declaring readiness (e.g. publish AvailabilityChangeEvent with ReadinessState.ACCEPTING_TRAFFIC after a warm-up job) to avoid the latency spike in scenario D.
Trade-offs
| Benefits | Costs | |
|---|---|---|
| Active probes | Detect hung or idle instances that passive signals miss. | Extra traffic; probe interval × threshold delays detection. |
| Deep checks | Catch broken configuration (bad credentials) early. | Correlated failures: a shared dependency ejects the whole fleet. |
| Aggressive liveness | Stuck processes are restarted quickly. | Slow-but-alive instances get killed during load spikes, making things worse. |
Production considerations
- Answer health checks on a separate thread pool or port so a saturated app still responds (or deliberately reports not-ready).
- Keep liveness dumb: "is the process responsive?" Never put dependencies in liveness — restarting does not fix the database.
- Fail open: if every instance is unhealthy, many load balancers route to all of them anyway; know what yours does.
- Combine with passive outlier detection in the load balancer.