# AWS ECS SQS EC2 Job Queue

By Numa

Topic: Computing, AI &amp; Electronics

[Open the interactive Jig](https://ask.jig.com/j/iOXl7cO1FI0O)

## Guide

### 1. A job factory that never drops a job

Little paper jobs arrive on the left: photos to resize, orders to pack, emails to send\. Workers on the right pick them up and finish them\. Three Amazon Web Services \(AWS\) parts make this run smoothly, even when a crowd shows up at once\.

Time in this model is sped up: real servers take minutes to start, not seconds\.

### 2. SQS: the waiting line

Amazon SQS \(Simple Queue Service\) is a waiting line for jobs, called messages\. Senders drop a message in and move on; the message waits safely until a worker is free\. The line soaks up a sudden rush so nothing is lost\.

By default SQS keeps a waiting message for 4 days, and it can be set to keep it for up to 14\.

### 3. ECS tasks: the workers

Amazon ECS \(Elastic Container Service\) keeps a chosen number of identical workers running\. Each worker is called a task\. Tasks pull work: each asks the line for a message, does the job, then asks for the next one\.

SQS never pushes jobs onto a worker\. Workers ask when they are ready\.

### 4. Taken, not thrown away

When a task takes a message, SQS only hides it and starts a timer, called the visibility timeout\. When the job is done, the task deletes the message\. If the task gets stuck, the timer runs out and the message reappears for another task to try\. ECS replaces the stuck worker\.

The usual timer is 30 seconds\. A job that fails again and again goes to a dead\-letter queue so it cannot jam the line\.

### 5. EC2: the rooms that hold the workers

Tasks need a computer to run on\. Amazon EC2 \(Elastic Compute Cloud\) lends out computers in Amazon data centers, called instances\. Each instance gives several tasks a share of its processors and memory\. Here every instance has room for four tasks\.

Four tasks per instance is just this model\. The real number depends on the instance size and what each task needs\.

### 6. Rush hour: scaling out

Watch the line grow\. Amazon CloudWatch measures the backlog per task: waiting messages divided by working tasks\. When the needle passes the target, ECS starts more tasks\. When the instances are full, a new instance joins to make room\.

An AWS example: a target of 100 messages per task, with 1,500 waiting and 10 tasks, means adding 5 tasks\.

### 7. Quiet time: scaling in

When traffic calms down, the line empties and the needle drops\. ECS stops tasks that are no longer needed, but only after they finish their current job\. An instance with no tasks left is shut down, so you stop paying for it\.

Scaling in is done slowly on purpose, so a short pause in traffic does not throw away workers you will need again\.

### 8. Without a hitch

The line absorbs bursts, tasks pull at their own pace, hidden messages come back if something breaks, and two layers grow and shrink with traffic: tasks quickly, instances more slowly\. Slide the traffic and watch the whole factory adjust\.
