Reduce costs with Auto Scaling
This tutorial shows you how to use Auto Scaling with a combination of pay-as-you-go and spot instances to handle periodic traffic fluctuations and reduce costs.
Use case
Consider an online education platform that experiences peak traffic from 17:00 to 22:00 every day. During other periods, traffic is low. To handle the peak load, operations teams must maintain enough compute resources for peak hours. These resources then sit idle during off-peak times, leading to waste. If traffic unexpectedly surges, engineers still need to manually create more ECS instances.
This solution is also suitable for applications with the following characteristics:
-
The application is deployed on a multi-server cluster.
-
The application is highly fault-tolerant and can withstand interruptions. For example, a test workload that uses spot instances.
-
The application has predictable traffic peaks and troughs. For example, demand is high from 17:00 to 22:00 every day, while resources are idle during other periods.
Solution
Auto Scaling lets you use a combination of pay-as-you-go and spot instances to manage periodic traffic fluctuations at a lower cost.
You can adopt the following approach:
-
For off-peak periods, use subscription ECS instances to handle baseline traffic.
-
For peak and off-peak periods, you can use scheduled tasks to add and release ECS instances. Details:
-
During peak hours, a scaling group uses a scheduled task to automatically add the required ECS instances. You can specify multiple instance types and combine pay-as-you-go instances and spot instances to further reduce costs. The scaling group sorts instance types by vCPU unit price in ascending order, prioritizing those with the lowest prices.
-
During off-peak hours, the scaling group uses a scheduled task to automatically release the unneeded ECS instances.
-
Benefits
Using Auto Scaling to reduce costs provides the following benefits:
-
Zero standby costs
Auto Scaling automatically creates and releases ECS instances on demand, which eliminates the need for standby servers. You only need to maintain compute resources for your baseline traffic during off-peak periods.
-
Zero operational overhead
You only need to configure scaling policies in advance. When the load increases, Auto Scaling automatically creates ECS instances and adds them to the IP address whitelist of an ApsaraDB RDS instance and the backend server group of an associated load balancer instance, such as a Classic Load Balancer (CLB) instance. This entire process is automatic, requiring no manual intervention.
-
High cost-effectiveness
Auto Scaling supports a combination of pay-as-you-go instances and spot instances. Spot instances can be purchased at a discount of up to 90% compared to pay-as-you-go instances. If the spot instance inventory is insufficient, pay-as-you-go instances are provisioned to ensure capacity delivery. The Cost Optimization Policy also supports spot instance compensation. Five minutes before a spot instance is reclaimed, Auto Scaling automatically tries to create a new, lower-priced spot instance, ensuring high cost-effectiveness.
Procedure
To reduce costs for your application modules, evaluate your business architecture and perform the following steps:
Before you begin, create a custom image from the ECS instance that runs your application. For more information, see Create a custom image from an instance.
Step 1: Create subscription ECS instances
Create the required number of subscription ECS instances to handle baseline traffic for your application modules during off-peak periods.
Log in to the ECS console.
In the left-side navigation pane, choose .
In the upper-left corner of the page, select a region and resource group.
-
Find your application's custom image and click Create Instance in the Actions column.
-
Configure instance information and create the instance.
-
Set Billing Method to Subscription.
-
The Region and Image information is automatically populated.
Configure other parameters as required. For more information, see Create an instance by using the wizard.
-
Step 2: Create and enable a scaling group
Create a scaling group for the application modules you are optimizing. In the scaling configuration, select your application's custom image to ensure new instances are configured correctly.
Sign in to the Auto Scaling console.
In the left-side navigation pane, click Scaling Groups.
In the top navigation bar, select a region.
-
Click Create Scaling Group, configure the parameters, and then create a scaling group.
-
Set Scaling Group Name to a custom name, such as
testname. -
Set Instance Configuration Source to Create from Scratch.
-
Set Minimum Number of Instances to 0.
-
Set Maximum Number of Instances to 10.
-
The Default cooldown (seconds) is 300 by default.
-
Set Network Type to VPC.
-
Set Scaling Policy to Cost Optimization Policy.
-
Set Minimum Pay-as-you-go Instances to 0.
-
Set Percentage of Pay-as-you-go Instances to 30.
-
Set Lowest Cost Instance Types to 3.
-
Enable Enable Supplemental Spot Instances.
-
Use Pay-as-you-go Instances to Supplement Spot Capacity is enabled by default.
-
-
Set Instance Reclaim Mode to Release.
-
Select the created VPC and the corresponding vSwitch.
-
Associate the Classic Load Balancer (CLB), Application Load Balancer (ALB), and Network Load Balancer (NLB) server groups, and ApsaraDB RDS instances that your application module uses.
Configure other parameters as required. For more information, see Create a scaling group of ECS instances.
-
-
Find the created scaling group and click View Details in the Actions column.
At the top of the page, click the Scaling Configurations tab.
-
Click Create Scaling Configuration, configure the parameters, and then create a scaling configuration.
-
Set Billing Method to Spot Instance.
-
For the instance configuration, select three or more instance types.
-
Select Image: Select your custom image.
Configure other parameters as required. For more information, see Create a scaling configuration for ECS instances.
-
-
Enable the scaling configuration and the scaling group.
Step 3: Configure auto scaling
Add the subscription ECS instances to the scaling group and create scheduled tasks to automatically and smoothly scale your resources based on traffic peaks and troughs. This approach maximizes cost savings by using spot instances.
-
Go to the Instances tab and add the created subscription ECS instances to the scaling group.
-
Set the subscription ECS instances to the protected state to ensure your application operates normally during off-peak periods.
-
Go to the Scaling Groups page and modify the minimum and maximum numbers of instances in the scaling group based on your business requirements.
For more information about how to modify the maximum and minimum numbers of instances in a scaling group, see Manage scaling groups.
-
On the scaling group details page, choose and create two simple scaling rules.
The key parameters are described as follows:
-
Set Rule Type to Simple Scaling Rule.
-
Configure the Operation parameter as follows:
-
Simple Scaling Rule 1: When the scaling group needs to scale out, add 5 instances.
-
Simple Scaling Rule 2: Removes 5 instances when the scaling group needs to scale in.
-
Configure other parameters as required. For more information, see Manage scaling rules.
-
-
On the scaling group details page, choose and create two scheduled tasks.
-
Configure the Executed At parameter as follows:
-
Scheduled task 1: To scale out before the business peak (for example, 17:00), set the time to 16:59 on November 20, 2021.
-
Scheduled task 2: To scale in before the business trough (for example, 22:00), set the time to 21:59 on November 20, 2021.
-
-
Scaling Group: Select the scaling group to which the subscription ECS instances were added.
-
Scaling Method: Select Select Existing Scaling Rule.
-
Set Simple Scaling Rule as follows:
-
Scheduled task 1: To perform a scale-out before the business peak (for example, 17:00), select simple scaling rule 1.
-
Scheduled task 2: To perform a scale-in before the business trough (for example, 22:00), select simple scaling rule 2.
-
Configure other parameters as required. For more information, see Manage scheduled tasks.
-
Verify the result
On the Scaling Activities page, you can view the details of scaling activities and verify that the instances in the scaling group scaled as expected. For more information, see View the details of a scaling activity.
-
The subscription ECS instances are in the protected state and are used to serve workloads during off-peak periods. An ECS instance in the protected state is not removed from its scaling group, and its load balancer weight remains unaffected.
-
During peak hours, the scaling group uses a scheduled task to automatically create the required number of ECS instances. Because the Cost Optimization Policy is used and spot instance compensation is enabled, new ECS instances are purchased at the lowest possible prices, maximizing cost-effectiveness.
-
During off-peak hours, the scaling group uses a scheduled task to automatically release surplus ECS instances.