- How To Add a Health Check To Your Node.js App
- Ali Kamalizade
- Warehouse address: Github
Quickly detect how your application is performing without having to dig deep into the code.
With the increasing number of application users, the usability of software will face a huge test. Let’s take a look at why your application needs high availability.
Why do we need high availability of applications?
- In SLAS, you often specify a specific availability (say 99.9%) that you can achieve.
- for
B2C
For applications (such as online stores), downtime can mean significant financial losses. - Beyond the financial impact, an unexpected outage can damage your reputation.
Now we understand why we need high availability. We’ll look at common problems that can cause outages.
How can usability be negatively affected?
- Critical issues that prevent your application from working properly (for example, a database connection failure).
- Third party infrastructure issues (e.g., your cloud service provider is down unexpectedly).
Obviously, we should be very interested in making sure our application is highly available.
But the question is, “When is an app really healthy?” . It’s hard to say and hard to achieve. It depends on what you mean by healthy. So you have to define it yourself.
How do you define the health of your application?
- The service can respond to requests
- The service can respond to requests and connect to the database.
- Services can respond to requests, connect to databases, and connect to other third-party and integrated systems.
API tests can be used as an entry point to start health checks. However, there are dedicated services, such as Pingdom, New Relic, and Freshping, that continuously monitor the availability of websites and services.
These tools provide common data and functionality:
- availability
- Down the actual
- Number of events
- Average response time (typically at the millisecond level)
- The accident
- Query related data by time
- Integration with other popular software tools:
Slack
,Jira
,PagerDuty
And so on.
Why use health checks in software engineering?
- Health checks are easy to implement. As mentioned above, you must define what health means to you.
- For example, when publishing applications, the publishing platform automatically invokes health check routes.
- Detect outages in a timely manner. There are many free and paid tools that provide continuous runtime monitoring, such as
Pingdom
,New Relic
andFreshping
. - Health checks provide a complete check when problems arise. If you have an upcoming task and something goes wrong (an instance stops running, for example), the first and easiest thing to do is to check the health of the service without having to dig into the current code. So even the most basic health test has some value.
- In this tutorial, we will use
Express.js
This hot oneNode.js
Framework. However, if you use other frameworks or programming predictions such as:C#
,Java
orPython
And the steps are the same.
How do Node.js applications perform health checks?
- Create a new route (e.g.
healthcheck.routes.js
). - in
app.js
Register your route in. I recommend usinghealthcheck
Name the route. - In your health check route, you need to send a success response if everything is okay. If your application is unhealthy, you need to send an error response.
- If you have many microservices, you should add a health check to each one.
- Optional: Add authentication to restrict access to your health check route.
- Optional: Add a call to the new route
API
The test.
// app.js: register the route. In our case, we don't want authorization for this route
app.use('/healthcheck'.require('./routes/healthcheck.routes'));
// healthcheck.routes.js: return a 2xx response when your server is healthy, else send a 5xx response
import express from 'express';
const router = express.Router({});
router.get('/'.async (_req, res, _next) => {
// optional: add further things to check (e.g. connecting to dababase)
const healthcheck = {
uptime: process.uptime(),
message: 'OK'.timestamp: Date.now()
};
try {
res.send();
} catch (e) {
healthcheck.message = e;
res.status(503).send(); }});// export router with all routes included
module.exports = router;
// healthcheck.spec.js (services like Pingdom or Freshping do a similar approach to check whether your server is healthy)
describe('Healthcheck', () => {
it('returns 200 if server is healthy'.async() = > {const res = await get(`/healthcheck`.null)
.expect(200);
expect(res.body.uptime).toBeGreaterThan(0);
});
});
Copy the code
How do we use health monitoring tools and how do these tools support our applications?
In this tutorial, I’ll use Freshping to monitor the health of the managed API. The following report shows data from October 2019.
Let’s take a closer look at the report:
-
In the selected timeline, availability reached 96.58%. In general, you should aim for 99.90% availability. Many software libraries guarantee at least 99.90% availability, especially for enterprise users.
-
96.58% availability sounds good at first glance, but you have to be careful about downtime: nearly 19 hours of continuous downtime can be very bad for your business. Therefore, it is important to notify you early when an incident occurs so that you can quickly resolve the problem.
-
Therefore, it is important to notify early when an incident is sent so that you can fix the problem quickly.
-
An accident occurred in the selected time frame. Of course, you should try to keep this number as small as possible. When an accident does occur, the problem should be solved as soon as possible to minimize the impact of the accident.
-
The average response time is 539ms, which is quite stable. This consistency is a good sign because it means our health checks work reliably.
-
While the average response time isn’t too slow, it’s not too fast either. Therefore, you should minimize response times. This may indicate that there are many things involved in the health of an application. On the other hand, it could just be an infrastructure problem (for example, the server is too far away from the client or the hardware is not powerful enough).
conclusion
Feel like you’re reading this article. As you can see, adding health checks to Node.js is very simple. Of course, you can (and probably should) add health checks if you’re using another server-side programming language. Tools like Pingdom or Freshping can often check the health of our applications and help us catch problems early.
How do you implement the health check? Also, what kind of tools do you use?