Amazon SQS (Amazon Simple Queuing Service) is a managed message queue service that allows us to create scalable and reliable systems. Services can be detached using queues.
They can interact asynchronously with each other, which is especially beneficial when the producing service’s throughput differs from the consuming service’s.
In this article, you will learn about Amazon SQS and its benefits, SQS FIFO Queues, their features, and their limitations.
What is Amazon SQS?
Amazon Simple Queue Service (Amazon SQS) is a hosted queue that allows you to link and decouple distributed software systems and components. It is safe, durable, and available. Dead-letter queues and cost allocation tags are two typical Amazon SQS structures.
It offers a general web services API that you can use with any programming language supported by the AWS SDK. Standard and FIFO queues are supported by Amazon SQS.
Benefits of Using Amazon SQS
Some benefits of using Amazon SQS are as follows:
- Security: You have complete control over who can send and receive messages from an Amazon SQS queue.
- Server-side Encryption (SSE): It protects the contents of messages in queues using keys controlled by the AWS Key Management Service (AWS KMS), allowing you to send sensitive data.
- Durability: Amazon SQS saves your messages on numerous servers to ensure their security. Messages are delivered at least once in standard queues, whereas messages are processed exactly once in FIFO queues.
- Availability: To allow highly concurrent access to messages and high availability for producing and consuming messages, Amazon SQS leverages redundant infrastructure.
- Scalability: Without any provisioning instructions, Amazon SQS may process each delayed request independently, scaling transparently to manage any traffic surges or spikes.
- Reliability: During processing, Amazon SQS locks your messages so that numerous producers and consumers can send and receive messages at the same time.
- Customization: Your queues don’t have to look the same; for example, you can modify a queue’s default delay. You can use Amazon Simple Storage Service (Amazon S3) or Amazon DynamoDB to store the contents of messages greater than 256 KB, with Amazon SQS holding a pointer to the Amazon S3 object, or you can divide a large message into smaller messages.
Understanding Amazon SQS FIFO Queues
FIFO (First-In-First-Out) queues are used to improve messaging between applications when the order of processes and events is crucial, or duplicates cannot be permitted. The following are some scenarios in which FIFO queues could be useful:
- To ensure that user-entered commands are executed in the correct sequence.
- By transmitting price updates in the correct order, the correct product price will be displayed.
- To stop a student from enrolling in a course without first creating an account.
FIFO queues enable Exactly-once Processing as well, but they can only handle a certain number of Transactions Per Second (TPS).
1. Key Terms
The following definitions will help you better grasp how FIFO queues work.
Message Deduplication ID
The token is used to prevent duplicate messages from being transmitted. Any messages sent with the same message deduplication ID are accepted successfully but not sent within the 5-minute deduplication period if a message with that deduplication ID is sent successfully.
Message Group ID
The tag indicates whether or not a message belongs to a particular message group. Messages from the same message group are always handled one by one, in the sequence in which they were received (however, messages that belong to different message groups might be processed out of order).
Receive Request Attempt ID
ReceiveMessage calls are deduplicated using this token.
Sequence Number
Each message is assigned a huge, non-consecutive number by Amazon SQS.
2. Message Ordering
The SQS FIFO queue is an enhancement to and addition to the regular queue. FIFO (First-In-First-Out) delivery and exactly-once processing are the most essential aspects of this queue type:
- The order in which messages are transmitted and received is rigorously maintained, and a message is delivered just once before being processed and deleted by a consumer.
- There are no duplicates added to the queue.
FIFO queues also enable message groups, which allow many ordered message groups to be stored in a single queue. A FIFO queue has no limit on the number of message groups it can hold.
3. FIFO Delivery Logic
The following concepts will help you better grasp how to send and receive messages to and from SQS FIFO Queues.
Sending Messages
Amazon SQS saves and acknowledges the transmission of several messages sent in succession to a FIFO queue, each with a unique message deduplication ID. Then, in the exact order in which the messages were broadcast, each message can be received and processed.
Messages in SQS FIFO queues are ordered by message group ID. When multiple hosts (or separate threads on the same host) deliver messages to a FIFO queue with the same message group ID, Amazon SQS keeps the messages in the order they arrive for processing. To ensure that Amazon SQS maintains the order in which messages are sent and received, each producer should send all of its messages using a unique message group ID.
Only one message group ID is affected by SQS FIFO queue logic. Within an Amazon SQS queue, each message group ID represents a different sorted message group. All messages are sent and received in exact order for each message group ID. Messages with differing message group ID values, on the other hand, maybe sent and received in the wrong order. A message must be associated with a message group ID. The activity will fail if you don’t supply a messaging group ID. Provide the same message group ID for messages submitted to the FIFO queue if you need a single group of ordered messages.
Receiving Messages
You can’t ask to receive messages from a certain message group.
Amazon SQS tries to return as many messages with the same message group ID as feasible when receiving messages from a SQS FIFO queue with multiple message group IDs. As a result, other consumers will be able to process messages with a different message group ID. If you receive a message with a message group ID, you will not receive any additional messages for that message group ID unless you delete it or it becomes visible.
Using the ReceiveMessage action’s MaxNumberOfMessages request parameter, you can receive up to 10 messages in a single call. These messages can have the same message group ID and keep their FIFO order. If there are less than ten messages available with the same message group ID, you may receive messages from a different message group ID in the same batch of ten, but still in FIFO order.
Retrying Multiple Times
Producers and consumers can try several times with SQS FIFO queues:
If a SendMessage action fails, the producer can retry as many times as necessary, using the same message deduplication ID. Multiple retries have no effect on the sequencing of messages or introduce duplicates if the producer receives at least one acknowledgment before the deduplication interval expires.
If a ReceiveMessage action fails, the consumer can retry as many times as needed, using the same receive request attempt ID. Multiple retries have no effect on the ordering of messages if the consumer receives at least one acknowledgment before the visibility timeout expires.
If you receive a message with a message group ID, you will not receive any additional messages for that message group ID unless you delete it or it becomes visible.
4. Exactly-once Processing
Unlike traditional queues, SQS FIFO queues do not cause duplicate messages to be sent. FIFO queues prevent duplicates from being sent to a queue. Amazon SQS does not put duplicates into the queue if you retry the SendMessage action during the 5-minute deduplication time.
You must do one of the following to configure deduplication:
- Deduplication based on content can now be enabled. This instructs Amazon SQS to construct the message deduplication ID using an SHA-256 hash based on the message body, not the message characteristics.
- Specify the message’s deduplication ID (or view the sequence number) explicitly.
5. Moving from Standard Queue to FIFO Queue
If you want to leverage the ordering or exactly-once processing features of FIFO queues in an existing application that uses conventional queues, you must configure the queue and your application correctly.
An existing standard queue cannot be converted to a SQS FIFO queue. To make the switch, either establish a new FIFO queue for your application or remove and recreate your existing standard queue as a FIFO queue.
Use the checklist below to ensure that your application functions appropriately with a SQS FIFO queue:
- FIFO queues can handle up to 3,000 messages per second per API method if batching is used (SendMessageBatch, ReceiveMessage, or DeleteMessageBatch). The 3000 messages per second correspond to 300 API calls, each containing ten messages in a batch. You can submit a support ticket to request a quota increase. FIFO queues can handle up to 300 API calls per second per API method without batching (SendMessage, ReceiveMessage, or DeleteMessage).
- Only per-queue delays are supported by FIFO queues. If your application sets the same DelaySeconds value for each message, you’ll need to change it to eliminate the per-message delay and instead set DelaySeconds for the entire queue.
- A message group ID is required for every message submitted to a FIFO queue. If you don’t need numerous sorted message groups, give all of your messages the same message group ID.
- Confirm the following before delivering messages to a FIFO queue:
- You can alter your application to provide a unique message deduplication ID for each delivered message if it can transmit messages with identical message contents.
- You can enable content-based deduplication if your application sends messages with distinct message bodies.
- You don’t need to change anything in your consumer’s code. Consider adding a receive request attempt ID to each ReceiveMessage action if it takes a long time to process messages and your visibility timeout is set to a high number. This prevents queues from stalling due to unsuccessful receive attempts by allowing you to retry receive attempts in the event of a network problem.
6. High Throughput for FIFO Queues
FIFO queues with high throughput support a higher number of requests per API, per second. Boost the number of message groups you use to increase the number of requests in high throughput FIFO queues. 300 queries per second are supported by each message group.
Any new or existing SQS FIFO queue can have high throughput enabled. When creating and editing FIFO queues, you now have three new options:
- Enable High Throughput FIFO: This allows messages in the current FIFO queue to have more throughput.
- Deduplication Scope: Specifies whether deduplication occurs at the queue or message group level.
- FIFO Throughput Limit: Determines whether the FIFO queue’s throughput quota is set at the queue or message group level.
To allow a FIFO queue to have a high throughput (console):
- Step 1: Create or update a first-in, first-out (FIFO) queue.
- Step 2: When configuring the queue’s options, select Enable High Throughput FIFO. When you enable high throughput for FIFO queues, the following options are enabled:
- The deduplication scope is set to Message group, which is essential for using FIFO queues with high throughput.
- The FIFO throughput limit is set to Per message group ID, which is the required value for FIFO queues with high throughput.
- Step 3: If any of the settings required for using high throughput for FIFO queues are changed, standard throughput is applied to the queue, and deduplication is performed as stated.
- Step 4: Continue to fill in all of the queue options. Choose Create queue or Save when you’re done.
You can send messages to the FIFO queue after building or updating it, as well as receive and remove messages, all at a higher TPS.
Limitations of Amazon SQS FIFO Queues
Here are a few limitations of Amazon SQS FIFO Queues:
- Performance: FIFO queue performance is impacted by the overhead of supporting exactly-once and in-order messages. This restriction could cause a bottleneck in your application. This blog post goes through a more in-depth investigation of the performance limits.
- Message Ordering: FIFO queues ensure that messages are ordered in the order that they are received by SQS. As a result, if you have numerous producers, you won’t be able to have total worldwide ordering.
- Message De-duplication: Content-based de-duplication is possible with FIFO queues. This is useful in the event that the producer sends a message more than once by accident. FIFO de-duplicates messages over a 5-minute period that cannot be changed. This restriction should be noted by any programs that rely on message de-duplication.
Move data effortlessly with Hevo’s zero-maintenance data pipelines, Get a demo that’s customized to your unique data integration challenges
Conclusion
SQS FIFO queues ensure that messages are delivered in the correct order and at the right time. These features, however, come at the expense of some performance constraints. Understand the breakdown of AWS SQS costs, including free tier options and scalability advantages. Discover more at AWS SQS Cost Structure.
SQS FIFO queues should be utilized only when these features are absolutely necessary; otherwise, Standard queues would be a better fit. Optimize your AWS SQS operations with a clear grasp of the DeleteMessage feature. Find essential details in SQS DeleteMessage.
However, as a Developer, extracting complex data from a diverse set of data sources like Databases, CRMs, Project management Tools, Streaming Services, and Marketing Platforms to your Database can seem to be quite challenging.
Sharon is a data science enthusiast with a hands-on approach to data integration and infrastructure. She leverages her technical background in computer science and her experience as a Marketing Content Analyst at Hevo Data to create informative content that bridges the gap between technical concepts and practical applications. Sharon's passion lies in using data to solve real-world problems and empower others with data literacy.