Lesson 7 of 8 in Cloud fundamentals with AWS
Managed databases, load balancers and autoscaling
A managed database such as Amazon RDS runs a database engine for you, including backups and patching. A load balancer spreads incoming requests across several servers, and an Auto Scaling group adds or removes servers as demand changes.
Draft. This lesson is written and waiting for a person to run every command and review it. It is safe to learn from, but check the official documentation for anything you depend on.
Read these first: Virtual machines on EC2, VPC and subnets: your private network in AWS
The problem this solves
One server running your app and your database is a single point of failure. If it dies, everything dies together, and so does the data if you forgot a backup. Running your own database well means handling backups, patches, failover and disk growth.
Splitting the work helps. A managed database takes over the backups, patching and failover. Several app servers behind a load balancer mean one can fail and users never notice. An Auto Scaling group replaces failed servers and adds more when traffic rises.
These services are also the ones most likely to run for days and bill by the hour. In this concept you only look at them, because you understand the idea better by reading the options than by paying for the lab.
How it works
- People and clients
- Network
- Runs code
- Data
Visitors reach a load balancer, which sends each request to a healthy server in one of two zones, and both servers use one managed database that keeps a standby copy.
- Visitors to Load balancer: 1 request
- Load balancer to VM in zone A: 2 sends to a healthy VM
- Load balancer to VM in zone B: 2 or to this one
- VM in zone A to Managed database: 3 query
- VM in zone B to Managed database: 3 query
- Control
- Runs code
- Network
- People and clients
A metric rises above its target, the Auto Scaling group launches a new server, the server registers with the load balancer, and the extra traffic is shared.
- Metric to Auto Scaling group: 1 above the target
- Auto Scaling group to New VM: 2 launches
- New VM to Load balancer: 3 registers
- More visitors to Load balancer: 4 load is shared
Read the first diagram left to right. The load balancer has a single stable address and keeps a list of servers, called targets. It checks each target's health and sends requests only to healthy ones. The servers sit in different availability zones, so the loss of one zone leaves the other running.
RDS runs engines such as PostgreSQL and MySQL. AWS handles the operating system, patching and automated backups. With a Multi-AZ setup it keeps a standby copy in another zone and fails over if the primary breaks. That doubles the database's cost, so it is a decision, not a default for a learning lab.
An Auto Scaling group has a minimum, a maximum and a desired size. It keeps the number of healthy servers at the desired size and replaces any that fail. A scaling rule can raise the desired size when a metric such as CPU use goes over a target. The group is the reason servers become disposable.
Equivalents: Azure has Azure SQL and managed PostgreSQL, Azure Load Balancer and Application Gateway, and virtual machine scale sets. Google Cloud has Cloud SQL, Cloud Load Balancing and managed instance groups.
Try it yourself
These steps only read. On a new account every list is empty, which also proves you are not paying for any of them. Nothing is created here.
- 1List any managed databases
aws rds describe-db-instances --query "DBInstances[].[DBInstanceIdentifier,DBInstanceStatus]" --output table
An empty result means you have no RDS instance. A
stoppeddatabase is still not free, because its storage is billed, and AWS starts a stopped database again automatically after seven days. - 2List any load balancers
aws elbv2 describe-load-balancers --query "LoadBalancers[].[LoadBalancerName,Type,State.Code]" --output table
Application, network and gateway load balancers all appear here. Each bills for every hour it exists, plus for the traffic it handles.
- 3List any Auto Scaling groups
aws autoscaling describe-auto-scaling-groups --query "AutoScalingGroups[].[AutoScalingGroupName,MinSize,MaxSize,DesiredCapacity]" --output table
An Auto Scaling group with a minimum above zero keeps servers running, so deleting a server by hand will just make it launch a new one. Delete the group first.
- 4Read the options without creating anything
In the console open RDS, then Create database, and read through the Standard create options: the engine, the free tier template, the Multi-AZ choice and the Public access setting. Then close the page without choosing Create. Do the same for EC2, then Load Balancers, then Create load balancer. Nothing is created until you confirm, so nothing needs cleanup.
What breaks, and where to look
Your load balancer's address returns 503 Service Temporarily Unavailable, or 502 Bad Gateway.
- WHY IT HAPPENS
- A 503 usually means no healthy targets are registered. A 502 means a target answered badly or closed the connection. The common reasons are a failing health check path or a security group that blocks the load balancer from reaching the servers.
- CHECK FIRST
- Run
aws elbv2 describe-target-health --target-group-arn <target-group-arn>. The reason field says why each target is unhealthy, for exampleTarget.TimeoutorTarget.ResponseCodeMismatch.
Your app cannot connect to RDS: connection to server at "<endpoint>" failed: Connection timed out.
- WHY IT HAPPENS
- The database is not reachable from where the app runs. It is in a private subnet, 'Public access' is off, or its security group does not allow the database port from the app's security group.
- CHECK FIRST
- Get the endpoint with
aws rds describe-db-instances --query "DBInstances[].Endpoint.Address" --output text, then read the database's security group inbound rules for the port, such as 5432 for PostgreSQL.
A database you 'stopped' is back on the bill, or its charges never went away.
- WHY IT HAPPENS
- AWS restarts a stopped RDS instance automatically after seven days, and storage and backups are billed while it is stopped.
- CHECK FIRST
- Check the status with
aws rds describe-db-instances. If you are finished with it, delete it in the console and decide deliberately whether to keep a final snapshot.
When not to use this
- For one small app, one VM or a managed app platform is simpler and cheaper. A load balancer bills by the hour even when idle, so one server does not need one.
- A managed database trades control for convenience. You cannot log in to its operating system or install arbitrary software on it.
Check yourself
What does a load balancer do when a server fails its health check?
It stops sending requests to that server and uses only the healthy ones.
What does Multi-AZ add to a managed database?
A standby copy in another availability zone with automatic failover, at roughly double the database cost.
Why does deleting one server in an Auto Scaling group not make it go away?
The group is told to keep a desired number of servers, so it launches a replacement. Change or delete the group instead.
Keep going
Lesson 61 of 92 in the DevOps path