Different applications may have different requirements on the type and scalability of compute resources. Function Compute supports two billing methods: subscription and pay-as-you-go. You can choose an appropriate billing method based on the scenario to save costs. Subscription means that you must pay for the resources before you can use them. In Function Compute, you can use reserved instances to offset the resource costs. The benefit of subscription is cost-effectiveness. In comparison to pay-as-you-go, it saves 70% of the costs. Subscription also has its own limits. In most cases, the number of resources that you purchase is determined based on the peak load. It is difficult to maintain a high utilization of the purchased resources during off-peak hours. Pay-as-you-go means that you only pay for the resources that you have used. Pay-as-you-go instances in Function Compute are charged based on the amount of time that it takes the instances to process requests, which is accurate to milliseconds. The pay-as-you-go instances are not charged when they are idle. Therefore, the utilization of pay-as-you-go instances is considered 100%. The benefit of pay-as-you-go is high resource utilization. However, pay-as-you-go instances are charged higher than reserved instances. The elastic scaling feature of Function Compute allows you to use reserved and pay-as-you-go instances simultaneously to gain benefits from both instance types.
Your reserved instances are shared by all the functions in the same region under your account. Function Compute sums up the amount of resources used by all the functions per second and uses reserved instances to offset the costs. For more information about subscription and pay-as-you-go, see Pricing.
Before you optimize the costs, you must analyze the types and proportions of the costs that your workloads incur. Fine-grained resource usage tracking can help you optimize costs more efficiently. Function Compute allows you to query resource usage information on a function, service, or user account basis. You can also classify services or functions by using tags. For example, you can use tags to classify and track functions by environment (test or production environment), application, or owner.
As described in the preceding sections, Function Compute provides multiple features for you to analyze and optimize your costs. Based on these features, a variety of approaches can be applied to improve resource utilization and reduce costs.
It is important to choose an appropriate billing method based on the way how your applications use the resources. For example, some online applications in a production environment are sensitive to the response latency. You must reserve a certain number of resources to guarantee a low response latency. In this case, you can use reserved instances, which can help you reduce costs. Tasks running test cases or running at a scheduled time require high throughput. Additional resources may be required to handle unexpected loads when the end date takes priority. In this case, you can use pay-as-you-go instances to balance resource utilization and costs. As shown in the following figure, you can analyze the usage of both reserved and pay-as-you-go resources from the Function Compute console. The resource usage information can be used as a reference for resource sizing, helping you allocate reserved and pay-as-you-go resources properly.
Function Compute supports fine-grained and multi-dimensional resource usage monitoring. You can check the usage of reserved and pay-as-you-go resources for each function, and release reserved resources that are idle to save costs. In addition, you can use tags to filter functions when you query resource usage information. For example, you can list all the functions that are used for testing purposes and then check whether they have idle resources. As shown in the following figure, the reserved instances allocated to the function have remained idle for a long period of time. In this case, you need to release the reserved instances to save costs.
To handle a large number of pending requests where the end date is flexible, you can use asynchronous calls to process the requests, and throttle the maximum number of instances launched concurrently. This can help you deal with fluctuations with ease. As shown in the following figure, the resource demand barely fluctuates. You can use reserved instances to offset the overheads, which is cost-effective. Function Compute will automatically cache requests in queues and process requests at a rate that corresponds to the specified maximum resource limit. It also uses load balancing and resource isolation to guarantee the overall performance of function invocation. You only need to specify the maximum number of instances to be launched concurrently.
For function invocation tasks such as regression tests, their end dates are flexible. You may want to run these tasks during off-peak hours, such as a time period between 21:00 of the current day and 9:00 of the next day. Function Compute provides triggers for you to run your tasks at a scheduled time. In this way, you are able to use reserved instances to run these tasks with low costs.
The variety of billing methods, competitive pricing, and fine-grained resource usage monitoring of Function Compute can help you manage resources for serverless applications with ease and save resource costs.