All Products
Search
Document Center

Function Compute:Task deduplication

更新時間:Jul 27, 2023

Function Compute allows you to configure globally unique IDs for the submitted tasks. You can retry a task by using the task ID when an unexpected event occurs. For example, a timeout error occurs when a task-submission operation is asynchronously called. The task deduplication feature helps avoid repeated execution of tasks. This topic describes how to set task IDs to implement task deduplication.

How it works

Function Compute provides the task ID feature. A task ID is globally unique. We recommend that you specify a task ID each time you submit a task. You can use the task ID to retry the task in case of an error such as a timeout error. Function Compute verifies the tasks that you repeatedly submit. When the same ID enters the system, the request is considered as a duplicate submission and rejected, and an error 409 is returned.

Note

Function Compute also provides request IDs. If you set the request ID but do not set the task ID, the system automatically set the task ID to the request ID. When you use asynchronous tasks, we recommend that you specify the task ID and do not specify the request ID.

Set the task ID

You can submit a task for execution by using the Function Compute console, using Serverless Devs, or calling an API operation. To set the task ID, add the HTTP request header X-Fc-Stateful-Async-Invocation-Id when the function is triggered.

The SDK for Go is used as an example. The following code shows how to set the task ID when the task is triggered:

import fc "github.com/aliyun/fc-go-sdk"

func SubmitJob() {
    invokeInput := fc.NewInvokeFunctionInput("ServiceName", "FunctionName")
    invokeInput = invokeInput.WithAsyncInvocation().WithStatefulAsyncInvocationID("TaskUUID")
    invokeOutput, err := fcClient.InvokeFunction(invokeInput)
    ...
}