This topic describes the background, limits, and behaviors of function instance health checks. This topic also describes how to configure a health check policy for function instances in the Function Compute console.

Background information

If you select Use Custom Runtime or Use Container Image when you create a function and use your own container image as the function runtime environment, your custom runtime environment may be unstable, which may cause exceptions for function instances. Function Compute supports periodic health checks for function instances to avoid request failures caused by instance exceptions.

Limits

You can configure health check policies only for the function instances that run in custom runtime environments and instances that are started by using custom images.

Health check behaviors

Instance startup

This section describes the startup behaviors of function instances after you configure a health check policy for a function:
  1. The Initializer hook is executed first if it is defined. If the Initializer hook is not defined, the first health check is directly performed.
  2. Instances are considered healthy if they pass the first health check. After that, they enter a periodic health check cycle. If they fail the first health check, the instance is considered to have failed to start and the health check process is terminated. After that, related error messages are reported.
    • If the number of consecutive health check failures reaches the value of Maximum Failures, the function instance is considered unhealthy. In this case, Function Compute attempts to allocate another instance to process the requests.
    • When the number of successful health checks for an unhealthy instance reaches the value of Threshold for Successful Detections, the instance is considered healthy.

For more information about how to configure Threshold for Successful Detections and Maximum Failures, see Configure a health check policy in the Function Compute console.

Single health check

This section describes the behaviors of a single health check in Function Compute.
  1. A GET request is sent to a specified HTTP path based on the function configurations.
  2. If the GET request does not time out and the HTTP response status code is less than 400 and greater than or equal to 200, the instance passes the health check. Otherwise, the instance is considered to have failed the health check.

Allocation after an instance fails the health check

If the function instance is unhealthy, Function Compute attempts to allocate another instance to execute the requests.
  • If Function Compute succeeds to allocate a new instance, the requests are executed by the new instance.
  • If Function Compute fails to allocate another instance to execute the requests in a timely manner, a health check error is returned. A new instance is allocated during the next health check.

Configure a health check policy in the Function Compute console

This topic describes how to configure a health check policy for a function that is created from Use Custom Runtime and whose Runtime Environments is Node.js 14 and Request Type is Event Requests.

Step 1: Create a function

  1. Log on to the Function Compute console. In the left-side navigation pane, click Services & Functions.
  2. In the top navigation bar, select a region. On the Services page, click the desired service.
  3. On the Functions page, click Create Function.
  4. On the Create Function page, select Use Custom Runtime, configure the parameters based on your business requirements, and then click Create.
    The following table describes the parameters that you must configure. Retain the default values for other parameters.
    Parameter Description
    Function Name Enter a name for the function.
    Request Type Select Event Request.
    Runtime Environments Select Node.js 14.
  5. On the function details page, click the Code tab, edit the function code in the code editor, and then deploy the code and run the function.
    The following example provides the sample code for index.js:
    'use strict';
    
    // Constants
    const PORT = 9000;
    const HOST = '0.0.0.0';
    const REQUEST_ID_HEADER = 'x-fc-request-id'
    
    const express = require('express');
    const bodyParser = require('body-parser');
    const app = express();
    
    app.use(bodyParser.urlencoded({ extended: true }));
    app.use(bodyParser.json());
    app.use(bodyParser.raw());
    
    app.get('/readyz', (req, res) => {
      console.log(`receive health check`);
      res.status(200);
      console.log(`i am ready`);
      res.send('i am ready\n');
    });
    
    // invocation
    app.post('/invoke', (req, res) => {
      var rid = req.headers[REQUEST_ID_HEADER]
      console.log(`FC Invoke Start RequestId: ${rid}`)
      res.send('OK');
      console.log(`FC Invoke End RequestId: ${rid}`)
    });
    
    var server = app.listen(PORT, HOST);
    console.log(`Running on http://${HOST}:${PORT}`);
    
    server.timeout = 0; // never timeout
    server.keepAliveTimeout = 0; // keepalive, never timeout

    In the preceding code, readyz is the handler of the defined health check policy to process HTTP GET requests in the /readyz path. After you complete Step 2, you can configure this handler to run periodically to check whether the instance is normal.

    Function Compute attempts to allocate another instance when it determines that the function instance is abnormal. If Function Compute fails to allocate another instance, a health check error is returned.

    For example, if you replace the code snippets for health check in the preceding sample code with the following code, the 500 error code is returned.
    app.get('/readyz', (req, res) => {
      console.log(`receive health check`);
      res.status(500);
      console.log(`i am not ready`);
      res.send('i am not ready\n');
    });
    The following information is returned if the function is run again.
    {
        "ErrorCode": "FunctionNotStarted",
        "ErrorMessage": "check function health failed with status code: 500 "
    }

Step 2: Configure a health check policy for instances

  1. Log on to the Function Compute console. In the left-side navigation pane, click Services & Functions.
  2. In the top navigation bar, select a region. On the Services page, click the desired service.
  3. On the Functions page, find the desired function and click Configure in the Actions column.
  4. In the Health Check Settings section, configure the parameters and click Save. The following table describes the parameters.
    Parameter Description
    Health Check Specify whether to enable the health check feature. If you select Enable, you must configure the following parameters:
    Request Path The HTTP GET path for the health checks. Function Compute sends HTTP GET requests to this path for health checks. The value must start with a forward slash (/).
    Delay for First Detection Specify the time to wait before the first health check.
    Detection Interval Specify the interval at which HTTP GET requests are sent.
    Timeout Period Specify the timeout period of HTTP GET requests. When the timeout period elapses, the requests are considered to have failed.
    Maximum Failures Specify the threshold for HTTP GET request failures. When the number of consecutive failed HTTP GET requests reaches the specified value during a periodic health check, the instance is considered to have failed the health check.
    Threshold for Successful Detections The success threshold for HTTP GET requests. When the number of consecutive successful HTTP GET requests reaches this value, the instance is considered to have passed the health check.

Additional information

Function Compute also allows you to call an API operation to configure the health check feature for instances. For more information, see the following topics: