This topic covers the background, limitations, and behavior of function instance health checks. It also shows how to configure them in the Function Compute console.
Background
Functions that use a custom runtime or custom container image can sometimes become unstable, causing a function instance to behave abnormally. To handle this, you can configure periodic health checks in Function Compute. This prevents requests from being routed to unhealthy instances, thereby avoiding request failures.
Limitations
Health checks are supported only for Web Function and GPU functions.
How health checks work
Instance startup behavior
If you configure a health check for a function, the instance starts up as follows.
If the function has an initializer hook, it runs first. Otherwise, the platform performs the initial health check immediately.
If the initial health check succeeds, the instance is marked as healthy and begins its periodic health check cycle. If the check fails, the instance has failed to start, the health check process stops, and an error is returned.
If the number of consecutive failed health checks reaches the configured Maximum Failures, the instance is marked as unhealthy. Function Compute then attempts to route requests to other healthy instances.
If the number of consecutive successful health checks for an unhealthy instance reaches the configured Threshold for Successful Detections, it is marked as healthy again.
For instructions on how to set the Threshold for Successful Detections and Maximum Failures, see Configure health checks in the console.
Single health check behavior
Function Compute performs a single health check as follows.
Sends a GET request to the specified HTTP path based on the function configuration.
If the GET request returns an HTTP status code from 200 to 399 before timing out, the check succeeds. Otherwise, it fails.
Scheduling after a health check failure
When a function instance becomes unhealthy, Function Compute attempts to route requests to other instances.
If Function Compute successfully routes the request, a new, healthy function instance executes it.
If Function Compute cannot route the request to another instance in time, it returns a health check failure error. On the next invocation, it attempts to schedule the request directly to a new function instance.
Configure health checks in the console
This section explains how to configure health checks for a function. The example uses a Web Function with the Node.js 18 Runtime.
Step 1: Create a function
Log in to the Function Compute console, and in the left navigation bar, select .
In the top navigation bar, select a region. On the Functions page, click Create Function.
On the Create Function page, select Web Function. For Custom Runtime, select . Then, click Create.
On the function details page, select the Code tab. Edit the function code in the code editor, and then click Deploy.
The following is 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 timeoutIn the code above,
readyzis the health check handler. It processes HTTP GET requests sent to the /readyz path. After you complete Step 2: Configure health checks for the function instance, you can configure Function Compute to periodically run this handler to check instance health.When Function Compute determines that your function instance is unhealthy, it attempts to route requests to other healthy instances. If Function Compute fails to do so, it returns an error related to the health check failure.
Step 2: Configure health checks
Log on to the Function Compute console, and in the left navigation bar, select .
In the top navigation bar, select a region. On the Functions page, click the function that you created in the previous step.
On the function details page, select the Configuration tab. To the right of Advanced Settings, click Modify. In the Advanced Settings panel, enable the Health Check switch, configure the parameters as described in the following table, and then click Deploy.
Parameter
Description
Request Path
The path to which Function Compute sends an HTTP GET request to perform a health check. The value must start with
/.Delay for First Detection
The delay in seconds after instance startup before the first health check runs.
Detection Interval
The interval in seconds between health checks.
Timeout Period
The timeout in seconds for a health check response. If a response is not received within this period, the check fails.
Maximum Failures
The number of consecutive failed health checks before an instance is marked as unhealthy.
Threshold for Successful Detections
The number of consecutive successful health checks required to mark an unhealthy instance as healthy again.
Step 3: Verify the health check
On the function configuration page, select the Code tab and click Test Function to view the result.
With the sample code in Step 1, the function instance passes the health check and returns OK.
Replace the health check handler in the code with the following snippet. Click Deploy. After the code is deployed, click Test Function again to see the result.
The modified code is as follows.
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 health check will now always fail with a 500 status code. When you invoke the function again, you will receive the following response:
{ "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 supports using an API to configure health checks for instances. For more information, see Update health check configurations.