Close Menu
Soshace Digital Blog

    Subscribe to Updates

    Get The Latest News, Updates, And Amazing Offers

    What's Hot
    JavaScript

    TOP 11 JavaScript Machine Learning & Data Science Libraries

    JavaScript

    Handling Mutations and Data Fetching Using React Query

    Recruitment

    Ensuring Quality Hires: The Vital Role of Background Checks

    Important Pages:
    • Home
    • About
    • Services
    • Contact Us
    • Privacy Policy
    • Terms & Conditions
    Facebook X (Twitter) Instagram LinkedIn YouTube
    Today's Picks:
    • Scaling Success: Monitoring Indexation of Programmatic SEO Content
    • Leveraging Influencers: Key Drivers in New Product Launches
    • How Privacy-First Marketing Will Transform the Industry Landscape
    • The Impact of Social Proof on Thought Leadership Marketing
    • Balancing Value-Driven Content and Promotional Messaging Strategies
    • Top Influencer Marketing Platforms to Explore in 2025
    • Emerging Trends in Marketing Automation and AI Tools for 2023
    • Strategies to Mitigate Duplicate Content in Programmatic SEO
    Wednesday, September 10
    Facebook X (Twitter) Instagram LinkedIn YouTube
    Soshace Digital Blog
    • Home
    • About
    • Services
    • Contact Us
    • Privacy Policy
    • Terms & Conditions
    Services
    • SaaS & Tech

      Maximizing Efficiency: How SaaS Lowers IT Infrastructure Costs

      August 27, 2025

      Navigating Tomorrow: Innovations Shaping the Future of SaaS

      August 27, 2025

      Maximizing Impact: Strategies for SaaS & Technology Marketing

      August 27, 2025
    • AI & Automation

      Enhancing Customer Feedback Analysis Through AI Innovations

      August 27, 2025

      Navigating the Impact of AI on SEO and Search Rankings

      August 27, 2025

      5 Automation Hacks Every Home Service Business Needs to Know

      May 3, 2025
    • Finance & Fintech

      Critical Missteps in Finance Marketing: What to Avoid

      August 27, 2025

      Analyzing Future Fintech Marketing Trends: Insights Ahead

      August 27, 2025

      Navigating the Complex Landscape of Finance and Fintech Marketing

      August 27, 2025
    • Legal & Compliance

      Exploring Thought Leadership’s Impact on Legal Marketing

      August 27, 2025

      Maximizing LinkedIn: Strategies for Legal and Compliance Marketing

      August 27, 2025

      Why Transparency Matters in Legal Advertising Practices

      August 27, 2025
    • Medical Marketing

      Enhancing Online Reputation Management in Hospitals: A Guide

      August 27, 2025

      Analyzing Emerging Trends in Health and Medical Marketing

      August 27, 2025

      Exploring Innovative Content Ideas for Wellness Blogs and Clinics

      August 27, 2025
    • E-commerce & Retail

      Strategic Seasonal Campaign Concepts for Online and Retail Markets

      August 27, 2025

      Emerging Trends in E-commerce and Retail Marketing Strategies

      August 27, 2025

      Maximizing Revenue: The Advantages of Affiliate Marketing for E-Commerce

      August 27, 2025
    • Influencer & Community

      Leveraging Influencers: Key Drivers in New Product Launches

      August 27, 2025

      Top Influencer Marketing Platforms to Explore in 2025

      August 27, 2025

      Key Strategies for Successful Influencer Partnership Negotiations

      August 27, 2025
    • Content & Leadership

      The Impact of Social Proof on Thought Leadership Marketing

      August 27, 2025

      Balancing Value-Driven Content and Promotional Messaging Strategies

      August 27, 2025

      Analyzing Storytelling’s Impact on Content Marketing Effectiveness

      August 27, 2025
    • SEO & Analytics

      Scaling Success: Monitoring Indexation of Programmatic SEO Content

      August 27, 2025

      Strategies to Mitigate Duplicate Content in Programmatic SEO

      August 27, 2025

      Effective Data Visualization Techniques for SEO Reporting

      August 27, 2025
    • Marketing Trends

      How Privacy-First Marketing Will Transform the Industry Landscape

      August 27, 2025

      Emerging Trends in Marketing Automation and AI Tools for 2023

      August 27, 2025

      Maximizing ROI: Key Trends in Paid Social Advertising

      August 27, 2025
    Soshace Digital Blog
    Blog / JavaScript / Node.js / Minimize Downtime by Creating a Health-check for Your NodeJS Application
    JavaScript

    Minimize Downtime by Creating a Health-check for Your NodeJS Application

    bradstarartBy bradstarartSeptember 23, 2020No Comments9 Mins Read
    Facebook Twitter Pinterest Telegram LinkedIn Tumblr Email Reddit
    Minimize Downtime by Creating a Health-check for Your NodeJS Application
    Minimize Downtime by Creating a Health-check for Your NodeJS Application
    Share
    Facebook Twitter LinkedIn Pinterest Email Copy Link
    Minimize Downtime by Creating a Health-check for Your NodeJS Application
    Minimize Downtime by Creating a Health-check for Your NodeJS Application

    When an API receives more traffic than it can handle, it will crash and be unavailable until someone manually brings it back online. This is referred to as downtime. One of the most important aspects of maintaining a production application is minimizing downtime.

    Downtime can arise due to certain crucial parts of your app breaking, thereby bringing down the whole system. For instance, if the database crashes, your application will no longer be able to properly service most requests. The best way to minimize this is to have data about running servers, giving us room to react before anything goes wrong.

    The most efficient way to achieve metrics tracking is via third party tools such as the TICK stack (Telegraf, InfluxDB, Capacitor and Kronograf), Sentry and Prometheus. However, each of these tools requires a lot of time investment in order to consistently track and maintain.

    A simpler way to track an API is to create a single route that responds with a simple “UP” or “DOWN” message to indicate whether key parts of our application are working or not. This could be useful when debugging often cryptic client-side errors.

    For example, rather than a simple 500 error message, an authorized user has additional details about errors being experienced by the server. It also gives us room to react to a server that is ‘unhealthy’ (i.e. in need of attention) but not necessarily ‘down’.

    Why create a health-check route?

    The most compelling reason to have a health-check route is in order to provide an API-accessible way to determine how ‘healthy’ the API is. This information could then be used in different ways:

    • Feed it to platforms like Statuspage.io to create automated incident reports.
    • Used by a load balancer to determine whether to route traffic to an instance or not,
    • By client apps to determine what kind of error messages to display to users.
    • By developers to debug server-side errors from the route level

    Tools like Prometheus have their place in a production environment, and should probably be used to some degree. However, they are usually a huge undertaking – from configuration to maintenance – so many developers tend to avoid them or aren’t very consistent in their use. Nonetheless, shipping an application without an uptime/downtime-monitoring service shouldn’t isn’t a great option to pick.

    A health-check route is a simple compromise that can either be built on top of or scrapped for a more mature and robust solution at later stages. The goal here isn’t to create a fully-fledged status monitoring system for your API. Rather, it’s to enforce some level of monitoring early into an application. Some monitoring is better than none at all.

    How it works

    Your server’s health could depend on any number of metrics. Slow response and long query times are good indicators of an ‘unhealthy’ server, for example.

    In this guide, we will first focus on simple core metrics such as whether your database accepts connections or not, and get to more complicated issues like query times later on. Services such as redis instances and other microservices can be handled in a similar fashion, but we won’t cover those here.

    We’re going to create a single route that is responsible for collecting any data from the underlying server and returning an appropriate response.

    Read More:  Conditional Rendering Methods in React

    If you’re confident with your Nginx prowess, you could also implement the same using extensions. This has the benefit of providing data directly to the load balancer, rather than on isolated instances.

    Monitoring Database Metrics

    The database is the holy grail of your application, making it the most important part of your stack to monitor and analyze. Different databases will have different monitoring requirements, and for this guide, we’re going to concentrate on Postgres.

    A lot of functionality can be optionally enabled using Postgres extensions. When enabled, pg_stat_statements silently records queries run against your database, strips out some variables from them and saves useful information about each query, e.g. query time, affected rows and the query itself.

    To enable pg_stat_statements, run the following command in your database:

    CREATE EXTENSION pg_stat_statements;

    Then head over to your postgresql configuration file (found in /etc/postgresql//main/postgresql.conf for Ubuntu 18.04+ users)

    And add the following lines:

    shared_preload_libraries = 'pg_stat_statements'
    pg_stat_statements.track = all
    pg_stat_statements.max = 10000
    

    Now, every query we make will be recorded in pg_stat_statements, and will look like:

    {
      "userid": 16384,
      "dbid": 24576,
      "queryid": "-1144474632245246520",
      "query": "SELECT NOW()",
      "calls": "11",
      "total_time": 0.15964,
      "min_time": 0.010483,
      "max_time": 0.037042000000000005,
      "mean_time": 0.014512727272727271,
      "stddev_time": 0.007268825078135321,
      "rows": "11",
      "shared_blks_hit": "0",
      "shared_blks_read": "0",
      "shared_blks_dirt
    }
    

    To learn more about what each of these fields means, visit the Postgres documentation
    Creating a health-check route with express
    Our application is going to be created using express-starter and will have the following dependencies:

    • pg – used to connect and interact with a Postgres database.
    • dotenv – for loading environment variables

    To install them, run

    npm install pg dotenv
    // or
    yarn add pg dotenv

    Create a file named ‘.env’ in the root directory of your project and add the following variables

    PGUSER= #postgres user
    PGPASSWORD= #postgres password
    PGDATABASE= #postgres database

    These will be used by pg to connect to our database.

    A simple check we could perform is to ensure that the database is simply up and accepts queries. Here is the method we’ll use to perform this check:

    const analyzeDatabaseHealth = async function () {
    
        let status = "up"
        let name = "database";
    
        try {
            // Try and make a query
            const result = await pool.query('SELECT * FROM pg_stat_statements');
    
            // if there are no results, there are no queries recorded
            if (!result) return {
                status,
                name,
                condition: {
                    health: "unhealthy",
                    cause: "no stats"
                }
            };
    
            // otherwise, our database is okay and accepts queries
            return {
                status,
                name,
                condition: {
                    health: "healthy"
                }
            }
        } catch (e) {
            // If the query fails, pg_stat_statements is not enabled
            // or some other database error occurred.
            // mark it as 'down'
            status = 'down';
            return {
                status,
                name,
                condition: {
                    health: "down",
                    cause: "unable to execute queries"
                }
            }
        }
    };
    

    This code tries to query for stats from pg_stat_statements and if an error occurs, it marks the database as being down and returns an error message.

    We can make this a bit more powerful by leveraging the wealth of information available at our disposal from pg_stat_statements. For instance, if we wanted to track the total amount of time (in hours) and the average time (in milliseconds) queries take, we could use the following query:

    SELECT
    (total_time / 1000 / 60) as total_time,
    (total_time/calls) as avg_time,
    query
    FROM pg_stat_statements
    ORDER BY 1 DESC
    LIMIT 100;
    

    And set up an alert (an email, for instance) for any queries that run consistently slow. Note: any query that takes more than 1ms can be conventionally considered ‘slow.’ This can be adjusted to fit your specific requirements.

    Read More:  Working with Jotai as your next state management in React

    Let’s create a function that will filter out long-running queries and mark the database as unhealthy.

    const analyzeDatabaseHealth = async function () {
    
     let status = "up"
    
     try {
       // run the query
       const {rows: stats} = await pool.query(`
         SELECT (total_time / 1000 / 60) as total_time,
                (total_time / calls)     as avg_time,
                query
         FROM pg_stat_statements
         ORDER BY 2 DESC
         LIMIT 1;
       `)
       // if there are no stats, something is probably wrong
       if (!stats) return {
         status,
         condition: {
           health: "unhealthy",
           cause: "no stats"
         }
       };
    
       // filter out any query that takes more than 1ms on average
       const longRunningQueries = stats.filter(queryObj => {
         return queryObj.avg_time > 1
       });
       // mark the database as unhealthy
       if (longRunningQueries.length) return {
         status,
         condition: {
           health: "unhealthy",
           cause: "long-running queries"
         }
       }
    
       // if there are no errors, the database is fine
       return {
         status,
         condition: {
           health: "healthy"
         }
       }
     }catch (e) {
       // mark the api as ‘down’   
       status = 'down';
       return {
         status,
         condition: {
           // mark the database as ‘down’
           health: "down",
           cause: "unable to execute queries"
         }
       }
     }
    };
    

    And here is how you’d use it:

    router.get("/health", async function (req, res) {
    
     let status = "up";
     let cause = ""
    
     const health = {
       // aggregate status
       status: "up",
     };
    
     const coreMetrics = [
       analyzeDatabaseHealth, // method analyzing database health
       analyzeRedis, // method analyzing redis
       analyzeThirdPartyConnection, // method analyzing third-party
     ];
    
     for (let i = 0; i < coreMetrics.length; i++){
       // run every function in turn. Note that each is async.
       let func = coreMetrics[i];
       const metricResult = await func(); 
       // if any core metric is down
       // set the api as being down, since some requests won’t be processed.
       if (metricResult.status === "down") {
         status = "down";
         cause = `dependent service '${metricResult.name}' is down`;
       }
    
       // add this to the 'services' array
       health.services.push(metricResult);
    
     }
    
     // if status is not ‘up’, we set it to ‘down’ and state a cause.
     if (status !== "up") {
       health.status = status;
       health.cause = cause;
     }
    
     // return the api health.
     return res.send(health);
    });

    This returns responses such as:

    {
      "status": "up",
      "services": [
        {
          "status": "up",
          "name": "database",
          "condition": {
            "health": "healthy"
          }
        },
        {
          "name": "redis",
          "status": "up",
          "condition": {
            "health": "healthy"
          }
        },
        {
          "name": "third-party",
          "status": "up",
          "condition": {
            "health": "healthy"
          }
        }
      ]
    }

    All the code is commented, but let’s go over it again.

    • When the /health route is called, three core metrics are called to ensure the API responds correctly. If it returns an ‘up’ status, it is considered healthy. A ‘down’ status on any of the core metrics indicates the API cannot respond to requests at all.
    • The analyzeDatabaseHealth method filters for any queries that take more than 1ms on average to complete and alerts us the database is unhealthy. Note that the database isn’t marked as being down since it still executes queries. It is marked as healthy otherwise.
    • Any number of checks can be integrated into the system – analyzeRedis and analyzeThirdPartyConnection are stub methods that represent this.

    Conclusion

    This guide went over a brief look at how to implement a health-check route for your NodeJS application. Remember – this isn’t meant to be a fully-fledged solution for your API-monitoring, but can make a good entrypoint to learning about how to deliver metrics that fulfil your expectations.

    Other important metrics you might want to track include:

    • Uptime
    • Cache-hit ratio
    • Remaining disk space
    • Used vs available RAM

    Source code of the lesson https://github.com/Bradleykingz/nodejs-health-check

    Share. Facebook Twitter Pinterest LinkedIn Tumblr Email
    bradstarart

      Related Posts

      Streamlining Resource Allocation for Enhanced Project Success

      December 18, 2024

      Conducting Effective Post-Project Evaluations: A Guide

      December 16, 2024

      Strategies for Keeping Projects on Track and Meeting Deadlines

      December 10, 2024
      Leave A Reply Cancel Reply

      You must be logged in to post a comment.

      Stay In Touch
      • Facebook
      • Twitter
      • Pinterest
      • Instagram
      • YouTube
      • Vimeo
      Don't Miss
      Programming September 22, 2016

      Структура организации

      Структура организации Account Manager – тот кто ведет заказчиков. В идеале мы должны организовать круглосуточный…

      Leveraging LinkedIn Prospecting for Local Business Expansion

      November 26, 2024

      Quam Nulla Porttitor Massa Dneque Aliquam Vestibulum

      January 28, 2020

      An in-depth guide on the CSS position property

      February 15, 2021

      Categories

      • AI & Automation
      • Angular
      • ASP.NET
      • AWS
      • B2B Leads
      • Beginners
      • Blogs
      • Business Growth
      • Case Studies
      • Comics
      • Consultation
      • Content & Leadership
      • CSS
      • Development
      • Django
      • E-commerce & Retail
      • Entrepreneurs
      • Entrepreneurship
      • Events
      • Express.js
      • Facebook Ads
      • Finance & Fintech
      • Flask
      • Flutter
      • Franchising
      • Funnel Strategy
      • Git
      • GraphQL
      • Home Services Marketing
      • Influencer & Community
      • Interview
      • Java
      • Java Spring
      • JavaScript
      • Job
      • Laravel
      • Lead Generation
      • Legal & Compliance
      • LinkedIn
      • Machine Learning
      • Marketing Trends
      • Medical Marketing
      • MSP Lead Generation
      • MSP Marketing
      • NestJS
      • Next.js
      • Node.js
      • Node.js Lessons
      • Paid Advertising
      • PHP
      • Podcasts
      • POS Tutorial
      • Programming
      • Programming
      • Python
      • React
      • React Lessons
      • React Native
      • React Native Lessons
      • Recruitment
      • Remote Job
      • SaaS & Tech
      • SEO & Analytics
      • Soshace
      • Startups
      • Swarm Intelligence
      • Tips
      • Trends
      • Vue
      • Wiki
      • WordPress
      Top Posts

      Flask Development Made Easy: A Comprehensive Guide to Test-Driven Development

      Flask January 4, 2024

      Creating Real-Time API with Beautiful Soup and Django REST Framework

      Python February 26, 2020

      Follow These Guidelines to Write Testable Code That Can Scale | with Examples

      Programming November 25, 2019

      The Impact of Social Proof on Thought Leadership Marketing

      Content & Leadership August 27, 2025

      Subscribe to Updates

      Get The Latest News, Updates, And Amazing Offers

      About Us
      About Us

      Soshace Digital delivers comprehensive web design and development solutions tailored to your business objectives. Your website will be meticulously designed and developed by our team of seasoned professionals, who combine creative expertise with technical excellence to transform your vision into a high-impact, user-centric digital experience that elevates your brand and drives measurable results.

      7901 4th St N, Suite 28690
      Saint Petersburg, FL 33702-4305
      Phone: 1(877)SOSHACE

      Facebook X (Twitter) Instagram Pinterest YouTube LinkedIn
      Our Picks
      JavaScript

      If Trello became too small for you.

      Interview

      Interview with Alexander

      React

      Build Real-World React Native App #8 : implement Dark mode

      Most Popular

      Hunting Down and Fixing Memory Leaks in Java

      Java

      Guidelines for Building Accessible Web Applications

      Programming

      23. Node.js Lessons. Domains, asynchronous try.. catch. Part 3.

      Programming
      © 2025 Soshace Digital.
      • Home
      • About
      • Services
      • Contact Us
      • Privacy Policy
      • Terms & Conditions

      Type above and press Enter to search. Press Esc to cancel.