Skip to content

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.

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

#!/bin/bash
export PATH=$PATH:/home/ubuntu/.nvm/versions/node/v22.0.0/bin/
echo "hi there before"
echo "hi there after"
npm install -g pm2
cd /home/ubuntu/week-22
pm2 start index.js
pm2 save
pm2 startup

  • ASG

    • 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

You can create an dynamic scaling policy

Try playing with the Min and max on the ASG

Try to stop a few servers in the ASG. Notice they spin back up to arrive at the desired amount.

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

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 client
const autoscaling = new AWS.AutoScaling();
// Function to update the desired capacity of an Auto Scaling group
const 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 usage
const groupName = 'node-app-1'; // Set your Auto Scaling group name
const newDesiredCapacity = 3; // Set the new desired capacity
// Call the function
updateDesiredCapacity(groupName, newDesiredCapacity);

Please delete all things one by one

  1. ASG
  2. Target group
  3. Load balancer
  4. Launch template
  5. Image
  6. 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