If you use Alibaba Cloud Function Compute (FC) and need to use it as a backend service for an Application Load Balancer (ALB) to process requests, you can add a Function Compute server group to your ALB instance. This configuration enables ALB to forward requests to Function Compute and invoke your function. After the function executes, Function Compute returns the result to the requester.
Overview
Alibaba Cloud Function Compute is an event-driven, fully managed compute service. With Function Compute, you do not need to manage infrastructure such as servers. All you need to do is write and upload your code. Function Compute prepares the required computing resources, runs your code in an elastic and reliable manner, and provides features such as log querying, performance monitoring, and alerting. By using Function Compute, you can quickly build any type of application and service, and pay only for the resources that you use.
When ALB receives a request, it forwards the request to Function Compute, which triggers a function invocation. After the function executes, Function Compute returns the result to the requester.
ALB supports adding Function Compute 3.0 and 2.0 as backend services.
Before using Function Compute, you must activate the service. 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 (ALB) and Function Compute (FC) communicate securely over the Alibaba Cloud internal network.
Key features
Serverless architecture support: Use Function Compute as a backend service for ALB to easily build serverless applications and reduce operational costs.
Auto scaling: Function Compute automatically scales computing resources up or down based on traffic, ensuring sufficient computing capacity during peak hours and saving resources during off-peak periods.
High availability and fault tolerance: ALB and Function Compute work together to provide high availability and disaster recovery, ensuring your application runs stably.
Use cases
Microservices: Use the advanced routing capabilities of ALB to distribute requests to different microservice functions. Function Compute scales dynamically to handle high-concurrency requests, improving system elasticity and reliability.
Real-time data processing: ALB distributes data processing requests to the appropriate functions. Function Compute then processes the data in real time with only a few lines of code and simple configuration.
Event-driven architecture: ALB 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, enabling dynamic, event-driven processing.
Image and video processing: ALB receives image or video upload requests and distributes them to the appropriate processing functions. Function Compute provides elastic compute resources that scale automatically based on the task, ensuring efficient task completion.
Limitations
You can use Function Compute as a backend service only in specific regions. For a list of supported regions, see Regions where ALB supports Function Compute as a backend service.
The ALB instance and the function must be in the same region.
A Function Compute server group supports only one function.
For Function Compute 2.0, if the Handler type is set to Event handler, you must configure an HTTP trigger to associate the function with an ALB instance.
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 and user base grow, the company requires 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 combines Alibaba Cloud's Function Compute service with ALB to efficiently handle these tasks and significantly improve the user experience.
Prerequisites
You have created an internet-facing ALB instance.
You have registered a custom domain name and completed its ICP filing.
Procedure
Step 1: Create a function
The steps in this topic are performed in the Function Compute 3.0 console. If you need to use Function Compute 2.0, click Back to Function Compute 2.0 in the upper-right corner to return to the Function Compute 2.0 console and create a function.
Log on to the Function Compute console. In the left-side navigation pane, choose Function. On the Function page, select a target region, and then click Create Function.
On the Create Function page, select a function type, configure the function code, and then click Create.
This topic provides an example of an Event Function where the Code uses the default Use Sample Code. You can keep the default values for other event function parameters or modify them as needed.


On the function details page, on the Code tab, click Test Function.
After the function runs successfully, you can view the result in the Response area. 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, choose Server Groups. Select the target region from the top navigation bar, 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 for the ALB instance, Function Compute counts the probes as requests, which incurs charges. For more information, see Billing.

In the The server group is created dialog box, 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 function you created is selected for Function Name, and Specified Version is set to LATEST. If you need to configure the function by using its ARN, select ARN for Configuration 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 target instance.
Click the Listener tab, and then click Create Listener.
On the Configure Listener page, 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 parameters or modify them as needed. For more information, see Add an HTTP listener.

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

On the Configuration Review page, confirm the settings and click Submit.
Step 4: Configure domain name resolution
For production environments, we recommend using a custom domain name and creating a CNAME record to point it to the domain name of the ALB instance.
In the left navigation bar, select , and on the Instances page, copy the DNS name of the created ALB instance.
Follow these steps to add a CNAME record:
Log on to the Alibaba Cloud DNS console. Find your custom 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 Alibaba Cloud DNS before you can configure its DNS settings.
On the DNS settings page, click Add Record. Configure the CNAME record and then click OK.
For this example, set Record Type to CNAME and Record Value to the DNS name of your ALB instance. You can keep the default values for other parameters or modify them as needed. For more information, see Add a DNS record.

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 <custom domain name> command to test the connectivity between ALB and Function Compute.
If you receive a response message as shown in the following figure, it indicates that ALB can forward requests to Function Compute and invoke the function.

FAQ
Why is QPS low with Function Compute backends?
A single ALB instance can handle up to 1 million QPS. However, when you use Function Compute as the backend service, the overall QPS may not meet expectations for the following reasons:
QPS of a single Function Compute instance: A single instance can process only a certain number of requests at a time. If the request volume exceeds the maximum QPS of the current instances, requests may be queued or throttled.
Function Compute instance limit: The instance limit defines the maximum number of instances that can run concurrently. If the number of available instances is insufficient, this limits the overall QPS, even if the QPS of a single instance is high.
To learn how to estimate the maximum number of instances you need, see Relationship between the instance limit and service QPS.
Based on your business needs and the results from you can fine-tune parameters such as instance concurrency and instance type and specifications. If necessary, you can also request an instance quota increase to achieve optimal QPS performance.
References
Console operations
API reference
CreateServerGroup: Create a Function Compute server group by setting the
ServerGroupTypeparameter toFc.AddServersToServerGroup: Add a Function Compute backend server by setting the
ServerTypeparameter toFc.RemoveServersFromServerGroup: Remove a backend server from a server group by setting the
ServerTypeparameter toFcand theServerIdparameter to the ARN of the function.DeleteServerGroup: Delete a specified server group.