For asynchronous function invocations, you can enable task mode to process the request. This mode lets you track and save the status of each invocation stage, providing enhanced control and observability. This topic describes the background, limitations, and features of asynchronous tasks.
Background
After you enable asynchronous tasks, you can:
Record detailed status transitions, such as Enqueued, Succeeded, and Failed, for each function invocation.
Manage the lifecycle of function requests, including terminating running asynchronous tasks.
Asynchronous tasks save state information, which adds minimal latency to function invocations and executions. You are not charged for this latency. For more information about Function Compute billing, see Billing overview.
Limitations
Use case limitations
Asynchronous tasks offer more features but also introduce system overhead. We recommend disabling task mode in the following scenarios:
Your application is highly sensitive to end-to-end request latency and requires an average latency of less than 100 ms.
You need to make asynchronous invocations at thousands of requests per second (RPS) or higher.
Region limitations
Asynchronous tasks are supported in the following regions: China (Hangzhou), China (Shanghai), China (Qingdao), China (Beijing), China (Zhangjiakou), China (Ulanqab), China (Shenzhen), China (Chengdu), China (Hong Kong), Singapore, UK (London), US (Silicon Valley), US (Virginia), Germany (Frankfurt), Malaysia (Kuala Lumpur), Indonesia (Jakarta), Thailand (Bangkok), Japan (Tokyo), and South Korea (Seoul).
Retention period
You can query the status of tasks created within the last 7 days.
Comparison with Kubernetes jobs
To run asynchronous tasks, you can use the asynchronous task feature of Function Compute or Kubernetes jobs. Function Compute provides a ready-to-use, serverless platform for running asynchronous tasks that is more efficient, controllable, and observable. The following table compares the two methods.
Feature | Function Compute tasks | Kubernetes jobs |
Use cases | Suitable for both real-time tasks lasting tens of milliseconds and offline tasks lasting tens of hours. | Suitable for offline tasks with a fixed workload and low requirements for submission speed and real-time performance. |
Task observability | Supported. Provides comprehensive observability features, including logs, metrics such as the queued task count, task path duration, and task status queries. | Requires manual integration of open source software. |
Automatic scaling of task instances | Supported. Automatically scales instances based on the number of queued tasks and resource utilization. | Requires manually implementing scaling and load balancing with a task queue, which adds complexity. |
Instance scaling speed | Millisecond-level. | Minute-level. |
Instance resource utilization | You only need to select an appropriate instance type. Instances scale automatically, and you are billed based on the actual task processing duration, resulting in high resource utilization. | You must specify the instance type and count when you submit a job. Auto scaling and load balancing are difficult to implement, leading to low resource utilization. |
Task submission throughput | A single user can submit tens of thousands of tasks per second. | A cluster can start a maximum of a few hundred jobs per second. |
Scheduled or delayed submission | Supported. | Supports scheduled submission but not delayed submission. |
Task deduplication | Supported. | Not supported. |
Pause or resume task execution | Supported. | Supported only in Kubernetes 1.21 and later. |
Terminate a specific task | Supported. | Limited support. Tasks are terminated indirectly by stopping task instances. |
Task throttling | Supported. Throttling can be configured at different granularities, such as at the user or function level. | Not supported. |
Automatic result callbacks | Supported. | Not supported. |
Development and O&M costs | You only need to implement the task processing logic. | Requires maintaining a Kubernetes cluster. |
Task management
Asynchronous task statuses
For each task invocation, Function Compute records state transitions and enables real-time status queries. You can view the specific status of a task by using an SDK or the Function Compute console. A task can be in one of the following statuses:
Status | Description |
Enqueued | The asynchronous message is queued and awaiting processing. |
Processed | The asynchronous message has been dequeued and is awaiting invocation. |
Running | The task is executing on an instance. |
Succeeded | The task completed successfully. |
Failed | The task failed. |
Stopped | The task has been manually stopped. |
Stopping | The system is processing a manual request to stop the task. |
Expired | The message's time-to-live (TTL) expired before it could be processed, so it was discarded. |
Invalid | The task could not be executed and is now in an invalid state, for reasons such as the function or service being deleted. |
Retrying | The asynchronous invocation is being retried due to an execution error. Once the retry starts, the status will change to Running. |
Call the GetAsyncTask API operation to get details about a task execution, or call the ListAsyncTasks API operation to filter tasks by status.
Manage tasks
Console
Enable tasks on function creation
Log on to the Function Compute console. In the left-side navigation pane, choose .
In the top navigation bar, select a region. On the Function page, click Create Function.
In the pop-up dialog box, select Task Function and click Create {title}. On the Create {title} page, set the function name and then click Create.
For more information about the configuration parameters, see Create a task function.
After the function is created, navigate to the function details page and click the Tasks tab. The task mode is enabled by default.
Enable tasks for existing functions
You must create a function first. For more information, see Create a function.
Log on to the Function Compute console. In the left-side navigation pane, click Functions.
In the top navigation bar, select a region. On the Functions page, click the function that you want to manage.
On the function details page, click the Tasks tab, and then click Modify next to Task Mode.
In the Task Mode panel, set Task Mode to Disable and click Deploy.
Start or stop a task
Log on to the Function Compute console. In the left-side navigation pane, click Functions.
In the top navigation bar, select a region. On the Functions page, click the function that you want to manage.
On the function details page, click the Tasks tab, and then click Submit Task. In the dialog box that appears, select a task ID generation method and specify the task execution time, and then click OK.
You can also click the arrow icon
next to Submit Task and select Configure Test Parameters from the drop-down list. For an event function, the parameters are passed as an event payload. For an HTTP function, the parameters are passed as HTTP parameters to simulate task submission.In the task list below, you can view the Task Status, Task ID, and Instance ID. You can also log on to the instance.
API
Configure task mode
To enable task mode, call the PutFunctionAsyncInvokeConfig API operation and set the asyncTask parameter to true. All subsequent asynchronous invocations for the function will then use task mode.
{
"asyncTask": true
}Even after you enable task mode, you can still invoke the function synchronously. The task mode applies only to asynchronous invocations.
Trigger a function asynchronously
To trigger an asynchronous task, call the InvokeFunction API operation and set the x-fc-invocation-type header to Async. To assign a custom task ID, include the HTTP header x-fc-async-task-id in the request. For more information, see Task deduplication.
View a task
After you submit a task, you can call the GetAsyncTask API operation to query its status or execution records.
If you need to find tasks that match specific keywords or conditions, you can call the ListAsyncTasks API operation.
Stop a task
To stop a running task, call the StopAsyncTask API operation with its TaskID. You can use the custom TaskID you provided or find the ID by calling the ListAsyncTasks API operation.
Event triggers
You can trigger asynchronous tasks by creating a trigger that supports asynchronous invocation. The following triggers support this feature:
Triggers integrated with EventBridge, including:
ImportantMake sure that the Invocation Method for HTTP triggers and triggers integrated with EventBridge is set to Async Invocation.
Task deduplication
To prevent duplicate executions, Function Compute lets you assign a globally unique ID to each task. If a task submission fails, for example due to a timeout, you can safely retry the request by using the same task ID. For more information about retry behavior, see retry policy.
Function Compute uses a globally unique TaskID for deduplication. We recommend specifying a TaskID for every task submission and use it for retries in case of timeouts or other failures. Function Compute rejects subsequent submissions with a duplicate TaskID with a 409 error.
Function Compute also provides the RequestID concept. If you set RequestID but do not set TaskID, the system automatically sets TaskID to RequestID. When you use asynchronous tasks, we recommend that you set TaskID without setting RequestID.
Set a TaskID
You can submit a task from the Function Compute console, by using Serverless Devs, or by calling an API operation. To set a TaskID, specify the x-fc-async-task-id header in the invocation request.
For sample code on how to set a TaskID when triggering a task, see Sample Code (International site).
Task monitoring
You can use the monitoring metrics provided by Function Compute to get real-time status updates for asynchronous tasks and promptly detect errors, timeouts, and other exceptions.
Prerequisites
A function is created. For more information, see Create a function.
Procedure
Log on to the Function Compute console. In the left-side navigation pane, click Functions.
In the top navigation bar, select a region. On the Function page, click the target function.
On the function details page, click the Monitoring tab to view metrics related to asynchronous invocations and resource usage for asynchronous task instances. For more information about monitoring metrics, see Monitoring metrics.
Click the Function Metrics tab to view metrics related to asynchronous invocations, such as Asynchronous Invocation Processing (count), Asynchronous Invocation Processing Latency (ms), Asynchronous Invocation Trigger Events (count), and Asynchronous Requests Backlogs (count).
Click the Instance Metrics tab to view instance-level resource usage for asynchronous tasks.
NoteBefore you can view instance-level metrics, you must enable the logging feature. For more information, see Configure logging.
Set an alert rule for a metric.
To monitor a metric such as memory usage or vCPU usage, click the bell icon
in the upper-right corner of the metric chart. You will be redirected to the Create Metric-based Alert Rule panel. Set the alert conditions and threshold as prompted, and then click OK.The following figure shows an example of an alert rule configuration. In this example, if the number of backlogged asynchronous invocations for a function is greater than or equal to 5, a medium-severity alert is sent 24/7 to the specified contacts via DingTalk, email, phone, and webhook.

Task orchestration
Integrate with CloudFlow to use asynchronous function invocations in large-scale, complex task workflows. You can use states like Pass, Map, and Parallel to orchestrate Function Compute asynchronous tasks. CloudFlow reliably coordinates executions according to predefined steps, tracks each task's state transitions, and applies your retry logic as needed to ensure the workflow completes successfully. For more information, see Integrate with Function Compute.
Prerequisites
Create an asynchronous task. For more information, see Task management.
Grant CloudFlow access to Function Compute. For more information, see Use a function role to grant Function Compute permissions to access other cloud services.
Procedure
Log on to the CloudFlow console. In the top navigation bar, select a region.
In the left-side navigation pane, click Workflows. On the Workflows page, click Create Workflow.
In the Create Workflow dialog box, select a method and a mode for creating the workflow.
CloudFlow supports two workflow modes: Standard and Express. For more information about the two modes, see Standard workflows and Express workflows. For more information about creating a workflow, see Create a workflow from a blank canvas.
Edit the workflow you just created.
Define the flow.
You can use the CloudFlow Studio editor or the YAML editor. This example uses the YAML editor.
In the upper-right corner of the flow details page, click YAML. Enter the flow definition language in the code editor on the left, and then click Save.
The following is an example:
Type: StateMachine Name: MyFlow SpecVersion: v1 StartAt: InvokeFunction States: - Type: Task Name: InvokeFunction Action: FC:InvokeFunction TaskMode: RequestComplete Parameters: invocationType: Async resourceArn: acs:fc:us-west-1:103435468****:functions/func-i1****/LATEST End: trueConfigure the execution role.
In the upper-right corner of the flow details page, click Configure Workflow. Select an Execution Role, and then click Save.
NoteEnsure the role you configure for the flow is attached to the AliyunFCInvocationAccess policy.
On the flow details page, you can click Execute in the upper-right corner to test the workflow.