When you invoke a function asynchronously, you get no built-in way to track whether it succeeded, failed, or is still running. Asynchronous task mode solves this by recording every state transition for each invocation—from queued to completed or failed—and exposing those records for real-time queries and management.
With task mode enabled, you can:
Query the current status of any task at any point in its lifecycle.
Stop a specific running task by its ID.
Prevent duplicate executions by assigning a unique task ID to each invocation.
Monitor queue depth, processing latency, and instance-level resource usage.
Coordinate tasks in large-scale workflows using CloudFlow.
Task mode adds minimal latency to invocations and executions. This latency is not billed. For billing details, see Billing overview.
How task mode works
When task mode is enabled, Function Compute records each state transition for every asynchronous invocation. After you submit a task:
Function Compute queues the invocation message (status: Enqueued).
The message is dequeued and waits for an available instance (status: Processed).
An instance picks up the task and runs it (status: Running).
The task transitions to a terminal state: Succeeded, Failed, Stopped, Expired, or Invalid.
You can query the state at any step using the console, the API, or Serverless Devs.
Limitations
Latency sensitivity: Disable task mode if your application requires an average end-to-end latency of less than 100 ms.
Throughput: Disable task mode if you need to invoke functions asynchronously at thousands of requests per second (RPS) or higher.
Retention period: Task records are queryable for 7 days after creation.
Supported 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).
Task statuses
Function Compute tracks the following statuses for each asynchronous task:
| Status | Description |
|---|---|
| Enqueued | The invocation message is queued and awaiting processing. |
| Processed | The message has been dequeued and the task is awaiting invocation. |
| Running | The task is running on an instance. |
| Succeeded | The task completed successfully. |
| Failed | The task failed. |
| Stopped | The task was manually stopped. |
| Stopping | The system is processing a stop request. |
| Expired | The message time-to-live (TTL) expired before the task was processed; the message was discarded. |
| Invalid | The task could not be run—for example, because the function was deleted. |
| Retrying | The task is being retried after an execution error. The status changes to Running once the retry starts. |
To retrieve the current status of a specific task, call GetAsyncTask. To filter tasks by status or other conditions, call ListAsyncTasks.
Comparison with Kubernetes jobs
To run asynchronous tasks, you can use the asynchronous task feature of Function Compute or Kubernetes jobs. The following table compares the two approaches.
| 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 including logs, metrics such as 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 | Select an appropriate instance type. Instances scale automatically; you are billed based on actual task processing duration, resulting in high resource utilization. | You must specify the instance type and count when submitting 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 | Supports both scheduled and delayed submission. | 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 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. |
Enable task mode
Enable task mode when creating a function
Log on to the Function Compute console. In the left-side navigation pane, choose Function Management > Function.
In the top navigation bar, select a region. On the Function page, click Create Function.
In the dialog box, select Task Function and click Create. Set the function name and click Create.
After the function is created, go to the function details page and click the Tasks tab. Task mode is enabled by default.
For configuration details, see Create a task function.
Enable task mode on an existing function
Before you begin, ensure that you have created a function. 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 target function.
On the function details page, click the Tasks tab, then click Modify next to Task Mode.
In the Task Mode panel, set Task Mode to Enable and click Deploy.
Enable task mode via API
Call PutFunctionAsyncInvokeConfig with asyncTask set to true. All subsequent asynchronous invocations for the function use task mode.
{
"asyncTask": true
}Enabling task mode does not affect synchronous invocations. Task mode applies only to asynchronous invocations.
Submit a task
Submit a task from the console
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 target function.
On the function details page, click the Tasks tab, then click Submit Task.
In the dialog box, select a task ID generation method and specify the execution time, then click OK.
To pass test parameters before submitting, click the arrow icon next to Submit Task and select Configure Test Parameters. For event functions, parameters are passed as an event payload. For HTTP functions, parameters are passed as HTTP parameters.
After submission, the task list shows Task Status, Task ID, and Instance ID.
Submit a task via API
Call InvokeFunction with the x-fc-invocation-type header set to Async. To assign a custom task ID for deduplication, include the x-fc-async-task-id header.
Example: submit a task and poll its status
The following example shows the complete lifecycle of submitting an asynchronous task and querying its status until it completes.
Enable task mode on the function (one-time setup):
PUT /2023-03-30/functions/{functionName}/async-invoke-config Content-Type: application/json { "asyncTask": true }Submit the task with a custom task ID:
POST /2023-03-30/functions/{functionName}/invocations x-fc-invocation-type: Async x-fc-async-task-id: my-task-001Query the task status using the task ID:
GET /2023-03-30/functions/{functionName}/async-tasks/my-task-001The response includes the current status (
Enqueued,Running,Succeeded, etc.). Poll this endpoint until the status reaches a terminal state.
For a runnable code sample that sets a task ID during invocation, see InvokeFunction sample (international site).
Stop a task
Stop a task from the console
On the Tasks tab of the function details page, locate the task in the task list and stop it.
Stop a task via API
Call StopAsyncTask with the task's TaskID. To find the TaskID, call ListAsyncTasks.
Task deduplication
Function Compute uses a globally unique TaskID to prevent duplicate executions. If a submission fails—for example, due to a timeout—retry with the same TaskID. Function Compute rejects duplicate submissions with a 409 error.
Assign a TaskID to every task by including the x-fc-async-task-id header in the invocation request. Use this ID when retrying failed submissions.
If you set RequestID without setting TaskID, Function Compute automatically sets TaskID to RequestID. Set TaskID directly and leave RequestID unset.
Trigger tasks with event triggers
The following triggers support asynchronous invocation and can trigger asynchronous tasks:
EventBridge-integrated triggers:
Set Invocation Method to Async Invocation for HTTP triggers and EventBridge-integrated triggers.
Monitor tasks
Use Function Compute monitoring metrics to track task queue depth, detect errors, and respond to timeouts in real time.
View metrics
Before you begin, ensure that you have created a function. 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 Function page, click the target function.
On the function details page, click the Monitoring tab.
Click Function Metrics to view asynchronous invocation metrics: Asynchronous Invocation Processing (count), Asynchronous Invocation Processing Latency (ms), Asynchronous Invocation Trigger Events (count), and Asynchronous Requests Backlogs (count).
Click Instance Metrics to view instance-level resource usage for asynchronous task instances.
Instance-level metrics require the logging feature to be enabled. See Configure logging.
For a full list of monitoring metrics, see Monitoring metrics.
Set alert rules
To set an alert on a metric, click the bell icon in the upper-right corner of the metric chart. Set the alert conditions and threshold in the Create Metric-based Alert Rule panel, then click OK.
Example: send a medium-severity alert when backlogged asynchronous invocations reach 5 or more, delivered 24/7 via DingTalk, email, phone, and webhook.
Orchestrate tasks with CloudFlow
Integrate Function Compute with CloudFlow to coordinate asynchronous tasks in large-scale workflows. CloudFlow tracks each task's state transitions, applies your retry logic, and ensures reliable execution of predefined steps. Supported CloudFlow states include Pass, Map, and Parallel.
For integration details, see Integrate with Function Compute.
Prerequisites
Before you begin, ensure that you have:
An asynchronous task enabled (see Enable task mode)
Granted CloudFlow access to Function Compute. See Use a function role to grant Function Compute permissions to access other cloud services
Create and run a workflow
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 creation method and workflow mode. CloudFlow supports two modes: Standard and Express. See Standard workflows and Express workflows and Create a workflow from a blank canvas for details.
Define the workflow. In the upper-right corner of the flow details page, click YAML. Enter the flow definition in the code editor, then click Save. The following example invokes a Function Compute function asynchronously and waits for it to complete before the workflow ends (
TaskMode: RequestComplete):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, click Configure Workflow. Select an Execution Role and click Save.
NoteThe role must be attached to the AliyunFCInvocationAccess policy.
On the flow details page, click Execute to test the workflow.