This topic describes the background information and scenarios in which asynchronous invocations can be used. This topic also describes how to defer the invocation of a function.

Background information

When Function Compute receives an asynchronous invocation request, the system returns a response immediately after the request is persisted instead of waiting for the request to be completed before returning a response. Function Compute ensures that the request is executed at least once. To obtain the results of the asynchronous invocation, you can configure destinations for the asynchronous invocation. For more information, see Result callback. To obtain the status of the asynchronous request at each phase, you can enable the task mode. For more information, see Feature overview.

Scenarios

If your function contains logic that is time-consuming, resource-consuming, or error-prone, you can use asynchronous invocations to allow your programs to respond to traffic spikes more quickly and reliably. Examples:
  • In a user registration system, a new user sends a registration request. After the registration is successful, the system sends email notifications to the user. The action of sending an email can be separated from the registration process and executed asynchronously.
  • When you upload a file, actions such as format conversion and import and export can be separated from the data upload process and executed asynchronously.
Note HTTP functions can be invoked synchronously or asynchronously. This feature is in public review.

Deferred invocation of a function

In some scenarios, after you submit an asynchronous invocation request, you may want Function Compute to defer the invocation. In this case, you can use the Function Compute API or SDK to defer the invocation.

Add the HTTP request header x-fc-async-delay to the code. Valid values: (0,3600). Unit: seconds. Function Compute invokes the function after the period specified by x-fc-async-delay elapses.

The following code provides an example on how to use Function Compute SDK for Go to defer a function invocation:
package main

import (
    "fmt"
    "os"

    "github.com/aliyun/fc-go-sdk"
)
func main() {
    fcClient, err := fc.NewClient(fmt.Sprintf("%s.cn-shanghai.fc.aliyuncs.com", os.Getenv("ACCOUNT_ID")),
        "2016-08-15", os.Getenv("ACCESS_KEY_ID"), os.Getenv("ACCESS_KEY_ID_SECRET"))
    if err != nil {
        panic(err)
    }

    // invoke function with delay
    invokeInput := fc.NewInvokeFunctionInput({ServiceName}, {FunctionName}).WithPayload({payload})
    invokeInput = invokeInput.WithAsyncInvocation().WithHeader("x-fc-async-delay", "200")
    _, err := FCClient.InvokeFunction(invokeInput)
    if err != nil {
        panic(err)
    }
}
Notice Invocations that are deferred by using the preceding method may be not accurate in some scenarios. If you need a more accurate deferred invocation, use a time trigger. For more information, see Configure a time trigger.

Common features

See the following topics to learn about common features of asynchronous invocations: