Skip to content

Instantly share code, notes, and snippets.

@pavelfomin
Last active October 28, 2024 18:29
Show Gist options
  • Save pavelfomin/c7e003cdc632c689f58cc2091509495c to your computer and use it in GitHub Desktop.
Save pavelfomin/c7e003cdc632c689f58cc2091509495c to your computer and use it in GitHub Desktop.
Spring Boot liveness / readiness probes

There is some confusion online about whether health indicators contribute to the liveness / readiness probe status. Research below confirms that liveness / readiness probe status does not depend on the health indicators as long as these conditions (causing the health indicators failure) do not prevent application from starting.

https://spring.io/blog/2020/03/25/liveness-and-readiness-probes-with-spring-boot

You should carefully consider tying external state to Liveness or Readiness and this is why Spring Boot is not adding any by default. Each application and deployment is different, but we’re committed to providing guidance and adapt defaults with the help of the community - check out the "Checking external state with Kubernetes Probes" section in our reference documentation.

Looking in the code, the usages of LivenessState.CORRECT are listed below:

  • EventPublishingRunListener.started(ConfigurableApplicationContext, Duration) (org.springframework.boot.context.event)
    • SpringApplicationRunListeners.started(ConfigurableApplicationContext, Duration) (org.springframework.boot)
      • SpringApplication.run(String...) (org.springframework.boot)
        • SpringApplication.run(Class[], String[]) (org.springframework.boot)
        • SpringApplicationBuilder.run(String...) (org.springframework.boot.builder)
        • SpringBootServletInitializer.run(SpringApplication) (org.springframework.boot.web.servlet.support)

https://github.com/spring-projects/spring-boot/blob/v3.3.2/spring-boot-project/spring-boot/src/main/java/org/springframework/boot/SpringApplication.java#L341

Which confirms that application LivenessState.CORRECT is published regardless of the custom health indicators as long as these conditions do not prevent application from starting (i.e. Kafka, etc).

Similarly, the usage of ReadinessState.ACCEPTING_TRAFFIC event publish are listed below:

  • EventPublishingRunListener.ready(ConfigurableApplicationContext, Duration) (org.springframework.boot.context.event)
    • SpringApplicationRunListeners.ready(ConfigurableApplicationContext, Duration) (org.springframework.boot)
      • SpringApplication.run(String...) (org.springframework.boot)
        • SpringApplication.run(Class[], String[]) (org.springframework.boot)
        • SpringApplicationBuilder.run(String...) (org.springframework.boot.builder)
        • SpringBootServletInitializer.run(SpringApplication) (org.springframework.boot.web.servlet.support)

If debug level logging is enabled for ApplicationAvailabilityBean:

logging:
  level:
    org.springframework.boot.availability.ApplicationAvailabilityBean: DEBUG

Then you will see something similar to this locally:

15:24:35.509 [main] DEBUG o.s.b.a.ApplicationAvailabilityBean -- Application availability state LivenessState changed to CORRECT
15:24:35.512 [main] DEBUG o.s.b.a.ApplicationAvailabilityBean -- Application availability state ReadinessState changed to ACCEPTING_TRAFFIC

right after the application is started.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment