liveBook · Manning

Amazon SQS

As explained in Amazon Web Services in Action, Third Edition: An in-depth guide to AWS

Amazon Simple Queue Service is a managed message-queuing service for storing and delivering messages between distributed components.

Amazon Simple Queue Service (SQS) provides managed message queues for work that does not need an immediate response. A producer sends a message to a queue; a consumer retrieves and processes it later. Neither component needs to know about the other, and the queue can buffer messages while consumers are unavailable. section 14.0 section 14.2

Generate a website screenshot in the background

A Node.js producer sends a URL and unique ID to SQS, then returns the ID to the user. A worker reads the message, captures the website, and uploads an image to S3 under that ID. The user can check S3 for the result without waiting for screenshot generation in the original request. section 14.2.2

A worker processes each message in three steps:

  • Receive: poll using the queue URL. Choose a batch size of one to ten, a visibility timeout long enough for processing, and optionally a long-poll wait of up to 20 seconds. section 14.2.5
  • Process: use the message body as the task input. In the screenshot example, the worker loads the URL and uploads the generated PNG to S3. section 14.2.5
  • Acknowledge success: delete the message using its receipt handle. If it is not deleted before the visibility timeout expires, it can be delivered again. section 14.2.5 section 14.2.6
Listing 14.8 worker.js: Connecting the parts
async function run() {
  while(true) {                                      #1
    const message = await receive();                 #2
    if (message) {
      console.log('Processing message', message);
      await process(message);                        #3
      await acknowledge(message);                    #4
    }
    await new Promise(r => setTimeout(r, 1000));     #5
  }
};
 
run();                                               #6

The worker loop connects receiving, processing, and acknowledgement. Deletion happens only after processing completes. section 14.2.5

Design consumers for at-least-once delivery: a message may arrive twice, including after a visibility timeout or, rarely, despite a delete request. Replacing an S3 image under the same ID leaves the intended result unchanged; sending an email twice would not. Ordinary SQS messaging does not guarantee consumption order. FIFO queues provide ordered messages when that requirement matters. section 14.2 section 14.2.6

SQS scales queue throughput and storage and is highly available by default, but usage is billed per request: sending and receiving both count, and larger payloads can count as multiple requests. Queue length can also drive a CloudWatch alarm that adjusts an EC2 Auto Scaling group, adding workers when background work accumulates. Unlike a load balancer, this arrangement requires application code to produce and consume messages. section 14.2 section 17.3.2 section 14.3

How Amazon Web Services in Action, Third Edition: An in-depth guide to AWS covers it

How deep it goes
Works through receiving, processing, and deleting messages, then examines delivery and ordering limitations; does not describe SQS internals.
Examples
Uses a Node.js URL-to-PNG producer and worker, plus a CloudWatch queue-length alarm for scaling workers.
What it assumes
Introduces producer–consumer queues from their basic behavior while using Node.js scripts for the implementation.
What you can do after
Build it from scratch with a Node.js AWS SDK worker that polls SQS, processes messages, and deletes them after success.
How it connects
Connects message queues to asynchronous decoupling, idempotence, S3, CloudWatch, and EC2 Auto Scaling.

Related concepts

This explainer was generated by Manning's knowledge graph from the text of Amazon Web Services in Action, Third Edition: An in-depth guide to AWS. The section links point to the parts of the book it draws on.