← Solutions Architect Associate study path

Resilient architectures · 13 min

Decoupling with Amazon SQS

In this lesson you will learn when to put an Amazon SQS queue between two parts of an application, how to choose between a standard queue and a FIFO queue, and how to adjust the visibility timeout and the dead-letter queue so a message isn't processed twice or doesn't go around forever.

Amazon Simple Queue Service (Amazon SQS) is a managed AWS queue that you place between two parts of an application, so one of them can leave pending work and the other can process it at its own pace. A queue is a waiting list where pending tasks are kept until someone handles them, and none of them is lost while it waits. Managed means that AWS takes care of the computers it runs on, and you just use it. For example, an online store leaves each new order in a queue, and the program that prepares the orders picks them up from there.

The exam uses SQS as the answer when a piece of the architecture fails or gets overwhelmed, that is, receives more work than it can handle, because it depends on another one. For example, if the store's website goes down every time the orders system fills up, the question usually calls for a queue between the two.

What problem does a queue solve?

Two parts of an application are coupled when one calls the other directly and waits for its response, because then the first one fails every time the second one fails. For example, in an online store where the website calls the orders service directly, everything works as long as the orders don't exceed what that service can handle. However, on sale day the service gets overwhelmed, the website is left waiting and the errors start, and if the service goes down, the website goes down with it.

On the left, the website calls the processing service and there are errors at the peak; on the right, the website leaves the order in an SQS queue and the workers empty it at their own pace

With an SQS queue in between, the website leaves each order in the queue and answers the user immediately, while the workers, which are the programs that do the work, read from the queue at their own pace. That way, at the peak the queue grows and then empties without losing orders, and each side scales on its own, that is, grows or shrinks without depending on the other. For example, on sale day the store can receive thousands of orders in an hour and prepare them during the afternoon, without any customer seeing an error.

The reliability pillar of the AWS Well-Architected Framework, AWS's guide to good practices, recommends this same design. It asks for loosely coupled dependencies and asynchronous interactions, in which whoever asks for something doesn't sit waiting for the answer, in the cases where an immediate response isn't needed. For example, the customer needs to know right away that their order went in, but doesn't need to wait for someone to pack it.

How does an SQS queue work?

The life cycle of a message in Amazon SQS in four steps: the producer sends, the consumer receives, the consumer processes and the consumer deletes

An SQS queue has two sides: a producer sends a message, and a consumer receives it, processes it and deletes it. A message is the piece of data that describes a task, such as an order number and what the customer bought. The queue stores each message redundantly, with several copies, on several , so the producer and the consumer don't have to be available at the same time. For example, the store's website can leave orders at night even if the program that prepares them is turned off, and that program finds them in the morning.

If nobody deletes a message, the queue keeps it for 4 days by default, and this retention period can be adjusted from 60 seconds to 14 days. Besides the retention period, a queue has three other limits and settings that show up in the questions:

  • The message size, which goes up to 1 and which, for larger content, is handled with the Extended Client Library, which stores the content in and leaves only the reference in the queue. For example, the store saves a product photo in S3 and sends in the message only where it was saved.
  • Long polling, which waits up to 20 seconds for a message to arrive before responding and so reduces empty responses, the times the consumer asks and there is nothing.
  • The delay queue, which delays the delivery of each message by up to 15 minutes. For example, the store uses it to send a reminder a few minutes after a customer leaves their cart.

Where does the queue go?

Users upload videos and the application converts them to several resolutions, which takes minutes. At peak hours the upload page gets slow and sometimes fails. Which change best decouples the two parts?

Standard queue or FIFO queue?

The two types of Amazon SQS queue: the standard queue, with nearly unlimited calls and at-least-once delivery, and the FIFO queue, with strict ordering and no duplicates

An SQS queue can be one of two types, which differ in ordering and in duplicates:

  • The standard queue, which is the default type, handles a nearly unlimited number of calls per second, stores each message in several before confirming it and delivers each message at least once.
  • The FIFO queue (first in, first out, the first thing that goes in is the first thing that comes out), which strictly preserves the order in which the messages were sent, doesn't put duplicates in the queue and has a limit of transactions per second that goes up with high mode.

Each type has one more detail worth knowing. Because the standard queue is distributed across many servers, it sometimes delivers a message more than once or out of order, so the consumer has to handle duplicates. For example, if the store uses a standard queue to send confirmation emails, a customer may occasionally get two emails for the same order.

The FIFO queue, on the other hand, supports message groups, where the order is preserved within each group and different groups are processed in parallel. For example, the store can use one group per customer, so the changes to each order are applied in order and the orders of different customers move forward at the same time. Also, the name of a FIFO queue has to end in .fifo.

Standard or FIFO?

0 of 4

Tap the type of queue each requirement calls for.

  1. The transactions of a bank account are applied in the order in which they happened.

  2. Resize millions of photos a day, where processing a photo twice causes no problem.

  3. A ticket sale where whoever arrived first wins.

  4. Distribute card validations among many workers as fast as possible.

What is the visibility timeout?

When a consumer receives a message, SQS doesn't delete it; it leaves it in the queue, hidden from the other consumers, for a period called the visibility timeout. This period is 30 seconds by default and reaches a maximum of 12 hours from the first receipt. If the consumer finishes in time, it deletes the message, but if it fails or takes longer than the timeout, the message becomes visible again and another consumer receives it. For example, if the program preparing an order shuts down halfway through, the order shows up in the queue again and another program takes it, so it isn't lost.

That is why the visibility timeout has to be adjusted to how long processing takes. With a timeout shorter than the processing, the message goes back to the queue while the first consumer is still working, and a second consumer processes it again. For example, if charging an order takes 50 seconds and the timeout is 30, after 30 seconds another consumer takes the same order and the customer pays twice.

A message received three times, hidden for 30 seconds each time, that the consumer never deletes and that ends up in the DLQ

A message with malformed data, for example an order with letters where the quantity should be, falls into a similar cycle. Each time a consumer receives it, the message stays hidden for 30 seconds, the consumer fails without deleting it and the message goes back to the queue, so the cycle repeats forever.

What is a dead-letter queue?

A dead-letter queue (DLQ) is a second queue where the messages that fail end up, and it is used to break that cycle. On the original queue you configure a redrive policy with maxReceiveCount, which is the number of times a message can be received before it is moved to the DLQ. There the team reviews the message and, once it fixes the problem, sends it back to the original queue with a redrive. For example, the order with letters in the quantity moves to the DLQ after several attempts, the store's team fixes it and sends it back to the queue so it gets prepared.

The DLQ has three rules that depend on the type of queue:

  • The location, which has to be the same account and the same Region as the source queue.
  • Retention in a standard queue, where the message keeps its original timestamp when it moves to the DLQ, which is why AWS recommends that the DLQ have a longer retention than the original queue. Otherwise, a message that had already spent days in the original queue could be deleted from the DLQ before anyone reviews it.
  • Ordering in a FIFO queue, which breaks when a message moves to the DLQ.

True or false?

0 of 4
  1. If processing takes 2 minutes and the visibility timeout is 30 seconds, the same message can be processed twice.

  2. When it receives a message, SQS deletes it from the queue.

  3. The DLQ can be in another Region to protect the failed messages.

  4. By default a message lives 4 days in the queue if nobody deletes it.

On the exam

SQS questions usually describe a piece that gets overwhelmed or loses work when another one fails. Phrases such as "decouple", "absorb spikes", "don't lose orders if processing fails" and "process in the background" point to an SQS queue. Within SQS, "order" and "no duplicates" point to a FIFO queue, while "maximum throughput" points to a standard queue. For example, if the question says the store's payments have to be applied in order and never twice, the answer is a FIFO queue.

cloud path tip: if the question says that a message is processed several times, check that the visibility timeout is longer than the processing. If it says that some messages fail over and over again, the answer is a DLQ.

Review question

An application processes orders from a standard SQS queue. The logs show that some orders are charged twice. Each order takes about 50 seconds to process and the queue has the default configuration. Which change solves the problem with the least effort?

Full account and new experience account

Amazon SQS is in the free plan of the new experience account, the simplified way to open an AWS account, and works the same as in a full account, within the project's Region (us-east-2 for Latin America). In the new experience there is no , so if a queue policy, the rule that says who can use the queue, denies access to everyone, the project owner deletes it from , in Root access management. For example, if while practicing you write by mistake a policy that locks you out of your own queue, the project owner removes it from there.

Next