← Solutions Architect Associate study path

Performance and cost · 12 min

Scaling with Amazon EC2 Auto Scaling and load balancers

In this lesson you will learn which Amazon EC2 Auto Scaling policy fits each traffic pattern, which metric drives it, how an Amazon SQS queue lets each tier scale separately and when to use an ALB, an NLB or a Gateway Load Balancer.

An application scales when its capacity grows and shrinks with demand, that is, with how many people use it at each moment. On the SAA-C03 exam the questions on this topic ask you how to achieve that with Amazon EC2 Auto Scaling and Elastic Load Balancing. To build that answer you choose three things: the policy that decides when the number of changes, the measure that drives that policy and the load balancer that sits in front of the instances. For example, an online store needs few instances early on a Tuesday morning and many on the day of a special sale, and without scaling it would overpay on Tuesday or go down during the sale.

Vertical and horizontal scaling

There are two ways to scale an application. Vertical scaling gives more processor or memory to the same instance, like swapping a computer for a more powerful one, while horizontal scaling adds more instances that share the work. For example, the store scales vertically if it swaps its only instance for a larger one, and it scales horizontally if it goes from two identical instances to six.

The AWS compute guide says horizontal scaling is the one you need for dynamic workloads, which are the ones that change a lot during the day, because it grows and shrinks with demand and you pay only for what you use. That is why the exam assumes you scale horizontally, and what it asks is how to do it.

Purchased capacity versus demand

The purple line is the demand at an online store over a year, with the November peak. The dashed line is the capacity it has. Switch between your own data center and AWS.

JFMAMJJASOND
  • Demand
  • Capacity
  • Paid for and unused
  • Customers without service

In your data center

Servers are bought all at once, so capacity goes up in steps. You paid for everything in lavender and it went unused; the red is November, when you came up short.

An illustration without figures: the shape of the demand is an example.

Amazon EC2 Auto Scaling policies

The five Amazon EC2 Auto Scaling policies: target tracking, step scaling, simple scaling, scheduled scaling and predictive scaling, with what each one does

An Auto Scaling group is a set of identical instances that AWS keeps between a minimum and a maximum that you set. The policy is the rule that decides when that number goes up or down, and it is almost always based on a . There are five types of policy:

  • Target tracking, where you choose a metric and a target value and the group adds or removes instances to keep it close to that value, like a thermostat, and which AWS recommends over the others.
  • Step scaling, which makes adjustments of different sizes depending on how far the metric went past the limit you set in an alarm, which is an alert that goes off when the metric crosses that limit.
  • Simple scaling, which makes a single adjustment per alarm and waits for a period of time (cooldown) before the next one.
  • Scheduled scaling, which changes capacity at fixed times, for changes you know about in advance.
  • Predictive scaling, which analyzes the history, detects daily or weekly patterns and launches capacity before the load arrives, and which AWS suggests when traffic repeats in cycles and the application is slow to start.

For example, if the store knows it opens a sale every Friday night, it uses scheduled scaling to have more instances at that time. If its visits go up every morning and its instances are slow to start, predictive scaling gets them ready before the customers arrive.

How target tracking works

With target tracking, the group compares the metric with the target and adjusts the instances until they are close. For example, a group of 4 instances has its average CPU, which is how busy the processor is, at 78% against a target of 50%.

Target tracking cycle: the instances report their CPU, CloudWatch calculates an average of 78%, the alarm goes off because the target is 50% and the group goes from 4 to 7 instances

The instances report their CPU to CloudWatch, which is the AWS service that collects metrics. CloudWatch calculates the average, the alarm that the policy created when it was configured goes off and the group goes to 7 instances without anyone signing in to the . The same cycle removes instances when the CPU stays below the target, so early in the morning the store stops paying for instances it doesn't use.

For the metric, AWS recommends one that decreases in proportion when you add capacity, so that if you double the group, the metric drops by half. Average CPU and the load balancer's requests per target behave this way, but the free memory of a single instance doesn't. For example, if with two instances each one receives a thousand requests per minute, when you go to four instances each one receives half.

Which scaling policy?

0 of 4

Tap the policy that fits each case.

  1. Keep the group's average CPU close to 60% with traffic that goes up and down with no pattern

  2. A payroll system that needs 20 instances on the last Friday of every month, from 8 a.m. to 6 p.m.

  3. An application with peaks every morning during business hours that takes 10 minutes to start

  4. Keep 1,000 requests per instance behind an ALB

Decoupling the tiers with a queue

An application usually has tiers, which are parts with different jobs: the web tier serves the visitors and another tier does the heavy work. When the web server also processes the orders, a spike in orders overloads it completely and it stops serving users. With an Amazon SQS queue in between, which is a line where messages are kept until someone handles them, the web tier only leaves one message per order and a tier of workers processes them at its own pace, so each tier scales on its own. The exam guide calls this decoupling workloads so that components can scale independently. For example, during a special sale the store's website keeps responding quickly even if many orders pile up in the queue.

To scale the workers, the number of messages in the queue isn't enough as a metric, because it doesn't take into account how many instances there are and it can launch too many instances while the queue empties. That is why AWS recommends the backlog per instance, which is the pending messages divided by the instances in the group.

An SQS queue with 1,500 messages and a group of 10 instances: the backlog is 150 per instance against a target of 100, and the group adds 5 instances

For example, with 1,500 messages in the queue and 10 instances in the group, the backlog per instance is 150. The target comes from dividing the wait time you accept for a message (10 seconds) by how long each message takes to process (0.1 seconds), which gives 100. Because 150 is more than 100, the group adds five instances and the backlog goes back to 100 per instance.

Hibernating instances that are slow to start

Some instances take a long time to start and to load what they need into their memory before they can serve. You can hibernate those instances instead of stopping them, just like a laptop that you close and that picks up where you left off when you open it. EC2 saves the RAM, which is the working memory, to the EBS and, when it starts again, resumes the processes where they were. For example, an instance that takes a long time to load the store's catalog into memory starts ready if it was hibernated.

Types of load balancers

The three current types of Elastic Load Balancing: Application Load Balancer at layer 7, Network Load Balancer at layer 4 and Gateway Load Balancer for virtual appliances

In front of the group sits an Elastic Load Balancing load balancer, which receives all the traffic and distributes it among the instances so none of them gets overloaded. Load balancers are told apart by the network layer they work at: at layer 7 they read the content of each request, such as the address of the page that was requested, and at layer 4 they only see which address and which channel the traffic is going to. There are three current types, since the Classic Load Balancer is from the previous generation:

  • Application Load Balancer (ALB), which works at layer 7 and receives the or HTTPS request. It checks the rules in priority order and sends it to the target group of the rule that applies. It is used for web applications and .
  • Network Load Balancer (NLB), which works at layer 4 with TCP, and TLS, handles millions of requests per second and has a static IP address per zone, to which you can assign an Elastic IP address. It is used for traffic that isn't HTTP or for clients that need static IP addresses.
  • Gateway Load Balancer (GWLB), which distributes traffic among virtual appliances, which are security programs such as or intrusion detection systems, with the GENEVE protocol on 6081.

For example, the store uses an ALB to send requests for the payments path to one group of instances and requests for the catalog to another. On the other hand, an online game that uses UDP puts an NLB in front of its servers.

Which load balancer?

An online game server uses UDP. The company's partners only allow connections to static IP addresses in their firewall. What do you put in front of the instances?

If your account uses the new experience

In a new experience account, EC2 Auto Scaling, Elastic Load Balancing and Amazon SQS are in the free plan, so you can build a group with its load balancer and a target tracking policy. Use small instances, and when you finish delete the group and the load balancer so you don't leave charges. For example, a group with a minimum of one instance keeps creating a new one every time you delete the previous one, so you have to delete the whole group.

On the exam

The questions on this topic describe how traffic changes or what breaks during peaks. These words in the question point to the answer:

  • "Every Monday at 8", which points to scheduled, and "daily pattern" together with "slow start", which point to predictive.
  • "Orders that are lost during peaks" or "the web tier is overloaded by processing", which point to an SQS queue with workers in their own group.
  • "HTTP", "path-based rules" or "microservices", which point to ALB.
  • "UDP", "static IP address" or "millions of requests", which point to NLB.
  • "Third-party firewalls", which points to GWLB.

For example, if the question says "orders are lost when there are many users at once", the answer includes an SQS queue between the web tier and the workers.

Review question

An application receives photos, stores them and generates three sizes of each one. During campaigns, the web tier takes so long to process that users see errors when they upload their photos. What change lets each part scale separately?

Next

  • Choosing an AWS compute service, the AWS decision guide. The "Scalability" and "Elastic Load Balancing" sections cover scaling and load balancers.
  • Take a Solutions Architect practice exam at /examenes.