Ordering Messages In An Amazon SQS Queue

Last November, AWS released a very useful feature in its FIFO support for Amazon Simple Queue Service (SQS). FIFO stands for “first in first out”, which essentially means that a FIFO queue maintains the order of how messages are delivered out of an SQS queue. The FIFO queue feature also guarantees on-time delivery of a message, which is a pretty big deal if you’ve had to design systems where on-time delivery was a “must have” feature.

Prior to having FIFO queue support for SQS, the random order of SQS messages was not a deal breaker but any ordering needed to be handled outside of SQS. You could create your own ordering key and parse it in the message attributes. Features such as the message timeout window and dead letter queues helped manage the issue of message duplication if needed. This approach didn’t always suit if you needed to implement a queue where the order and frequency of messages were mission critical.

Enter First In First Out queue support for Amazon SQS in US West and US East.

How FIFO for Amazon SQS works

You interact with a FIFO SQS queue the same way that you interact with a standard SQS queue. With a FIFO queue, however, the order in which messages are sent and received is strictly preserved. A message is delivered once and remains available until a consumer processes and deletes it. This means that duplicates will not be created in the queue. This is pretty cool, so let’s take a look at how we can use it.

An SQS queue is recognized as a FIFO queue if it has the FifoQueue attribute set to true and the queue name ends with the .fifo suffix. FIFO queues use the same API actions as standard queues.

// A FIFO queue must have the FifoQueue attribute set to True
attributes.put("FifoQueue", "true");
// A FIFO queue name must end with the .fifo suffix
CreateQueueRequest createQueueRequest = new CreateQueueRequest("MyFifoQueue.fifo").withAttributes(attributes);
String myQueueUrl = sqs.createQueue(createQueueRequest).getQueueUrl();

The methods for receiving messages, deleting messages, and changing the visibility timeout are the same as for a standard SQS queue. However, when you send a message from a FIFO queue you must specify a message group ID.
Every message sent to a FIFO queue requires a MessageGroupId value. If you don’t add a MessageGroupId value, the send action fails.

// You must provide a MessageGroupId
sendMessageRequest.setMessageGroupId("1");

How Amazon SQS uses MessageGroupId

Amazon SQS uses the MessageGroupId value to group messages together. Messages within a group are ordered within that group. So if we add a MessageGroupId=1 value to 10 messages, those 10 messages are treated as a message group and will be processed one by one, in a strict order relative to the group MessageGroupId=1. If we send another five messages with a new MessageGroupId=2, those five messages will be ordered within the group MessageGroupId=2.

That means we can have multiple ordered message groups within a single queue. So, our 10 messages in MessageGroupId=1 and our five messages in MessageGroupId=2 will be processed in order relative to each group rather than to the queue as a whole. As ordering only applies to the message group, messages that belong to a different message group might be processed in the same order.

If you have a system that will have multiple senders and multiple recipients, you should generate a unique MessageGroupId for each message. If you don’t need to create multiple ordered message groups, just use the same MessageGroupId for all of your messages.

If your SendMessage action fails, you can retry sending as many times as you want using the same message deduplication ID. Assuming that you receive at least one acknowledgment before the deduplication interval expires (the minimum deduplication window is five minutes), multiple retries will not affect the ordering of messages or create duplications.

When you receive messages, SQS will try to return as many messages with the same MessageGroupId as possible. However, you can’t request to receive messages with a specific MessageGroupId. If you get a failed ReceiveMessage action, you can retry using the same receive request attempt ID. Assuming that you receive at least one acknowledgment before the visibility timeout expires, multiple retries will not affect the ordering of messages.

A FIFO message uses a MessageDeduplicationId key to manage deduplication of sent messages. Every message must have a unique MessageDeduplicationId. If a message with a particular MessageDeduplicationId is sent successfully, any messages sent with the same MessageDeduplicationId will not be delivered during the five-minute deduplication window.

So, if you send 10 messages in succession to a FIFO queue, each with a distinct MessageDeduplicationId, SQS acknowledges the transmission and stores those messages. Then, each message can be received and processed in the exact order in which the messages were transmitted.

How to create a MessageDeduplicationId

There are two ways to create a MessageDeduplicationId. First, you can define your own MessageDeduplicationId for the message. If your application sends messages with identical message bodies, you can modify your code to provide a unique message MessageDeduplicationId for each sent message.

// define a MessageDeduplicationId
sendMessageRequest.setMessageDeduplicationId("1");

Second, you can just enable content-based deduplication. This tells SQS to use an SHA-256 hash to generate the MessageDeduplicationId using the body of the message. If your application sends messages with unique message bodies, then enabling content-based deduplication will be easiest.

// Generate a MessageDeduplicationId based on the content
attributes.put("ContentBasedDeduplication", "true");

Is a FIFO SQS for you?

Here are five things to keep in mind when considering if a FIFO SQS queue suits your use case.

1. Conversions. You can’t convert an existing standard queue into a FIFO queue. You need to delete your standard queue and then create a new FIFO queue.
2. Transaction rate. FIFO queues are limited to 300 transactions per second (TPS). If your application needs a higher rate than 300 TPS, stick to a standard SQS queue.
3. Delays. FIFO queues support per queue delays but not per message delays. If your application sets a DelaySeconds = 10 on each message, that’s probably not going to work with a FIFO queue. You will need to remove any per message parameter and set a DelaySeconds =10 parameter on the entire queue.
4. Processing time. You need to ensure that there is enough time to process and delete a message. Extend your message visibility timeout to the maximum time it takes to process and delete the message. If you don’t know how long it takes to process a message, specify the initial visibility timeout plus the period of time after which you can check whether the message is processed. If it’s taking a long time to process messages and your visibility timeout is already set to a high value, consider adding a receive request attempt ID to each ReceiveMessage action. This allows you to retry receive attempts in case of network failures and prevents queues from pausing due to failed receive attempts.
5. Inflight message limit. While Amazon SQS queues can include an unlimited number of messages, there is a 20,000 inflight message limit for inflight FIFO queues (The inflight message limit for standard SQS queues is 120,000.). Messages are inflight after they have been received from the queue by a consumer, but have not yet been deleted from the queue.

Learning more about FIFO SQS

Hopefully, I have piqued your interest in this great new feature. There is a lot more to know about SQS FIFO, so if you are interested, I suggest starting with the AWS developer guide and API reference. If you have any questions or content you’d like to add, please leave a comment right here below this post.

Avatar

Written by

Andrew Larkin

Andrew is an AWS certified professional who is passionate about helping others learn how to use and gain benefit from AWS technologies. Andrew has worked for AWS and for AWS technology partners Ooyala and Adobe. His favorite Amazon leadership principle is "Customer Obsession" as everything AWS starts with the customer. Passions around work are cycling and surfing, and having a laugh about the lessons learnt trying to launch two daughters and a few start ups.


Related Posts

Alisha Reyes
Alisha Reyes
— April 9, 2020

New on Cloud Academy: AWS Solutions Architect – Associate Exam Prep, Azure Courses, Google Associate Cloud Engineer Exam Prep, Programming Labs, and Much More

Free content on Cloud Academy More and more customers are relying on our technology and content to keep upskilling their people in these months, and we are doing our best to keep supporting them. While the world fights the COVID-19 pandemic, we wanted to make a small contribution to he...

Read more
  • AWS
  • Azure
  • Google Cloud Platform
  • programming
Joe Nemer
Joe Nemer
— April 3, 2020

Breaking News: All AWS Certification Exams Now Available Online

Remote proctoring for all AWS certifications Cloud Academy is an Advanced AWS Technology Partner, and we are happy to announce all AWS certification exams are available online!  What does this mean for you? You can stay focused on your certification goal. Or you can start a certifica...

Read more
  • AWS
  • AWS certification
  • AWS Certifications
Connie Benton
Connie Benton
— April 1, 2020

How To Build a Career with AWS Certifications

From Iaas and PaaS solutions to digital marketing, cloud computing reshapes the world of technology. As the influence of this technology grows, so does investment. Tens of billions of dollars are being spent on cloud computing-related services each year. This influx is continuing to inc...

Read more
  • AWS
  • Certifications
Vijayakumar Athithan
Vijayakumar Athithan
— March 27, 2020

What is Cognito in AWS?

Web applications usually allow a valid username and password combination for successful sign in to the application. Modern authentication flows incorporate more approaches to ensure user authentication. When using AWS, this is no exception, thanks to the abilities and features offered b...

Read more
  • AWS
  • AWS Cognito
  • Solutions Architect
Avatar
Andrew Larkin
— March 20, 2020

The 12 AWS Certifications: Which is Right for You and Your Team?

As companies increasingly shift workloads to the public cloud, cloud computing has moved from a nice-to-have to a core competency in the enterprise. This shift requires a new set of skills to design, deploy, and manage applications in cloud computing. As the market leader and most ma...

Read more
  • AWS
  • AWS Certifications
Alisha Reyes
Alisha Reyes
— March 17, 2020

Cloud Academy’s Blog Digest: How Do AWS Certifications Increase Your Employability, How to Become a Microsoft Certified Azure Data Engineer, and more

With everything going on right now, it's likely that the only thing you've been reading lately is related to the coronavirus pandemic. It's important to stay informed during these times, but it's also good to jump into something that can take your mind off of the current situation for j...

Read more
  • AWS
  • Azure
  • blog digest
  • Certifications
  • Cloud Academy
  • programming
  • Security
Avatar
Cloud Academy Team
— March 13, 2020

Which Certifications Should I Get?

As we mentioned in an earlier post, the old AWS slogan, “Cloud is the new normal” is indeed a reality today. Really, cloud has been the new normal for a while now and getting credentials has become an increasingly effective way to quickly showcase your abilities to recruiters and compan...

Read more
  • AWS
  • Azure
  • Certifications
  • Cloud Computing
  • Google Cloud Platform
Alisha Reyes
Alisha Reyes
— March 7, 2020

New on Cloud Academy: Intro to GitOps; AWS Courses; Java, Python, Amazon Linux 2, Ubuntu, & Docker Playgrounds; and much more

New Lab Playgrounds This month, our Content Team released six new "playground labs." Our playground labs provide a safe and secure sandbox environment for you to explore your own ideas, follow along with Cloud Academy courses, or answer your own questions — all without having to instal...

Read more
  • AWS
  • Azure
  • gitops
  • Google Cloud Platform
  • lab playground
  • programming
Alisha Reyes
Alisha Reyes
— March 6, 2020

New on Cloud Academy: Intro to GitOps; AWS Courses; Java, Python, Amazon Linux 2, Ubuntu, & Docker Playgrounds; and much more

New Lab Playgrounds This month, our Content Team released six new "playground labs." Our playground labs provide a safe and secure sandbox environment for you to explore your own ideas, follow along with Cloud Academy courses, or answer your own questions — all without having to instal...

Read more
  • AWS
  • Azure
  • gitops
  • Google Cloud Platform
  • lab playground
  • programming
Patrick Navarro
Patrick Navarro
— March 4, 2020

AWS Certifications: How Do They Increase Your Employability and Progress Your Career?

AWS certifications are no walk in the park. They’re designed to validate in-depth, specialist knowledge and comprehensive experience, often requiring months of dedicated studying to earn even for those already working with the cloud platform. But the rewards that AWS professionals ca...

Read more
  • AWS
  • AWS certification
  • certification
Avatar
Chandan Patra
— February 21, 2020

Elasticsearch vs. CloudSearch: AWS Cloud Search Choices

Elasticsearch vs. CloudSearch: What's the main difference? Let's compare AWS-based cloud tools: Elasticsearch vs. CloudSearch. While both services use proven technologies, Elasticsearch is more popular, open source, and has a flexible API to use for customization; in comparison, CloudS...

Read more
  • AWS
  • Azure
  • cloudsearch
  • elasticsearch
Avatar
Andrew Larkin
— February 13, 2020

Cloud Academy Content Roadmap Updates

Welcome to our Q1 2020 roadmap. This is the content we plan to build over the next three months, between February 1 - and April 30, 2020. Let's look at some of our roadmap highlights. Atlassian Bamboo for CI/CD We had a lot of requests for practical guides on how to apply DevOps tool...

Read more
  • Artificial Intelligence
  • AWS
  • Azure
  • Docker
  • Google Cloud Platform
  • Kubernetes
  • Machine Learning