Health checks are extremely useful when we are deploying applications on Docker containers or using orchestrators such as Kubernetes. In this post, we will learn how to implement a simple Fastify Health Check using the fastify-healthcheck package.

The fastify-healthcheck plugin makes it easy to setup a health check endpoint. Any orchestrator can call the endpoint to get reports about the web application to determine whether it is alive and running.

In case you are new to Fastify plugins, I will recommend you to go through this post.

1 – Installation

Apart from installing fastify, we also need to install the fastify-healthcheck package.

$ npm install fastify-healthcheck

Under the hood, this package uses the @fastify/under-pressure plugin as a dependency. Due to this package, we can also specify some configuration options as well.

2 – Using Basic Fastify Health Check

The easiest way to implement a health check is to simply register the fastify-healthcheck plugin in our application context.

See below:

const fastify = require('fastify')({
    logger: true
})

fastify.register(require('fastify-healthcheck'))

fastify.listen(3000, function(err) {
    if (err) {
        fastify.log.error(err)
        process.exit(1)
    }
})

Just by doing this simple thing, we will have an endpoint /health. The endpoint will return HTTP status code 200 in case the application is up and running.

3 – Configuring Fastify Health Check Setup

While the above setup might be fine for basic use-case, the fastify-healthcheck plugin also provides some configuration options. These options allow us to customize some aspects of the health check.

Below are the available options:

  • healthcheckUrl – This can be used to set a different health check url for our application instead of /health.
  • healthcheckUrlDisable – Using this property, we can disable the health check URL.
  • healthcheckUrlAlwaysFail – With this property set to true, the health check will always return a failure response. This is particularly useful for testing failure responses.
  • exposeUptime – This property returns the uptime of the underlying NodeJS process.

How can we set these properties?

See the below example where we customize the healthcheckUrl and also set the exposeUptime to true.

fastify.register(require('fastify-healthcheck'),
{ healthcheckUrl: '/custom-health', 
exposeUptime: true })

The above configuration will return a response as below when we hit /custom-health.

{
	"statusCode": 200,
	"status": "ok",
	"uptime": 4.757126667
}

We get the statusCode and status. Also, we get the uptime of the NodeJS process.

Similarly, we can also set the other configuration properties for the health check.

Conclusion

Setting up a Fastify Health Check is extremely easy using the fastify-healthcheck package. And from a microservices architecture point of view, health checks are an extremely important concept.

In my view, we should definitely use this feature in our application.

In case you want to learn to create a complete app using Fastify, check out this post on building a Fastify REST API using Postgres.

If you have any comments or queries about this post, please feel free to mention them in the comments section below.

Categories: BlogFastify

Saurabh Dashora

Saurabh is a Software Architect with over 12 years of experience. He has worked on large-scale distributed systems across various domains and organizations. He is also a passionate Technical Writer and loves sharing knowledge in the community.

0 Comments

Leave a Reply

Your email address will not be published. Required fields are marked *