Stateful asynchronous invocation allows you to perceive the business status in real time and control the execution of functions so that you can better handle some abnormal situations, such as alert notifications and automated retries. This topic describes best practices for stateful asynchronous invocation in Function Compute.

Background information

Function as a service (FaaS) supports stateless, short-term, and lightweight computing in the early stage, such as API backend services, image processing, and data processing in streaming mode. In various scenarios, FaaS functions can be asynchronously or synchronously invoked. 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 and queues to be processed by the backend service.

Scenarios

Business in some online live streaming scenarios is gradually migrating to FaaS due to business development. In such business scenarios, execution duration is long, latency is sensitive, and high availability is required. Developers must perceive the business status in real time and control the execution of functions. Neither asynchronous invocation nor synchronous invocation can be used because latency is insensitive during asynchronous invocation, and persistent connections are unstable and client resources are occupied during synchronous invocation. However, asynchronous invocation can implement load shifting in offline business where a large amount of traffic bursts, to smoothly buffer traffic for downstream systems of the business. Function Compute introduces stateful asynchronous invocation based on asynchronous invocation. Stateful asynchronous invocation is applicable to the following business scenarios:
  • Observable execution
    • During the execution, you can view the status of the execution in real time, such as whether a message is dequeued, whether the execution of the function is triggered, and whether an execution result is sent to the destination.
    • The business has the task property.
      • You can view the event that triggers a specific execution and real-time execution logs.
      • You can query historical records based on the name, time, or status of an execution.
  • Operable execution
    The business has the task class property.
    • If the execution duration is long, you can manually stop function execution in some cases.
    • You can perform historical failed or successful executions again.

For more information about stateful asynchronous invocation, see Overview.

Example

The following example shows how to use Funcraft and the Function Compute console to implement asynchronous invocation with a Message Service (MNS) queue as the destination for the asynchronous invocation:

You can enable stateful asynchronous invocation to achieve the following effects:
  • After a function starts to be asynchronously executed, you can use the Function Compute console, SDKs, or API operations to view the execution status.
  • If a function fails to be executed or the execution of the function is manually stopped, Function Compute pushes asynchronously invoked messages to the specified MNS queue to implement the feature of a dead-letter queue. This allows you to perceive the messages that fail to be executed by the asynchronously invoked function and process them later.

Procedure

  1. Create an MNS queue. For example, the name of the created queue is stateful-invocation-dlq. For more information, see Create a queue.
  2. Run the following command to clone the sample project:
    git clone https://github.com/awesome-fc/Stateful-Async-Invocation.git
  3. Replace {please_replace_me} in the template.yml file with the name of the created queue.
  4. Run the following command in the directory of the sample project to deploy a function:
    fun deploy --use-ros --stack-name demo-1
    After the function is deployed, you can log on to the Function Compute console to check whether the function is created and stateful asynchronous invocation is enabled.

What to do next

  • Asynchronous invocation configurations take effect only during asynchronous invocation. You can send asynchronous invocation requests by using SDKs or CLI tools.
    In this example, you can run the following command in the directory of the sample project to send an asynchronous invocation request and view the MNS queue:
    invk main -t Async -s '{"failure":true}'
  • You can view and manage asynchronous invocations in the Function Compute console.
    • View the configuration of an asynchronous invocation.
    • View the status of an asynchronous invocation.
    • Stop an asynchronous invocation that is in progress.
    • View historical executions and the result of a specific execution.
    • Copy the payload.
    • Re-trigger an asynchronous invocation.
    Note To view detailed information such as execution logs of a function, you must configure Log Service resources for the function. For more information, see Configure Log Service resources and view function execution logs.
  • After a function is executed, if the destination is configured, you can go to the destination to view the push records.
    In this example, if a destination is configured, you can receive the message pushed to the destination in the MNS console, as shown in the following code:
    {
      "timestamp":xxxx,
      "requestContext":{
        "requestId":"xxxx",
        "functionArn":"acs:fc:::services/Stateful-Invocation-demo.LATEST/functions/main",
        "condition":"UnhandledInvocationError",
        "approximateInvokeCount":1
      },
      "requestPayload":"{\"failure\":true}",
      "responseContext":{
        "statusCode":200,
        "functionError":"UnhandledInvocationError"
      },
      "responsePayload":"{\"errorMessage\":\"Process exited unexpectedly before completing request (duration: 13689ms, maxMemoryUsage: 10.86MB)\"}"
    }
     
    Note If an asynchronous invocation fails or is manually terminated, you can receive detailed error information about the asynchronous invocation in MNS.