By Che'er, Technical Expert of Alibaba Cloud Serverless
Currently, Alibaba Cloud Function Compute (FC) supports two types of functions: event function and HTTP function. The HTTP function, which is combined with HTTP triggers, can allow users to directly initiate the function calls using Restful API through HTTP requests.
As such, users can realize function calls without integrating the SDK provided by Function Compute and integrate better the with components and Web services of existing systems.
The original design of HTTP functions does not take the support for asynchronous calls into consideration. Therefore, when HTTP functions are used in more scenarios, the restriction on asynchronous calls hinders the use of HTTP functions.
Currently, many customers have used Function Compute HTTP triggers to build Web services. Many of them need to transcode files (videos, images, etc.), deliver tasks, and perform stress testing through Web services.
These requirements are often characterized by long execution and uneven traffic. Functions with these features have the following disadvantages when they are executed synchronously:
1. Long execution of functions increases the risk of errors and machine overhead.
Scenario: Video website transcodes videos during uploading. Connection interruption may occur due to long waiting times when refreshing the page, resulting in transcoding failure.
2. Cannot process and receive sudden traffic smoothly
Scenario 1: Pulsive Stress Testing
Scenario 2: Time-Limited Online Promotions
Customers can use asynchronous invocations in these scenarios to decouple HTTP triggers from function execution. This improves execution efficiency and success rate and reduces overheads.
Asynchronous invocation has the ability of target delivery. The model of asynchronous tasks with observability and manageability can enable customers to enjoy the convenience of function managing services.
When the HTTP trigger does not support asynchronous invocations, customers often need to indirectly implement an HTTP-triggered asynchronous invocation by function jump. The process is shown below
Customers can create two functions. Function A is an HTTP function, which is invoked synchronously through HTTP. Function B is an event function, which can be invoked asynchronously using an SDK. However, the shortcomings are obvious:
Function Compute currently supports the feature of making asynchronous invocations with HTTP triggers. Customers need to prepare an HTTP function and an HTTP trigger to use this feature.
Customers can use the Function Compute console, SDK, and Serverless Devs to create HTTP functions and triggers. The HTTP trigger can be configured to your liking. If you do not configure it, Function Compute will automatically create a default trigger for you.
You need to use the request header (X-Fc-Invocation-Type) to select the trigger method. The trigger is synchronous by default. If you need to implement an asynchronous trigger, you can add {"X-Fc-Invocation-Type":"Async"} to the request header.
Test in the function code and select asynchronous invocation:
You can implement an asynchronous invocation test on the test function side by selecting, "I want to invoke asynchronously."8
curl -v -H "X-Fc-Invocation-Type: Async" https://http-***.cn-shenzhen.fcapp.run/$path
Test Result:
The result of the Function Compute received request is returned immediately after the trigger is completed. Status code 202 indicates the request is successful, and the rest indicates that an error occurred. The request ID is also returned in the request header. You can use the returned request ID to track the execution status of the asynchronous request. You can activate asynchronous tasks [1] to observe the execution status of requests and increase control over function execution.
You can use the request ID to query execution result logs in the console:
You can use the asynchronous task list for requests that enable asynchronous tasks to monitor and query the status:
Please see the function callback documentation [2] for the callback of asynchronous execution results.
Here, we use HTTP-triggered video transcoding as an example to describe how to implement HTTP-triggered asynchronous tasks by combining Serverless Devs with the console.
1. Initialize the project:
s init http-video-transcode -d http-video-transcode
2. Enter the project and deploy:
cd http-video-transcode && s deploy
3. Use an HTTP trigger to initiate an asynchronous invocation:
curl -v -H "X-Fc-Invocation-Type: Async" -H "Content-Type: application/json" -d '{"bucket":"my-bucket", "object":"480P.mp4", "output_dir":"a", "dst_format":"mov"}' -X POST https://http-***.cn-shenzhen.fcapp.run/
4. Log on to the console to query the task status and control the task:
5. Function Callback
You can add processing logic to the dest-fail or dest-succ. As the execution result of a transcoding function, the callback function can sense and obtain the function execution result.
Function callback-related documents: https://help.aliyun.com/document_detail/422720.html
HTTP trigger supports asynchronous invocations. It builds Web services using Function Compute (FC) as a fully managed platform so HTTP trigger users can get an out-of-the-box asynchronous invocation experience.
[1] Asynchronous Task:
https://www.alibabacloud.com/help/en/function-compute/latest/async-tasks
[2] Result Callback: https://www.alibabacloud.com/help/en/function-compute/latest/result-callback
[3] Install Serverless Devs: https://www.alibabacloud.com/help/en/function-compute/latest/install-serverless-devs
[4] Configure Serverless Devs: https://www.alibabacloud.com/help/en/function-compute/latest/configure-serverless-devs
[5] Create an OSS Bucket:
https://www.alibabacloud.com/help/en/object-storage-service/latest/create-buckets-2
[6] Create a RAM Role: https://www.alibabacloud.com/help/en/resource-access-management/latest/create-a-ram-role-for-a-trusted-alibaba-cloud-service
The Details of Asynchronous Tasks: Function Compute Task Triggered Deduplication
99 posts | 7 followers
FollowAlibaba Cloud Storage - June 4, 2019
Alibaba Clouder - June 16, 2020
hyj1991 - July 22, 2019
Alibaba Clouder - April 16, 2019
Alibaba Cloud Serverless - February 17, 2023
Alibaba Clouder - September 24, 2020
99 posts | 7 followers
FollowAlibaba Cloud Function Compute is a fully-managed event-driven compute service. It allows you to focus on writing and uploading code without the need to manage infrastructure such as servers.
Learn MoreA unified, efficient, and secure platform that provides cloud-based O&M, access control, and operation audit.
Learn MoreManaged Service for Grafana displays a large amount of data in real time to provide an overview of business and O&M monitoring.
Learn MoreElastic and secure virtual cloud servers to cater all your cloud hosting needs.
Learn MoreMore Posts by Alibaba Cloud Serverless