Function Compute HTTP trigger supports asynchronous

Currently, Alibaba Cloud Function Compute supports two types of functions: event functions and HTTP functions. Among them, HTTP functions combined with HTTP triggers can support users to directly initiate function calls by using Restful API through HTTP requests;

In this way, users can implement function calls without integrating the SDK provided by Function Compute, and better integrate with existing system components and web services.

Considering the purpose of the original design of HTTP functions, for Web scenarios, HTTP functions do not support asynchronous calls. With the increase in the number of scenarios where users use HTTP functions, HTTP functions cannot support the limitation of asynchronous calls. The wider use of HTTP functions by users brings many restrictions.

Does not support asynchronous calls, how difficult is it for users?

At present, many customers have used Function Compute HTTP triggers to build WEB services, and many of them have the need to transcode files (videos, pictures, etc.) through Web services, deliver tasks, and perform stress testing.
These requirements often have the characteristics of long execution and uneven traffic. Functions with these characteristics have the following disadvantages in the case of synchronous execution:

1. Long execution functions increase the risk of function errors and increase machine overhead.

• The client needs to maintain a long link, network fluctuations, and the client loses patience and disconnects the link because the function takes a long time to execute, all of which increase the probability of function errors.
Scenario: A video website user uploads a video for transcoding, and it takes a long time to refresh the page, causing the link to be interrupted and the transcoding to fail.
• Keeping long links increases the client's machine overhead and reduces the resource utilization of the client's machine.

2. In the face of sudden traffic, it cannot be smoothly processed and received.
• For scenarios with concurrency restrictions, the client's burst traffic will be limited in the synchronous call scenario, which will cause certain requests to fail when the client does not handle errors.
Scenario 1: Pulse stress measurement scenario.
Scenario 2: Limited time online promotion.
In these scenarios, customers can decouple HTTP triggering and function execution through asynchronous calls, improving execution efficiency and execution success rate, and reducing overhead.
The guarantee of asynchronous invocation at least once, the ability of target delivery, and the asynchronous task mode with observability and controllability can better allow customers to enjoy the convenience of function hosting services and free their hands.
When HTTP triggers do not support asynchronous calls, in order to meet their needs, customers often need to indirectly implement HTTP-triggered asynchronous calls through function jumps. The specific process is as follows:
Customers can create two functions, function A is an HTTP function that can be called synchronously through HTTP; function B is an event function that can be asynchronously called by an HTTP function through the SDK. However, the disadvantages of this scheme are also obvious:
• High cost: Each asynchronous call requires two triggers.
• Unable to realize full hosting of flow control: The first layer function is called synchronously. In the face of sudden traffic flow control, customers need to make self-adaptation, so they cannot enjoy full hosting of flow control for asynchronous calls.
• Increase customer development and maintenance costs: Two functions need to be developed and maintained to use asynchronous functions.

New: HTTP triggers support asynchronous calls

Function Compute is currently online and supports the function of HTTP triggers for asynchronous calls. To use this feature, clients need to prepare an HTTP function and an HTTP trigger.
Customers can create HTTP functions and triggers through the Function Compute console, SDK, and Serverless Devs tools. HTTP trigger clients can configure it themselves. If you don't configure it, when you create an HTTP function, Function Compute will automatically create a default trigger for you.
HTTP triggers need to select the triggering method through the request header X-Fc-Invocation-Type. The default is synchronous triggering. If asynchronous triggering is required, you can add {"X-Fc-Invocation-Type": "Async" to the request header "} to achieve asynchronous triggering.

HTTP asynchronous call test

1) Test via console
Test at the function code, and perform the asynchronous call test by checking the asynchronous call.
On the test function side, you can implement asynchronous call testing by checking "I want to call in an asynchronous way".
2) Test via cURL
curl -v -H "X-Fc-Invocation-Type: Async" https://http-***$path
View the test results:
After the trigger is completed, the result of the Function Compute receiving request will be returned immediately. Among them, the status code 202 indicates that the request was successful, and the rest indicate that there was an error in the request. The request ID is also returned in the request header, and you can track the execution status of the asynchronous request through the returned request ID. To better observe the request execution status and increase the control over function execution, you can enable asynchronous tasks [1].
You can query the execution result log in the console through the request ID:
For requests to activate asynchronous tasks, status monitoring and query can be performed through the asynchronous task list:
For the callback of asynchronous execution results, please refer to the function callback document [2].

Best Practices

This section uses HTTP-triggered video transcoding as an example to introduce how to implement HTTP-triggered asynchronous tasks by combining Serverless Devs and the console.
• Install Serverless Devs[3]
• Configure Serverless Devs[4]
• Create an OSS storage space [5] to facilitate access and content changes to the OSS storage space in subsequent test cases.
• Create a RAM role and grant the OSSFullAccess permission policy. You can also use the AliyunFCDefaultRolePolicy policy directly. See Creating RAM Roles [6] for details on permission policies.
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. Initiate asynchronous calls through HTTP triggers
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-***
4. Log in to the console to query the task status and manage and control the task
5. Function callback
You can add processing logic to dest-fail or dest-succ as required, as a callback function for the execution result of the transcoding function, to perceive and obtain the execution result of the function.
Function callback related documents:


HTTP triggering supports asynchronous calls, which enables users to build WEB services and use Function Compute as a fully managed platform to get through the last mile, so that users of HTTP triggers can also experience the convenient out-of-the-box experience of asynchronous calls.
Reference link:
[1] Asynchronous tasks:
[2] Result callback:
[3] Install Serverless Devs:
[4] Configure Serverless Devs:
[5] Create OSS storage space
[6] Create RAM role
Click here to learn more about FC!

Related Articles

Explore More Special Offers

  1. Short Message Service(SMS) & Mail Service

    50,000 email package starts as low as USD 1.99, 120 short messages start at only USD 1.00