Scalability
When it comes to scalability, it divides into two parts:
- Horizontal Scalability -> elasticity, we also call this scale up and scale down
- Vertical Scalability -> we also call this scale in and scale out
Horizontal Scalability
increase the number of instances/systems, distributed system
Vertical Scalability
increase the size of the instance, like from t2.micro to t2.large, etc…
HA (High Availability)
HA (High Availability) is a plan for failure, you should prepare for
- Everything will fail
- You should always plan for failure
- It is designed for failure
- you can use Multi-AZ and Multi-Regions
- RDS supports
- Multi-AZ: this is for Disaster Recovery
- Read Replicas: this is for Improving performance
- Scaling Out and Scaling Up
- Scaling Out: more EC2 Instances
- Scaling Up: upgrading with more resources (RAM, CPU, etc..)
- Considering "cost" as a factor
- different S3 storage class
High Availability:
- HA usually goes hand-in-hand with Horizontal Scalability
- HA means running your application/system in at least 2 data centers, to be specific, 2 AZs
- HA can be both active or passive
- active: Horizontal Scalability
- passive: RDS Multi-AZ
There are two methods of Horizontal Scaling:
- Auto Scaling Group - ASG
- Load Balancer
Load Balancing (Elastic Load Balancer - ELB)
Load Balancers are servers that forward internet traffic to multiple servers (EC2) downstream
But, why would we use a load balancer, anyway?
- Spread load across multiple downstream instances
- Expose a single point of access (DNS) to your application
- Seamlessly handle failures of downstream instances, through health checks
- Do regular health checks to your instances
- Provide SSL Termination (HTTPS) for your website
- Enforce stickiness with cookies
- HA across AZs
- Separate public traffic from private traffic
There are three types of Load Balancers on AWS
- Application Load Balancer
- HTTP, HTTPS, WebSocket
- supports SSL
- Network Load Balancer
- TCP, TLS (secure TCP) & UDP
- supports SSL
- Classic Load Balancer
- HTTP, HTTPS, TCP
- DO NOT support SSL
You can setup internal/private or external/public ELBs
more details, please refer here: https://aws.amazon.com/elasticloadbalancing/
Application Load Balancer (ALB)
It balances HTTP / HTTPS traffic, you can also create
- advanced request routing
- sending specific requests to specific web servers
Target Groups
Each target group is used to route requests to one or more registered targets.
where targets can be:
- EC2 Instances (can be managed by an ASG) - HTTP
- ECS tasks (Elastic Container Service) - HTTP
- Lambda functions - HTTP request if translated into a JSON event
- IP Address - must be private IPs
ALB can also route to multiple target groups, also, Health checks are at the target group level
Good to know about ALB
- it has a fixed hostname, (
xxx.<region>.elb.amazonaws.com
, etc..) - the application servers don't see the IP of the client directly, if you wanna see, then:
- the true IP of the client is inserted in the header
X-Forwarded-For
- we can also get the Port (
X-Forwarded-Port
) and proto (X-Forwarded-Proto
)
- the true IP of the client is inserted in the header
Network Load Balancer (NLB)
It balances TCP (layer 4) traffic
Forward TCP & UDP traffic to your instances. Network Load Balancer can handle millions of requests per second while maintaining ultra-low latencies, ~100ms, where 400ms for ALB
NLB has one static IP per AZ and supports assigning Elastic IP
Normally, NLB is used for extreme performance, TCP or UDP traffic
Classic Load Balancer (CLB)
This is just legacy Load Balancers
suggest reading:
- Elastic Load Balancing FAQs: https://aws.amazon.com/elasticloadbalancing/faqs/?nc=sn&loc=6
Advanced Load Balancer Theory
Sticky Sessions
Sticky Session allows you to bind a user’s session to a specific EC2 Instance. This ensures that all requests from the user during the session are sent to the same instance
you can enable the “sticky session” for Application Load Balancer, but the traffic will be sent at the “Target Group” level, rather than Individual EC2 Instance
Cross-zone Load Balancing
With cross-zone load balancing, each load balancer node for your Classic Load Balancer distributes requests evenly across the registered instances in all enabled Availability Zones. If cross-zone load balancing is disabled, each load balancer node distributes requests evenly across the registered instances in its Availability Zone only.
https://docs.aws.amazon.com/elasticloadbalancing/latest/classic/enable-disable-crosszone-lb.html
- With Cross Zone Load Balancing each load balancer instance distributes evenly across all registered instances in all AZ
- Otherwise, each load balancer node distributes requests evenly across the registered instances in its AZ only
Cross-Zone in 3 types of Load Balancers
- Application Load Balancer
- Always on (cannot be disabled)
- No charges for inter AZ data
- Network Load Balancer
- Disabled by default
- You pay charges for inter AZ data is enabled
- Classic Load Balancer
- Disabled by default
- No charges for inter AZ data is enabled
Path Patterns
This creates a listener with rules to forward requests based on the URL Path -> “path-based routing”
- Stickey Session is useful when the user stores information locally to that instance, more like an online file storage server, like Box or Dropbox?
- Cross-zone Load Balancing enables you to balance loads across multiple AZs
- Path Patterns are just "path-based routing", it is based on the URL path in the requests
Auto Scaling Group (ASG)
the goal for an ASG is to:
- Scale in or scale out to match an increased load or decreased load
- Ensure minimum / maximum number of running instances
- automatically register new instances to a load balancer
Scaling Policies
- Target Tracking Scaling
- Most simple and easy to set-up
- Example I want the average ASG CPU to stay at around 40%
- Simple / Step Scaling
- When some metrics is triggered, do something
- Scheduled Actions
- eg. increase the min capacity to 10 at 5 pm.
Launch Configurations are where the details (AMI, IAM) are specified for creating/launching new instances
Elastic Beanstalk
Deploy the application to AWS without actually knowing AWS
Elastic Beanstalk automatically handles the details of capacity provisioning load balancing, scaling, and application health monitoring
There are 3 components in ElasticBeanStalk
- Application
- Application version: each deployment gets assigned a version
- Environment name (dev, test, prod ..): free naming
You can deploy application versions to environments and can promote application versions to the next environment; It supports Rollback feature to previous application version; Full control over the lifecycle of environments