All Products
Document Center

Function Compute (2.0):Best practices for asynchronous tasks

Last Updated:Feb 02, 2024

Asynchronous tasks allow you to obtain information about your business status in real time and manage function execution. This helps you handle abnormal issues such as alert notifications and automated retries. This topic describes the best practices for asynchronous tasks in Function Compute.

Background information

Function as a service (FaaS) was originally used for stateless, short-term, and lightweight computing, such as API backend services, image processing, and stream processing. FaaS functions can be asynchronously or synchronously invoked in various scenarios. When a function is synchronously invoked, the function is immediately executed. When a function is asynchronously invoked, 202 HttpCode is immediately returned to the invoker, and the asynchronously invoked message is enqueued for processing by the backend service.


Due to development in business, some online live streaming business is migrated to FaaS. Such business features long execution time, high latency sensitivity, and high availability requirement. Therefore, developers must obtain information about their business status in real time and manage function execution. In this case, neither asynchronous invocations nor synchronous invocations can be performed because latency is insensitive during asynchronous invocations, and persistent connections are unstable and client resources are occupied during synchronous invocations. Since synchronous invocations can help balance the loads of offline business to smoothly buffer traffic for downstream systems when traffic spikes occur, Function Compute introduces asynchronous tasks based asynchronous invocations. Asynchronous tasks can be used in the following business scenarios:

  • Observable execution

    • You want to view information about an execution in real time, such as whether a message is removed from the queue, a function is triggered, or an execution result is sent to the destination.

    • Your business requires to be observed in the same way as task executions are observed.

      • You want to view the event that triggers a specific execution and the real-time execution logs.

      • You want to query historical records based on the name, time, or status of an execution.

  • Operable execution

    Your business requires to be operated in the same way as task executions are operated.

    • In some cases, if the execution duration is long, you want to stop function execution.

    • You want to perform failed or successful executions again.

For more information about asynchronous tasks, see Overview.


The following example shows how to use Serverless Devs together with the Function Compute console to perform an asynchronous invocation for which the destination is Message Service (MNS).

You can use asynchronous tasks to obtain the following results:

  • After a function starts to be asynchronously executed, you can view the execution status by using the Function Compute console or SDKs, or by calling Function Compute API operations.

  • If a function fails to be executed or the execution of the function is stopped, Function Compute pushes the asynchronous message to the specified MNS queue to use the dead-letter queue feature. This way, you can obtain information about the message and process the message later.

Before you start


  1. Run the following command to clone the project:

    git clone
  2. Run the following command to go to the project directory:

    cd Stateful-Async-Invocation
  3. Modify the s.yaml file.

    Change the value of the first {please_replace_me} parameter to the ID of your Alibaba Cloud account. Change the values of the other two {please_replace_me} parameters to the names of the destination MNS queues that you configured for the asynchronous invocation.

  4. Run the following command in the project directory to deploy the function:

    s deploy -y

    After the function is deployed, you can log on to the Function Compute console to check whether the service and function are created, and whether the asynchronous task feature is enabled for the function.

Additional information

  • Configurations for asynchronous invocations take effect only when asynchronous invocations are performed. You can send asynchronous invocation requests by using SDKs or CLI tools.

    In this example, the following command is run in the directory of the sample project to send an asynchronous invocation request and view the MNS queue:

    s invoke --invocation-type async -e '{"failure":true}'
  • You can view and manage asynchronous invocations in the Function Compute console. For more information, see Task management.


    To view the detailed information such as the execution logs of a function, you must configure Simple Log Service for the function. For more information, see Configure the logging feature.

  • After a function is executed, if a destination is configured, you can go to the destination to view the push records.

    In the example of this topic, if a destination is configured, you can receive the message pushed to the destination in the MNS console. Sample code:

      "responsePayload":"{\"errorMessage\":\"Process exited unexpectedly before completing request (duration: 13689ms, maxMemoryUsage: 10.86MB)\"}"

    If an asynchronous invocation fails or is terminated, you can receive information about the error in MNS.