Horizontal scaling
Course: Horizontal and vertical scaling, Indexes in DBs - chapter 5
Horizontal scaling represents increasing the number of instances you have based on a metric to be able to support more load.
AWS has the concept of Auto scaling groups, which as the name suggests lets you autoscale the number of machines based on certain metrics.
Buzz words
Section titled “Buzz words”Images (AMI) - Snapshots of a machine from which you can create more machines
Load balancer - An entrypoint that your users send requests to that forwards it to one of many machines (or more specifically, to a target group). Its a fully managed service which means you don’t have to worry about scaling it ever. AWS takes care of making it highly available.
Target groups - A group of EC2 instances that a load balancer can send requests to
Launch template - A template that can be used to start new machines
💡 Please make sure you get rid of all your resources after this.
There are two ways you can use ASGs
- Create a EC2 instance.
- install Node.js on it https://www.digitalocean.com/community/tutorials/how-to-install-node-js-on-ubuntu-20-04
- Clone the repo - https://github.com/100xdevs-cohort-2/week-22
- Create an AMI with your machine
- Create security group
- Launch template
#!/bin/bashexport PATH=$PATH:/home/ubuntu/.nvm/versions/node/v22.0.0/bin/echo "hi there before"echo "hi there after"npm install -g pm2cd /home/ubuntu/week-22pm2 start index.jspm2 savepm2 startup
-
ASG
- Callout on availability zones - ASGs try to balance instances in each zone

- Callout on availability zones - ASGs try to balance instances in each zone
-
Load balancer
- Add an HTTPS Listener from your domain, request a certificate from ACM
-
Target group - Attach the target group to the ASG
Autoscaling part
Section titled “Autoscaling part”You can create an dynamic scaling policy

Try playing with the Min and max on the ASG

Try killing servers
Section titled “Try killing servers”Try to stop a few servers in the ASG. Notice they spin back up to arrive at the desired amount.
Simulate a scale up
Section titled “Simulate a scale up”Try running an infinite for loop on the instance to see if a scale up happens
let c = 0;
while (1) { c++;}
You’ll notice the desired capacity goes up by one in some time

Try turning the infinite loop off and notice a scale down happens
Scaling via a Node.js app
Section titled “Scaling via a Node.js app”Create a new user with permissions to AutoscalingFullAccess


import AWS from 'aws-sdk';
AWS.config.update({ region: 'ap-south-1', accessKeyId: 'YOUR_ACCESS_KEY', secretAccessKey: 'YOUR_ACCESS_SECRET'});
// Create an Auto Scaling clientconst autoscaling = new AWS.AutoScaling();
// Function to update the desired capacity of an Auto Scaling groupconst updateDesiredCapacity = (autoScalingGroupName: string, desiredCapacity: number) => { const params = { AutoScalingGroupName: autoScalingGroupName, DesiredCapacity: desiredCapacity };
autoscaling.setDesiredCapacity(params, (err, data) => { if (err) { console.log("Error", err); } else { console.log("Success", data); } });};
// Example usageconst groupName = 'node-app-1'; // Set your Auto Scaling group nameconst newDesiredCapacity = 3; // Set the new desired capacity
// Call the functionupdateDesiredCapacity(groupName, newDesiredCapacity);Cleanup
Section titled “Cleanup”Please delete all things one by one
- ASG
- Target group
- Load balancer
- Launch template
- Image
- Instance that the image was created from
💡 Try using elastic beanstalk. Gives you the same benefits w/o the developer having to create all of these