As your business grows, the load on your monolithic web service increases, which can reduce system stability. To address this, you can evolve your monolithic architecture into an elastic and highly available one by using a scaling group to scale out servers and distribute the load. This approach improves system stability and response speed.
Why use an elastic and highly available architecture?
Problems with a monolithic architectureIn a typical monolithic architecture, all resources are deployed on a single ECS instance. Users access the service on this single server directly through a domain name or IP address. This architecture has two main problems:
| Features of a highly available architectureIn an elastic and highly available architecture, a load balancer routes user requests to business services running on instances in the cluster. This architecture has the following key features:
|
How to evolve to a highly available architecture
As shown in the figure, evolving from the monolithic architecture on the left to an elastic and highly available architecture involves two phases.
Phase 1: Separate data storage from business logic
An elastic and highly available architecture replicates entire ECS instances. Therefore, you must separate the data storage and business logic of your ECS instances (and the services within them). This is also known as making them stateless, and it prevents data consistency issues when you scale out by adding more instances.
For example, when replicating an ECS instance, you should ensure that the database is not copied, thereby preventing the cluster from using multiple different data sources. By separating data storage from the ECS instance, you ensure all instances share the same data source, maintaining data consistency.
If you encounter any of the following common situations, you need to assess the potential impact on your business and adjust your architecture accordingly.
Phase 2: Evolve to a highly available architecture
After separating the data storage and business logic on the ECS instance, you can scale out by creating replicas of the instance and its business service.
Migrate the service cluster to a scaling group to leverage its elasticity for rapid replication. Use a load balancer as the access point for the service cluster to distribute the load, ensuring system stability and efficiency.
Quick migration to a highly available architecture
Solution overview
If you have already separated your data storage from your business logic (made it stateless), you can follow these steps to quickly migrate your service from a monolithic architecture to an elastic and highly available architecture using a scaling group.
Deploy a demo website (prepare an instance). This solution simulates a stateless demo web service instance to demonstrate the migration process. You can also use an existing instance.
Build an instance image that contains the business service. This image will be used to launch instances in your cluster, automatically starting the business service upon startup.
Create a scaling group to manage the service cluster. You will use this scaling group to quickly replicate instances (scale out).
Set up a unified access point for the cluster. Associate a load balancer to create a unified access point.
Scale out instances (verification). Quickly replicate instances and access the load balancer to verify that the cluster is working correctly.
1. Deploy a demo website
First, you need an application instance that represents your production environment. This instance will be used for replication and automated deployment.
This tutorial provides a demo website service that you can use to experience the migration process. Click Deploy Demo Service to set up the demo service.
This demo website service includes a web service package, its runtime environment, and a startup script. The instance has already been made stateless by separating its data storage from its business logic.
If your business service instance is already stateless and has the required components (software package, environment, startup script), you can use your actual service instance instead for the following migration steps.
2. Build an instance image
To ensure that new instances have the business service and startup script, create a custom image from the prepared instance. Subsequent instances will use this as their base image, so your business service will start automatically when the instances launch.
Go to the ECS console and switch to the region where you prepared the instance in Step 1.
Find the ECS instance prepared in Step 1. In the Actions column on the right, choose .
In the Create Custom Image dialog box, enter an image name for easy identification, and then click OK. Wait for the image creation to complete. You can check the progress by clicking in the left-side navigation pane of the ECS console.
3. Create a scaling group
Using a scaling group to manage your service cluster is the core of an elastic and highly available architecture. You can use a scaling group to quickly replicate instances. Follow these steps to create and enable a scaling group:
Go to the Scaling Groups page.
Log on to the Auto Scaling console.
If this is your first time using Auto Scaling, follow the on-screen instructions to grant the necessary permissions. For more information, see Service-linked roles.
In the left-side navigation pane, click Scaling Groups.
In the top navigation bar, select the region where Auto Scaling is activated.
Create a scaling group.
On the Scaling Groups page, click Create to open the Create page.
Complete the following configurations on the page. You can keep the default values for any settings not mentioned in the table.
Parameter
Description
Scaling Group Name
Enter a name as prompted. In this example, the name is ess-demo.
Type
Select ECS.
Instance Configuration Source
Select Select Existing Instance.
Select Existing Instance
Select the ECS instance prepared in Step 1.
Minimum Number of Instances
The minimum number of instances in the scaling group. If the number of instances falls below this value, an automatic scale-out event is triggered. In this example, the minimum is 0.
Maximum Number of Instances
The maximum number of instances in the scaling group. If the number of instances exceeds this value, an automatic scale-in event is triggered. In this example, the maximum is 10.
VPC and vSwitch
These settings are automatically populated based on the ECS instance you select.
ImportantWe recommend that you create and select vSwitches in multiple availability zones. This helps prevent scaling failures caused by insufficient instance inventory in a single availability zone. To create a vSwitch, see Create a vSwitch.
Select to enable this feature. After enabling it, you can automatically scale in or out by setting the expected number of instances.
Enter 0 to create an empty scaling group initially.
Select Instance Status Check and Load Balancer Health Check. When enabled, the system uses health check results to ensure that all instances in the scaling group and their business services are running correctly. If a service becomes unhealthy, it is promptly replaced with a new instance.
Click Create and wait for the scaling group to be created.
Modify the instance image in the scaling configuration.
When you create a scaling group by selecting an existing instance, a scaling configuration is created based on the instance's original image. The original image of the instance created in Step 1 does not include the application package deployed later. Therefore, you need to update the base image in the scaling configuration to the new image created in Step 2.
On the Scaling Groups page, find the scaling group you just created. In the Actions column, click View Details to go to the scaling group's details page.
On the tab, find the only scaling configuration and click Edit Image in the Actions column.
In the Edit Image dialog box, select User-Defined Image. Follow the prompts to select the image you created in Step 2. Click OK to complete the modification.
Enable the scaling group.
On the Scaling Groups page, click the Enable button in the upper-right corner to activate the scaling group.
4. Set up a unified access point
When scaling from one instance to many, you need a unified access point for the cluster. To do this, create and associate a load balancer with your scaling group. This allows user requests to be automatically distributed across the ECS instances in the cluster, balancing the load and maximizing resource utilization. This example uses an Application Load Balancer (ALB). Follow these steps:
4.1 Create a load balancer
Log on to the ALB console.
On the Instance page, click Create Application Load Balancer.
On the Application Load Balancer (Pay-As-You-Go) purchase page, follow the on-screen instructions to create the ALB.
This example uses the following configurations. You can keep the default values for any settings not mentioned.
Parameter
Description
Region
Select the same region as the instance from Step 1.
Network Type
Select Internet.
VPC
Select the VPC of the instance prepared in Step 1.
Zone
Select at least two. If a selected availability zone does not have a vSwitch, follow the on-screen instructions to create one. For instructions on how to create a vSwitch, see Create a vSwitch.
4.2 Create a server group
This server group will be associated with the scaling group. Instances created by the scaling group will be automatically added to this server group to provide services through the load balancer.
In the Application Load Balancer (ALB) console, select a region.
In the left-side navigation pane, click Server Group to go to the Server Groups page. Click Create Server Group and follow the on-screen instructions to create the server group.
This example uses the following configurations. You can keep the default values for any settings not mentioned.
Parameter
Description
Server Group Type
Select Server Type.
Server group name
Enter a name as prompted. This example uses ess-test-server-group.
VPC
Select the VPC of the instance prepared in Step 1.
4.3 Configure a listener
Create an HTTP listener to forward requests from the HTTP protocol. When the load balancer instance receives an HTTP request, it can forward the request to the ECS instances in the server group.
In the Application Load Balancer (ALB) console, select a region.
In the left-side navigation pane, click Instance. Find the load balancer created in Step 4.1 and click Create Listener in the Actions column. Follow the on-screen instructions to create the listener.
This example uses the following configurations. You can keep the default values for any settings not mentioned.
Parameter
Description
Select listener protocol
Select HTTP.
Listener port
The port on which the load balancer provides services. The demo service uses port 80, which means the listener will handle requests to port 80 of the load balancer.
Select server group
Select the server group created in Step 4.2.
4.4 Associate the load balancer
On the Scaling Groups page, find the scaling group you created in Step 3. In the Actions column, click View Details to go to the scaling group's details page.
On the Basic Information tab, find Associated ALB/NLB Server Groups. Click Add Associated ALB/NLB Server Group. In the dialog box that appears, click Add Server Group and follow the prompts to associate the server group created in Step 4.2. After configuring, click OK to complete the association.
ImportantThe port number in this configuration should be the port on which your business service provides services. The demo service in this example uses port 80.
5. Scale out and verify
After configuring the scaling group and associating it with a load balancer, you can scale out instances to verify that the cluster works correctly.
Trigger a scale-out event for the scaling group, and the scaling group will automatically create instances.
You can trigger a scale-out event by modifying the Desired Capacity for the scaling group. Follow these steps:
On the Scaling Groups page, find the scaling group you just created. In the Actions column, click View Details to go to the scaling group's details page.
On the Basic Information tab, find Instance Scaling Overview and click
. In the Edit Instance Scaling Overview dialog box, change the Expected Number of Instances from 0 to 3 (meaning the cluster needs 3 ECS instances to provide services). Click OK to apply the change.Wait for the instances to be created. Three ECS instances will be created in the scaling group. You can check the creation status on the Instances tab.
Access the load balancer's address to verify that it is routing requests to the new instances.
Go to the Application Load Balancer (ALB) console.
Find the load balancer created in Step 4.1. Under DNS Name, find the access URL.
Visit this URL multiple times. You should see different IP addresses, which confirms that you can access different servers through the load balancer.
6. (Optional) Clean up resources
If you no longer need the cluster, you can follow this process to clean up the resources.
Release the load balancer created in Step 4.1. For more information, see Release an ALB instance.
Delete the scaling group created in Step 3. For more information, see Delete a scaling group.
Delete the server group created in Step 4.2. For more information, see Create and manage server groups.
Delete the custom image created in Step 2. For more information, see Delete a custom image.
Delete the vSwitches you created in Step 3 and Step 4.1. For more information, see Create and manage a VPC and Delete a vSwitch.
Clean up the demo service deployed in Step 1. If you deployed the demo service from Step 1, you can delete its ROS stack. When deleting the stack, select Release Resources as the Deletion Method to clean up the resources created by the demo service. For more information, see Delete a stack.
Next steps
Before you go live
To ensure this elastic and highly available solution runs stably in a production environment, we recommend that you complete the following actions before you go live:
Refine the scaling group configuration
Implement multi-zone disaster recovery: You can configure vSwitches in multiple availability zones for your scaling group and set a Multi-zone Scaling Policy. This allows the scaling group to create instances across multiple availability zones and distribute service instances evenly among them, improving the scaling success rate and the cluster's disaster recovery capability. For more information, see Scaling policies.
Select multiple instance types: A scale-out event may fail if a single instance type is out of stock. You can select multiple instance types to improve the scaling success rate. For more information, see Create a scaling configuration (ECS instances).
Conduct thorough testing
If you plan to use this elastic and highly available solution in a production environment, we recommend that you conduct tests after setting up the cluster. This ensures that your service works correctly when replicated and allows you to perform stress tests on the cluster to estimate your resource needs.
Use a domain name for access
Use Auto Scaling features
Design a scaling strategy: This tutorial does not cover the design of automatic scaling strategies. You can set up automatic scaling for your scaling group later to optimize costs. To learn how to design a scaling strategy, see Supported scaling strategies.
Advanced features: If you have more advanced requirements for your scaling group, such as improving scaling success rates or further reducing costs, see Advanced features.