Add Function Compute as a backend service
To use an Alibaba Cloud Function Compute function as a backend service, add a Function Compute-type server group to your Application Load Balancer (ALB) instance. The ALB instance forwards requests to Function Compute, which invokes a function and returns the execution result to the requester.
Overview
Alibaba Cloud Function Compute is a fully managed, event-driven compute service. With Function Compute, you can write and upload code without managing infrastructure such as servers. Function Compute provisions computing resources and runs your code elastically and reliably. It also provides features such as log queries, performance monitoring, and alerts. By using Function Compute, you can quickly build any type of application or service and pay only for the resources that you actually consume.
Application Load Balancer (ALB) supports adding Function Compute as a backend service. When an ALB instance receives a request, it forwards the request to Function Compute to trigger a function invocation. Function Compute then returns the execution result to the requester.
ALB supports adding both Function Compute 3.0 and 2.0 as a backend service.
You must activate Function Compute before use. If you registered your Alibaba Cloud account and completed real-name verification after August 27, 2024, you can use Function Compute directly without activation.
Application Load Balancer and Function Compute communicate securely over the Alibaba Cloud internal network.
Key features
Serverless architecture: Add Function Compute as a backend service to your ALB instance to build serverless applications and reduce O&M costs.
Auto scaling: Function Compute automatically scales computing resources up or down based on traffic, which ensures sufficient capacity during peak hours and saves resources during off-peak periods.
High availability and fault tolerance: Application Load Balancer and Function Compute work together to provide high availability and disaster recovery, ensuring application stability.
Use cases
Microservices: Use the advanced routing capabilities of ALB to distribute requests to different microservice functions. Function Compute can dynamically scale to handle high-concurrency requests, improving system elasticity and reliability.
Real-time data processing: Use ALB to distribute data processing requests to the appropriate functions. Function Compute can process data in real time with just a few lines of code and simple configurations.
Event-driven scenarios: An ALB instance receives event-triggered requests and forwards them to the corresponding functions. Function Compute processes the events and stores the results in a database or sends them to other services, which enables dynamic, event-driven processing.
Image and video processing: An ALB instance receives image or video upload requests and distributes them to the appropriate processing functions. Function Compute provides elastic computing resources that automatically scale with the task load, ensuring efficient task completion.
Limits
Regions that support Function Compute as a backend service for ALB.
The ALB instance and the function must be in the same region.
You can add only one function to a Function Compute server group.
If the Handler Type of a Function Compute 2.0 function is set to Event Handler, you must configure an HTTP trigger to associate it with ALB.
Example scenario
An e-commerce company deploys an ALB instance in an Alibaba Cloud region to handle a large volume of user requests on its platform. As the business grows and the number of users increases, the company needs a flexible and efficient way to handle tasks such as dynamic content generation, user behavior analysis, and personalized recommendations.
To meet this demand, the company integrates its ALB instance with Function Compute. This solution allows the company to efficiently process these tasks and improve the user experience.
Prerequisites
An internet-facing ALB instance is created.
A domain name is registered and an ICP filing is completed for it.
Procedure
Step 1: Create a function
This topic uses the Function Compute 3.0 console. If you want to use Function Compute 2.0, click Back to Function Compute 2.0 in the upper-right corner of the console and then create a function.
Log on to the Function Compute console. In the left-side navigation pane, click Functions. On the Functions page, select a region and then click Create Function.
On the Create Function page, select a function type, configure the function code, and then click Create.
In this example, Event Function is selected as the function type, and Use Sample Code is selected for Code. You can keep the default values for other Event Function parameters or modify them.


On the function details page, go to the Code tab and click Test Function.
After the function executes successfully, you can view the result in the Response section. In this example, the result is
hello world.
Step 2: Create a Function Compute server group
Log on to the Application Load Balancer console. In the left-side navigation pane, click Server Groups. In the top navigation bar, select a region and then click Create Server Group.
In the Create Server Group dialog box, select Function Compute for Server Group Type and then click Create.
ImportantIf you enable health checks, health check probes are counted as requests to Function Compute and incur fees.

In the The server group is created message that appears, click Add Backend Server.
In the Add Backend Server panel, select the function you created and then click OK.
In this example, Configuration Method is set to Service, the created function is selected for Function Name, and Specified Version is set to LATEST. If you want to use the ARN method, you must first obtain the function ARN.

Step 3: Configure a listener
In the left-side navigation pane, choose and then click the ID of the ALB instance you want to manage.
Click the Listener tab and then click Create Listener.
In the Configure Listener step, configure the listener protocol and port, and then click Next.
This example uses the HTTP protocol and port 80. You can keep the default values for other HTTP listener parameters or modify them.

In the Server Group step, select your Function Compute server group from the Server Group drop-down list, and then click Next.

In the Configuration Review step, confirm the settings and click Submit.
Step 4: Configure domain name resolution
For production environments, we recommend that you use a custom domain name and create a CNAME record to map it to the domain name of your ALB instance.
In the left-side navigation pane, choose . On the Instances page, copy the DNS name of the ALB instance.
To add a CNAME record:
Log on to the Alibaba Cloud DNS console. Find your domain name and click Settings in the Actions column.
NoteIf your domain name was not registered with Alibaba Cloud, you must first add it to the Alibaba Cloud DNS console.
On the Configure page, click Add Record, configure the CNAME record, and then click OK.
In this example, Record Type is set to CNAME and Record Value is set to the DNS name of the ALB instance. You can keep the default values for other DNS record parameters or modify them.

Step 5: Test connectivity
After you complete the preceding operations, a connection is established between ALB and Function Compute. You can open a command-line window and run the curl <your custom domain name> command to test the connectivity between ALB and Function Compute.
The response shown in the following figure indicates that the ALB instance can forward requests to Function Compute and invoke the function.

FAQ
Why is the QPS lower than expected when using Function Compute?
A single ALB instance can achieve a maximum performance of 1 million QPS. However, when the backend service is Function Compute, the QPS of your Function Compute service may fall short for the following reasons:
QPS of a single function instance: A single function instance can process only a limited number of requests at a time. If the number of requests exceeds the maximum QPS that the current instances can handle, requests may be queued or throttled.
Function Compute instance limit: The instance limit specifies the maximum number of instances that can run concurrently. If the number of instances is insufficient, the overall QPS is limited even if each instance has a high QPS.
To learn how to estimate the required number of instances, see Relationship between instance limits and service QPS.
Based on your business requirements, configure parameters such as instance concurrency and instance types and specifications. If necessary, you can also increase the instance limit to achieve the optimal QPS.
References
Console documentation
API reference
CreateServerGroup: Creates a Function Compute server group by setting the
ServerGroupTypeparameter toFc.AddServersToServerGroup: Adds a Function Compute backend server by setting the
ServerTypeparameter toFc.RemoveServersFromServerGroup: Removes a backend server from a server group by setting the
ServerTypeparameter toFcand theServerIdparameter to the ARN of the function.DeleteServerGroup: Deletes a specified server group.


