Serverless Workflow can asynchronously invoke Function Compute functions in Task steps. Asynchronous invocation suits long-running tasks and manual audits, prevents throttling errors, and simplifies error handling procedures and retry logic. Serverless Workflow supports multiple integration patterns for asynchronous invocation.
Prerequisites
Complete the following operations:
-
Optional: Asynchronous invocation is enabled. For more information, see Feature overview.
With stateful asynchronous invocation enabled, you can stop function instances during execution for finer-grained flow control.
-
Function Compute has permissions to access Serverless Workflow, so that Serverless Workflow can receive callbacks after asynchronous function execution completes. Configure the following permission policy:
{ "Statement": [ { "Effect": "Allow", "Action": [ "fnf:ReportTaskSucceeded", "fnf:ReportTaskFailed" ], "Resource": [ "*" ] } ], "Version": "1" }For more information about authorization, see Grant Function Compute access to other services.
Why use asynchronous invocation
By default, Serverless Workflow uses the synchronous pattern when Serverless Workflow orchestrates Function Compute tasks. The flow waits for each function to return before proceeding to the next step:
version: v1
type: flow
steps:
- type: task
name: mytask
resourceArn: acs:fc:{region}:{account}:services/{serviceName}.{qualifier}/functions/{functionName}
Synchronous invocation has the following limitations:
-
Function Compute resource limits may cause throttling. By default, each Alibaba Cloud account allows a maximum of 300 pay-as-you-go instances per region.
Serverless Workflow invocations share the same quota with invocations from other services. Throttling may occur even with complex retry policies in your Serverless Workflow flow.
-
Long-running tasks require a persistent connection between Serverless Workflow and Function Compute, making them vulnerable to network instability.
Asynchronous invocation with Serverless Workflow integration patterns solves these issues and supports the following scenarios:
-
Proceed to subsequent steps without waiting for the current step to complete.
-
Skip the current step and proceed to the next step when unexpected errors occur.
Scenarios
Serverless Workflow supports three patterns for asynchronous Function Compute invocation: request-response (requestResponse), synchronous (sync), and wait-for-callback (waitForCallback).
|
Integration pattern |
Value of the pattern parameter |
Scenario |
|
Request-response pattern (default pattern) |
Sample flow:
|
Long-running tasks where execution results are non-consequential. |
|
Synchronization pattern |
Sample flow:
|
Long-running tasks that may be throttled. |
|
Wait-for-callback pattern |
Sample flow:
|
The flow must proceed regardless of whether the current step completes. Example: manual audit. |
Service parameters
In these examples, Serverless Workflow uses Function Compute as the task node. resourceArn specifies the destination service, and serviceParams configures the invocation. serviceParams supports:
-
InvocationType: the function invocation type. Valid values:Sync(synchronous) andAsync(asynchronous). -
Optional:
StatefulAsyncInvocationID: the ID of a stateful asynchronous invocation. Use this ID to locate the task in the Function Compute console.NoteWhen
InvocationTypeis set toAsyncwith a StatefulAsyncInvocationID, Serverless Workflow uses stateful asynchronous invocation. The StatefulAsyncInvocationID value must be unique across all functions.
Integration patterns
Request-response pattern
When the flow reaches the specified step, Serverless Workflow asynchronously invokes the function and proceeds to the next step without waiting for a callback or execution completion.
version: v1
type: flow
steps:
- type: task
name: mytask
resourceArn: acs:fc:{region}:{account}:services/{serviceName}.{qualifier}/functions/{functionName}
pattern: requestResponse # Async invocation with sync pattern
serviceParams:
InvocationType: Async
In this example, the function triggers at the mytask step. The flow proceeds to the next step immediately, regardless of whether the function has completed.
Synchronization pattern
When the flow reaches the specified step, Serverless Workflow asynchronously invokes the function. The Serverless Workflow flow waits until notified that the function execution is complete before proceeding.
version: v1
type: flow
steps:
- type: task
name: mytask
resourceArn: acs:fc:{region}:{account}:services/{serviceName}.{qualifier}/functions/{functionName}
pattern: sync # Async invocation with sync pattern
serviceParams:
InvocationType: Async
In this example, the function triggers at the mytask step. The flow suspends until notified that the function execution is complete.
The synchronous integration pattern behaves the same as synchronous function invocation. Only the invocation method differs.
Wait-for-callback pattern
When the flow reaches the specified step, Serverless Workflow asynchronously invokes the function and passes a task token. The flow suspends until you use the task token to report the execution result, regardless of whether the function has completed.
version: v1
type: flow
steps:
- type: task
name: mytask
resourceArn: acs:fc:{region}:{account}:services/{serviceName}.{qualifier}/functions/{functionName}
pattern: waitForCallback # Async invocation with sync pattern
serviceParams:
InvocationType: Async
In this example, the function triggers at the mytask step. The flow suspends until it receives a callback via the ReportTaskSucceed or ReportTaskFailed API operation. You can initiate the callback at any time, whether or not the function has completed.
Example: asynchronous function invocation
Serverless Workflow combines asynchronous invocation with stateful asynchronous invocation for job-type scenarios. With both features enabled, you can monitor execution status and stop function execution at any time. The following FDL snippet shows this configuration in Serverless Workflow:
version: v1
type: flow
steps:
- type: task
name: mytask
resourceArn: acs:fc:::services/{serviceName}.{qualifier}/functions/{functionName}
pattern: sync # Async invocation with sync pattern
inputMappings:
- target: id
source: $context.execution.name
serviceParams:
InvocationType: Async
StatefulAsyncInvocationID: $.id