Overview
EC2 instances in an Auto Scaling group (ASG) poll messages from an SQS queue. The ASG scales automatically based on the queue size. See ASG and 02-SQS-Standard-Queues.
Scaling on Queue Length
- The CloudWatch metric
ApproximateNumberOfMessages(queue length) shows how many messages are left in the queue. - Set a CloudWatch alarm, e.g. over 1,000 messages means processing is lagging.
- The alarm triggers a scaling action in the ASG, adding EC2 instances.
- Messages are processed faster and the queue size decreases.
It works in both directions: scale up or scale down.
Pattern 1: SQS as a Buffer for Database Writes
Scenario: your biggest marketing campaign. Customers place orders stored in Amazon RDS or Aurora (OLTP) or DynamoDB (NoSQL). Writing very fast and suddenly into the database can cause errors if the database is overloaded, and you lose customer transactions.
Solution: use SQS as a buffer.
- The front-end application writes each transaction request as a message into the SQS queue (enqueue) instead of writing to the database. SQS is, per AWS, infinitely scalable with no throughput issue.
- Messages are durably stored, so none are lost.
- A second ASG dequeues the messages and inserts them into the databases.
- Only after a message is known to be inserted into the database is it deleted from the queue.
This guarantees every transaction is written. It works only if the client does not need confirmation that the write reached the database. Once the message is in SQS, it will eventually be written.
SQS as a buffer for database writes is a very common exam pattern.
SQS as a buffer for database writes
Pattern 2: Decoupling Application Tiers
Instead of one application receiving a request, processing it and returning a response, requests arrive at a front-end web application, which sends them to an SQS queue. The back-end processing job receives and handles the messages in its own time. Each tier scales accordingly.
SQS is a very common answer on the exam, more common than before. Decoupling, sudden spike load, timeouts, or a need to scale a lot very fast -> think SQS queue.
Questions & Answers
Q1: Which metric scales an ASG that consumes an SQS queue?
Answer:
ApproximateNumberOfMessages (queue length) in CloudWatch, with an alarm that triggers a scaling action.
Q2: How do you avoid losing transactions when a database is overloaded by a spike?
Answer:
Write requests to an SQS queue and have a second ASG dequeue them into the database, deleting each message only after a successful insert.
Q3: When does the buffer pattern not work?
Answer:
When the client needs confirmation that the write reached the database.
Check yourself
Quiz
Flashcards
Click the card (or focus it), then use ← → to move and Space to flip.