This topic describes how to specify a function from Function Compute as a backend server for Application Load Balancer (ALB). The function is used to process requests. You can create a server group of the Function Compute type for your ALB instance. After you add a function to an ALB server group, requests to ALB are forwarded to Function Compute and invoke the function. After Function Compute runs the function, the results are returned to the requesters.
Solution overview
Alibaba Cloud Function Compute is a fully managed, event-driven computing service. With Function Compute, you don't need to manage infrastructure such as servers. All you need to do is write and upload your code to Function Compute. Then, Function Compute prepares the required computing resources and runs your code with scalable resources in a reliable way. In addition, it provides logging, performance monitoring, and alerting features. Using Function Compute, you can quickly build any type of application and service, and only pay for the resources that you actually consume.
ALB supports adding Function Compute functions as backend servers. When ALB receives requests, it forwards them to Function Compute. The functions associated with ALB as backend servers are invoked, and when function execution is complete, the execution results are returned as responses to the requesters.
ALB supports specifying Function Compute 3.0 or Function Compute 2.0 functions as backend servers. However, Function Compute 3.0 is not available by default. To use it, contact your account manager to submit an application.
To use Function Compute for ALB, you must activate the Function Compute service first. If your account is signed up one day after August 27, 2024 and has completed real-name authentication, you can skip the activation process for Function Compute.
ALB and Function Compute communicate over the secure internal network of Alibaba Cloud.
Key features
Serverless architecture: Adding a Function Compute function as a backend service to ALB allows you to easily build serverless applications and reduce O&M costs.
Auto scaling: Function Compute automatically scales computing resources up or down based on traffic, ensuring sufficient computing power during traffic spikes and saving resources during traffic troughs.
High availability and fault tolerance: ALB and Function Compute together provide high availability and disaster recovery capabilities, ensuring stable application operation.
Use scenarios
Microservices architecture: Through the advanced forwarding capabilities of ALB, requests are distributed to different microservice functions. Function Compute auto scales its resources to handle high concurrency requests, improving system scalability and reliability.
Real-time data processing: With ALB distributing requests to required functions, you only need to write a few lines of code and perform basic configuration. Then, Function Compute can process your business data in real time.
Event-driven requests: ALB checks for and forwards event-triggered requests to associated functions. Function Compute processes the requests and stores the processing results in a database or sends them to other services, enabling an event-driven architecture.
Image and video processing: ALB checks for and forwards image and video upload requests to processing functions. Function Compute auto scales computing resources based on task sizes, ensuring efficient completion of image and video processing tasks.
Limitations
For regions where Function Compute is supported as backend servers for ALB, see Regions and zones.
The ALB instance and the function you want to use as a backend server must be deployed in the same region.
For an ALB server group of the Function Compute type, only one function can be added as the backend server.
For Function Compute 2.0, if you set the Handler Type of a function to Event Handler, and use the function as an ALB backend server, an HTTP trigger must be configured for the function.
Example scenario
An e-commerce company has deployed ALB in an Alibaba Cloud region to handle user requests accessing its platform. As the business grows and the number of users increases, the company needs a scalable architecture to efficiently complete content generation, user behavior analysis, and personalized recommendation tasks.
To meet this need, the company combines Alibaba Cloud's Function Compute with ALB to improve its user experience.
Prerequisites
An Internet-facing ALB instance is created.
A custom domain name is registered and an Internet content provider (ICP) number is obtained for the domain name.
Procedure
Step 1: Create a function
In this example, Function Compute 3.0 is used. If you want to use Function Compute 2.0, on the homepage in the Function Compute console, click Back to Function Compute 2.0 in the upper right corner and refer to this guide: Quickly create a function.
Log on to the Function Compute console. In the left-side navigation pane, choose Functions. On the Functions page, select a region in the top navigation bar, then click Create Function.
On the Create Function page, select a function type, complete the function code configuration, then click Create.
In this example, the Event Function type is selected and the default Code configuration of the Use Sample Code method is used. You can keep the default values for other Event Function parameters or set them as needed.
On the function details page, on the Code tab, click Test Function.
When the Successful Execution message is displayed, you can view the function execution result in the Response section. In this example, the response is
hello world
.
Step 2: Create a server group of the Function Compute type
Go to the ALB console, in the left-side navigation pane, choose Server Groups. In the top navigation bar, select the same region as the created function, then click Create Server Group.
In the Create Server Group dialog box, select Function Compute for Server Group Type, then click Create.
ImportantIf you enable health checks for this server group, health check probes are counted as requests to Function Compute, and Function Compute charges for these requests.
In the message that is displayed, click Add Backend Server.
In the Add Backend Server panel, select the created function, then click OK.
In this topic, Configuration Method is set to Service, the created function is selected for Function Name, and Version is set to LATEST. If you need to configure by ARN, you need to obtain the Alibaba Cloud Resource Name (ARN) of the function.
Step 3: Create a listener
In the left-side navigation pane, choose
, then click the ID of the instance you want.Click the Listener tab, then click Create Listener.
On the Configure Listener wizard page, set the listener protocol and port, then click Next.
In this topic, the HTTP protocol and port 80 are selected. You can keep the default values for other HTTP listener parameters or set them as needed.
On the Select Server Group wizard page, select Function Compute from the Select Server Group drop-down list, select the created server group, then click Next.
On the Configuration Review wizard page, confirm the configuration, then click Submit.
Step 4: Configure domain name resolution
In real-world business scenarios, we recommend that you use a custom domain name and resolve it to the domain name of your ALB instance with a CNAME record.
In the left-side navigation pane, choose
. On the Instances page, copy the DNS name of your ALB instance.Add a CNAME record:
On the Authoritative DNS Resolution page in the Alibaba Cloud DNS console, find the custom domain name you want, and click DNS Settings in the Actions column.
NoteFor domain names not registered on Alibaba Cloud, you need to add the domain name to Alibaba Cloud DNS before you can configure domain name resolution.
On the resolution settings page, click Add DNS Record, configure the CNAME record, then click OK.
In this topic, 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 set them as needed.
Step 5: Test the load balancing effect
With the above steps finished, a connection is established between ALB and Function Compute. 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 the response message shown in the following figure, it indicates that ALB can forward requests to Function Compute and invoke the function now.
FAQ
Why does the queries per second (QPS) of my ALB instance fail to reach the committed level when a Function Compute function is used as a backend server for ALB?
An ALB instance can handle up to 1 million QPS at optimal performance. However, when the backend service of an ALB instance is Function Compute, the QPS of the ALB instance may not reach the committed level for the following reasons:
QPS limit of Function Compute: The number of concurrent requests a Function Compute instance can process is limited. When the business request volume exceeds the maximum QPS that the current instances can handle, requests may be queued or throttled.
Instance number limit of Function Compute: The number of Function Compute instances running in parallel is limited. If the Function Compute instances are insufficient for your business traffic, even if the QPS of a single instance is high, the overall QPS is insufficient.
For information about estimating the number of instances required for your business, see Considering that the upper limit of instances for each user in a region is 100, can a service process more than 100 requests per second?
Solution: Refer to the results of function performance profiling, properly configure instance concurrency, instance type, and other relevant parameters, and increase the Function Compute quotas to achieve the optimal QPS.
References
Console
For more ALB information:
For more Function Compute information:
API
CreateServerGroup: Creates a server group of the Function Compute type by setting the
ServerGroupType
parameter toFc
.AddServersToServerGroup: Adds a Function Compute function as a backend server by setting the
ServerType
parameter toFc
.RemoveServersFromServerGroup: Removes a backend server from a server group by setting the
ServerType
parameter toFc
and theServerId
parameter to the ARN of the Function Compute function.DeleteServerGroup: Deletes a specified server group.