Explain the importance of health checks in deployment and how you would implement them.

Health checks are endpoints that report whether a running service is functioning correctly, letting load balancers and orchestrators route traffic only to instances that are actually able to serve requests.

Key Points: • A liveness check answers "is this instance still running?" and, if it fails repeatedly, the orchestrator typically restarts the container. • A readiness check answers "is this instance ready to serve traffic right now?" and, if it fails, traffic is withheld without necessarily restarting anything. • The endpoint usually returns a simple status like 200 OK for healthy or 503 for unhealthy, sometimes with details about downstream dependencies. • Kubernetes uses livenessProbe and readinessProbe configuration to periodically call these endpoints and act automatically based on the results. • Spring Boot Actuator provides a ready-made /actuator/health endpoint that can be extended with custom health indicators for databases or external services.

Example: A new pod starts up but its database connection pool hasn't initialized yet; the readiness probe fails during that window, so Kubernetes withholds traffic until /actuator/health reports healthy, preventing users from hitting a half-started instance.

Code Example:

readinessProbe:
  httpGet:
    path: /actuator/health
    port: 8080
  initialDelaySeconds: 10
  periodSeconds: 5

Interview Tip: A concise interview answer is:

"I expose a health endpoint, typically Spring Boot Actuator's /actuator/health, and wire it into Kubernetes as both a liveness and readiness probe. That way traffic is only routed to instances that are genuinely ready, and unhealthy ones get restarted automatically."