MicroProfile Health for creating health checks

Last Updated:  October 11, 2020 | Published: August 19, 2019

Once your application is deployed to production you want to ensure it's up– and running. To determine the health and status of your application you can use monitoring based on different metrics, but this requires further knowledge and takes time. Usually, you just want a quick answer to the question: Is my application up? The same is true if your application is running e.g. in a Kubernetes cluster, where the cluster regularly performs health probes to terminate unhealthy pods. With MicroProfile Health you can write both readiness and liveness checks and expose them via an HTTP endpoint with ease.

Learn more about the MicroProfile Health specification and how to use it in this blog post.

Specification profile: MicroProfile Health

  • Current version: 2.2 in MicroProfile
  • GitHub repository
  • Latest specification document
  • Basic use case: Add liveness and readiness checks to determine the application's health

Determine the application's health with MicroProfile Health

With MicroProfile Health you get three new endpoints to determine both the readiness and liveness of your application:

  • /health/ready: Returns the result of all readiness checks and determines whether or not your application can process requests
  • /health/live: Returns the result of all liveness checks and determines whether or not your application is up- and running
  • /health : As in previous versions of MicroProfile Health there was no distinction between readiness and liveness, this is active for downwards compatibility. This endpoint returns the result of both health check types.

To determine your readiness and liveness you can have multiple checks. The overall status is constructed with a logical AND of all your checks of that specific type (liveness or readiness). If e.g. on liveness check fails, the overall liveness status is DOWN and the HTTP status is 503:

In case of an overall UP status, you'll receive the HTTP status 200:

Create a readiness check

To create a readiness check you have to implement the HealthCheck interface and add @Readiness to your class:

As you can add multiple checks, you need to give every check a dedicated name. In general, all your readiness checks should determine whether your application is ready to accept traffic or not. Therefore a quick response is preferable.

If your application is about exposing and accepting data using REST endpoints and does not rely on other services to work, the readiness check above should be good enough as it returns 200 once the JAX-RS runtime is up- and running:

Furthermore, once /health/ready returns 200, the readiness is identified and from now on the /health/live is used and no more readiness checks are required.

Create liveness checks

Creating liveness checks is as simple as creating readiness checks. The only difference is the @Livness annotation at class level:

In this example, I'm checking for free disk space as a service might rely on storage to persist e.g. files.  With the .withData() method of the HealthCheckResponseBuilder you can add further metadata to your response.

In addition, you can also combine the @Readiness and @Liveness annotation and reuse a health check class for both checks:

This check now appears for /health/ready and /health/live:

Other possible liveness checks might be: checking for active JDBC connections, connections to queues, CPU usage, or custom metrics (with the help of MicroProfile Metrics).

YouTube video for using MicroProfile Health

Watch the following YouTube video of my Getting started with MicroProfile series to see MicroProfile Health in action:

You can find the source code for this blog post on GitHub.

Have fun using MicroProfile Health,

Phil

>