All Products
Search
Document Center

Function Compute:Configure a health check policy for function instances

Last Updated:Aug 22, 2024

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

Background

Web functions that run in custom runtimes and Custom Container runtimes may be unstable and exceptions may occur when function instances work. You can use the health check feature of Function Compute to periodically perform health checks on function instances to prevent failed invocations due to abnormal instances.

Limits

Only web function instances support the health check feature.

Behaviors of health checks

Instance startups

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 the function defines the Initializer hook. If the function does not define the Initializer hook, the first health check is directly performed.

  2. Instances are considered healthy if they pass the first health check. After that, the instances enter a periodic health check cycle. If they fail the first health check, the instances are considered to have startup failures and the health check process is terminated. After that, related error messages are reported.

    • If the number of consecutive health check failures reaches the specified value of the Maximum Failures parameter, the function instances are considered unhealthy. Then, Function Compute allocates other instances to process requests.

    • When the number of successful health checks for an unhealthy instance reaches the specified value of the Threshold for Successful Detections parameter, the instance is considered healthy.

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

Single health check

In each health check, Function Compute performs the following operations:

  1. A GET request is sent to a specified HTTP path based on the configurations of the function instance.

  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 a function instance is unhealthy, Function Compute attempts to allocate another instance to execute requests.

  • If Function Compute successfully allocates another instance, the requests are executed by the instance.

  • If Function Compute fails to allocate another instance 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

This section describes how to configure a health check policy in the Function Compute console. A function that is created by selecting Web Function and whose Runtime is Node.js 18 is used as an example.

Step 1: Create a function

  1. Log on to the Function Compute console. In the left-side navigation pane, click Functions.

  2. In the top navigation bar, select a region. On the Functions page, click Create Function.

  3. On the Create Function page, select a method to create the function, configure the following parameters, and then click Create.

    In this example, the Web Function method is selected. 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.

    Runtime

    Specify the runtime of the function. In this example, Node.js 18 is selected.

  4. On the function configuration page, click the Code tab. In the code editor, edit the code of the function. Then, deploy the code and run the function.

    The following example provides sample code for :

    '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, which is used to process HTTP GET requests in /readyz paths. After you complete Step 2: Configure a health check policy for function instances, you can specify to periodically run this handler so that health checks are performed on a regular basis.

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

Step 2: Configure a health check policy for function instances

  1. Log on to the Function Compute console. In the left-side navigation pane, click Functions.

  2. In the top navigation bar, select a region. On the Functions page, click the function that you want to manage.

  3. On the function details page, click the Configurations tab.

  4. In the left-side navigation pane, click the Health Check tab and then click Modify. On the Health Check page, configure parameters based on your business requirements and click OK. The following table describes the parameters.

    Parameter

    Description

    Health Check

    Specify whether to enable the health check feature. If you select Enable, configure the following parameters:

    Request Path

    The HTTP GET path for health checks. Function Compute sends HTTP GET requests to the path for health checks. The value must start with a forward slash (/).

    Delay for First Detection

    The time to wait before the first health check.

    Detection Interval

    The interval at which HTTP GET requests are sent.

    Timeout Period

    The timeout period of HTTP GET requests. When the timeout period elapses, requests are considered to be failed.

    Maximum Failures

    The allowed maximum number of HTTP GET request failures. When the specified value is reached during a periodic health check, the function instance is considered to have failed the health check.

    Threshold for Successful Detections

    The success threshold for HTTP GET requests. When the specified value is reached, the function instance is considered to have passed the health check.

Step 3: Verify the configurations of a health check

  1. On the function details page, click the Code tab and click Test Function to view whether the function can run as expected.

    Run the sample code in Step 1. If OK is returned, the function instance is considered healthy.

  2. Replace the health check part in the code with the following code and then click Deploy Code. After the code is deployed, click Test Function again to view the function running result.

    The following code snippet shows the modified code:

    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');
    });

    After you run the code, the 500 error code is returned in the health check result of the function instance. You can execute the function instance again, then the following code is returned:

    {
        "RequestId": "1-65081d42-e4895cbc7d6252bda643****",
        "Code": "FunctionNotStarted",
        "Message": "The function http server cannot be started. check function health failed with status code: 500 "
    }

More information

In addition to the console, Function Compute also allows you to call API operations to configure instance health checks. For more information, see UpdateFunction.