Amazon SQS is a managed message queue. One part of your app puts messages in the queue, and another part picks them up and works on them when it is ready. It stops a slow or failing part from taking the rest of the system down with it.

What it does

  • Holds messages until a consumer processes them.
  • Lets the sender and the worker run at different speeds.
  • Is fully managed, so there are no queue servers to run.
  • Offers Standard queues and FIFO queues.

Key concepts

Piece What it is
Producer and consumer The part that sends messages and the part that reads and processes them
Standard queue Very high throughput, at-least-once delivery, order is not guaranteed
FIFO queue Keeps order and avoids duplicates, with lower throughput
Visibility timeout How long a message is hidden from others while one consumer works on it
Long polling The consumer waits for messages, which reduces empty responses and cost
Dead-letter queue (DLQ) A queue for messages that keep failing
Retention period How long an unprocessed message is kept

Good to know

  • A consumer must delete a message after it succeeds, or it will reappear after the visibility timeout.
  • Set the visibility timeout longer than the time your consumer needs to process a message.
  • Standard queues can deliver a message more than once, so make your processing safe to repeat.
  • Use long polling to cut cost and empty reads.
  • Use a dead-letter queue with a maximum receive count so bad messages do not loop forever.
  • SQS holds messages for one consumer group to pick up. SNS pushes one message to many subscribers. They are often used together.
  • Check the current message size limit and pricing in the documentation.

Hands-on: try it in your own account

  1. Open SQS > Create queue and choose Standard.
  2. Name it practice-queue, and leave the defaults.
  3. Create a second queue called practice-dlq.
  4. Edit practice-queue, turn on the dead-letter queue, choose practice-dlq, and set the maximum receives to 3.
  5. Open Send and receive messages and send a message with some text.
  6. Choose Poll for messages, and look at the message you get.
  7. Delete the message after you have read it, and send another.
  8. Clean up: delete both queues.

Common mistakes

  • Not deleting the message after processing, so it is processed again and again.
  • A visibility timeout shorter than the processing time, so two workers do the same job.
  • Assuming a Standard queue keeps order.
  • Having no dead-letter queue, so one bad message keeps failing.
  • Forgetting to delete the practice queues.

Key takeaways

  • SQS is a managed queue that lets parts of your system work at their own pace.
  • Delete messages after success, and set the visibility timeout above the processing time.
  • Make processing safe to repeat, and add a dead-letter queue.
  • Delete the practice queues when you are done.

Tomorrow I'll start a new AWS topic.

Part of #120DaysOfAWS.