Today we discuss about the best scaling mechanism for the production server. There are two types of scaling which we can inherit either Horizontal Scaling or Vertical Scaling.
In Horizontal Scaling we can add more servers to our pool of resources and make them distribute the load accordingly but In Vertical Scaling we have to increase the computational or memory power of the single machine to handle the load more efficiently. Horizontal Scaling is more efficient and economic as compared to the Vertical Scaling because in Horizontal Scaling we can increase or decrease the pool of resources in real time without any downtime or affecting the services which is not possible in Vertical Scaling. So, we always prefer horizontal scaling for our production or live server environment.
Lets implement the auto scaling (Automatic Horizontal Scaling) on AWS.
To implement auto scaling on your application, you have to take care of few things which are as follows-:
- Application and Database should be on separate servers so that we can clone our application server as per demand and it doesn’t affect our central database, for database server we can use RDS Service of AWS (Required).
- All dynamic files (files created at the time of code execution for ex-: images, thumbnails, templates etc.) should be stored and managed through S3 or any other Central Storage Media so that all servers can access these files irrespective of their life cycle (Required).
- We can setup GIT or SVN on the application server for Updating and Maintaining the code structure throughout the server pool (Recommended).
- Load Balancer is required to balance the load between the pool of servers (Required).
- Load Balancer, Database Server, Application Server and Auto scaling group should be in the same VPC (Required).
- Create AMI image of your application server to setup launch configuration of autoscaling(Required).
First we have to create a load balancer as follows-:
- After logged into AWS Console go to EC2 Panel and select load balancer.
- Click “Create Load Balancer” button.
- Type Load Balancer name, Select the same VPC and subnet in which your application and database servers are present and setup the listener configuration as per your choice either on port 80,443 or both.
- Select an existing Security Group or Create a new one in the same VPC and subnet.
- Configure health check on the load balancer either on TCP port basis or webpage basis, for more details refer to AWS Documents for Load Balancer.
- Add EC2 instance (Application Server) in the load balancer.
- Add Tags and Review all your settings in the next window, after reviewing all the settings launch the load balancer.
As we have already added one EC2 instance (Application Server) in our Load Balancer, now we have to enable the auto scaling by following steps.
- After logged into AWS Console go to EC2 Panel and select “Launch Configuration” in “Auto Scaling” pane.
- Click “Create Auto Scaling Group” and then click on “Create Launch Configuration”.
- In the next step choose your application server AMI which you have created for launch configuration.
- In next step choose the instance type and fill the launch configuration details like name, IAM role and other advanced details or you can make it simple by giving name only.
- In the next step all the storage volume and set the storage space for instances.
- Now create or select the security group but you have to take care that these security groups should be the member of same VPC group in which application and database server are present.
- After that review your configuration and launch the configuration in next step.
- Now in the “Configure Auto Scaling group details” pane type the group name for auto scaling group and set the initial size of the group by giving size in “Group size”, In “Network” drop down list and “Availability Zone” select your same VPC and Zones respectively.
- In Advanced Details select your load balancer which you have created before and enter the “Health Check Grace Period”.
- Now configure the scaling policies as per your requirement either on CPU consumption, Memory Consumption or based on traffic for more details please check here or you can select the keep this group at its initial size.
- After that skip the notification part and give the tags to your auto scaled instances.
- Review your configuration in the next step and create the auto scaling group.
After this go to your EC2 instance panel and check for launched instances which will be managed on the basis of your scaling policy and you don’t need any manual scaling for extra traffic.