×
Community Blog Function Compute HTTP Triggers Support Async to Free Manual Operations of Building Web Services

Function Compute HTTP Triggers Support Async to Free Manual Operations of Building Web Services

This article discusses Function Compute HTTP triggers and issues with long execution and traffic.

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.

How Difficult Is It for Users When Asynchronous Invocations Are Not Supported?

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.

  • The client needs to maintain a long connection. Network fluctuations and autonomous disconnection due to impatience will increase the probability of errors.

Scenario: Video website transcodes videos during uploading. Connection interruption may occur due to long waiting times when refreshing the page, resulting in transcoding failure.

  • Maintaining a long connection increases the machine overhead of the client and reduces the utilization of client machine resources.

2. Cannot process and receive sudden traffic smoothly

  • For scenarios with concurrency limits, the sudden traffic increase of customers will be throttled in the scenario of synchronous invocations, thus causing certain request failures when customers do not handle errors.

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

1

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:

  • High Cost: Each asynchronous invocation requires two triggers.
  • Unable to Implement Full-Managed Traffic Control: The first-layer function is a synchronous invocation. In the face of sudden traffic increase, customers need to be adaptative and cannot enjoy the full management.
  • Increase Customer Development and Maintenance Costs: Development and maintenance functions are needed to enable asynchronous features.

New Feature: HTTP Triggers Support Asynchronous Invocations

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.

HTTP Asynchronous Invocation Test

1) Test in the Console

Test in the function code and select asynchronous invocation:

2

You can implement an asynchronous invocation test on the test function side by selecting, "I want to invoke asynchronously."8

3

2) Test by cURL

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:

4

You can use the asynchronous task list for requests that enable asynchronous tasks to monitor and query the status:

5

Please see the function callback documentation [2] for the callback of asynchronous execution results.

Best Practice

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.

Prerequisites

  • Install Serverless Devs [3]
  • Configure Serverless Devs [4]
  • Create an OSS bucket [5] to access and change the content of the OSS bucket in subsequent test cases
  • Create a RAM role and grant it the OSSFullAccess policy. You can also directly use the AliyunFCDefaultRolePolicy. Please see Create a RAM Role [6] for more information.

Steps

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:

6

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

7

Summary

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.

References

[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

0 0 0
Share on

Alibaba Cloud Serverless

99 posts | 7 followers

You may also like

Comments

Alibaba Cloud Serverless

99 posts | 7 followers

Related Products