Simple Queue Service (SQS)

Learn about the Amazon SQS and the type of queues we can create using this service.

We'll cover the following

Amazon Simple Queue Service (SQS) is a message queuing service that can help us decouple our application components. It uses queues to store the messages sent by producers and sends them to consumers using a polling model where consumers check the queues for messages sent by the producer whenever they are ready to process them.

Press + to interact

Amazon SQS uses a redundant infrastructure and stores the messages it receives in multiple availability zones, making SQS queues highly available and durable. Unprocessed messages are retained for a maximum of 14 days, and after this period, all copies of the message are deleted from the SQS servers.

Press + to interact
Redundant infrastructure in Amazon SQS
Redundant infrastructure in Amazon SQS

In SQS, it is the responsibility of the consumer to delete the messages available in an SQS queue after processing them. Once a message “M1” is received by a consumer, SQS starts a visibility timeout period where this message is not sent to any consumer to avoid message reprocessing. SQS supports two types of queues, standard and FIFO queues.

Standard queues

In standard queues, messages are delivered at least once to each consumer, however, since the messages are stored in multiple availability zones, there is a chance multiple copies of one message are delivered. In some rare cases, you might delete a message whose copy is stored in an unavailable server. The message will become visible for processing once again when the server is available. Due to this, the order of delivery of the messages is not guaranteed in standard queues, and it is recommended to use them in applications where processing one message multiple times doesn’t affect it.

FIFO queues

In FIFO queues, messages are delivered to consumers exactly once, and their order of delivery is maintained. Due to this functionality, they are suitable for applications where the order of messages is critical, for example, e-commerce applications where the order of the messages being sent and delivered must be maintained.

While creating a FIFO queue, we must add the suffix .fifo in the queue’s name. In addition to the attributes associated with standard queues, FIFO queues have the following two attributes to help them send and receive messages:

  • Message duplication ID: This attribute helps to prevent message duplication in the queue and is provided by the sender. If a queue receives more than one message with the same message duplication ID within five minutes, it retains one copy and discards all other copies.

  • Message group ID: This attribute is used to order the messages received by the queue. Messages with the same group ID are stored in the order they are received and are then processed in order. While delivering messages, FIFO queues try to send as many messages as it can that belong to the same group, however, messages with different group IDs can be sent out of order.

Press + to interact
Message grouping in FIFO queues
Message grouping in FIFO queues

In the diagram above, there are two different message groups, M and X, sent by Producer 1 and 2, respectively. The message X1 is sent before the message M2; however, since the message group M was created before group X, SQS will send message M2 before it sends X1 to its consumers.

Get hands-on with 1300+ tech skills courses.