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

Joe Nemer
Joe Nemer
— September 15, 2020

New Content: Azure DP-100 Certification, Alibaba Cloud Certified Associate Prep, 13 Security Labs, and Much More

This past month our Content Team served up a heaping spoonful of new and updated content. Not only did our experts release the brand new Azure DP-100 Certification Learning Path, but they also created 18 new hands-on labs — and so much more! New content on Cloud Academy At any time, y...

Read more
  • AWS
  • Azure
  • DevOps
  • Google Cloud Platform
  • Machine Learning
  • programming
Joe Nemer
Joe Nemer
— August 28, 2020

AWS Certification Practice Exam: What to Expect from Test Questions

If you’re building applications on the AWS cloud or looking to get started in cloud computing, certification is a way to build deep knowledge in key services unique to the AWS platform. AWS currently offers 12 certifications that cover major cloud roles including Solutions Architect, De...

Read more
  • AWS
  • AWS Certifications
Patrick Navarro
Patrick Navarro
— August 25, 2020

Overcoming Unprecedented Business Challenges with AWS

From auto-scaling applications with high availability to video conferencing that’s used by everyone, every day —  cloud technology has never been more popular or in-demand. But what does this mean for experienced cloud professionals and the challenges they face as they carve out a new p...

Read more
  • AWS
  • Cloud Adoption
  • digital transformation
Avatar
Andrew Larkin
— August 18, 2020

Constant Content: Cloud Academy’s Q3 2020 Roadmap

Hello —  Andy Larkin here, VP of Content at Cloud Academy. I am pleased to release our roadmap for the next three months of 2020 — August through October. Let me walk you through the content we have planned for you and how this content can help you gain skills, get certified, and...

Read more
  • alibaba
  • AWS
  • Azure
  • content roadmap
  • Content updates
  • DevOps
  • GCP
  • Google Cloud
  • New content
Alisha Reyes
Alisha Reyes
— August 5, 2020

New Content: Alibaba, Azure AZ-303 and AZ-304, Site Reliability Engineering (SRE) Foundation, Python 3 Programming, 16 Hands-on Labs, and Much More

This month our Content Team did an amazing job at publishing and updating a ton of new content. Not only did our experts release the brand new AZ-303 and AZ-304 Certification Learning Paths, but they also created 16 new hands-on labs — and so much more! New content on Cloud Academy At...

Read more
  • AWS
  • Azure
  • DevOps
  • Google Cloud Platform
  • Machine Learning
  • programming
Alisha Reyes
Alisha Reyes
— July 16, 2020

Blog Digest: Which Certifications Should I Get?, The 12 Microsoft Azure Certifications, 6 Ways to Prevent a Data Breach, and More

This month, we were excited to announce that Cloud Academy was recognized in the G2 Summer 2020 reports! These reports highlight the top-rated solutions in the industry, as chosen by the source that matters most: customers. We're grateful to have been nominated as a High Performer in se...

Read more
  • AWS
  • Azure
  • blog digest
  • Certifications
  • Cloud Academy
  • OWASP
  • OWASP Top 10
  • Security
  • VPCs
Avatar
Cloud Academy Team
— July 9, 2020

Which Certifications Should I Get?

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 companies. With all that in mind, the s...

Read more
  • AWS
  • Azure
  • Certifications
  • Cloud Computing
  • Google Cloud Platform
Alisha Reyes
Alisha Reyes
— July 2, 2020

New Content: AWS, Azure, Typescript, Java, Docker, 13 New Labs, and Much More

This month, our Content Team released a whopping 13 new labs in real cloud environments! If you haven't tried out our labs, you might not understand why we think that number is so impressive. Our labs are not “simulated” experiences — they are real cloud environments using accounts on A...

Read more
  • AWS
  • Azure
  • DevOps
  • Google Cloud Platform
  • Machine Learning
  • programming
Joe Nemer
Joe Nemer
— June 19, 2020

Kickstart Your Tech Training With a Free Week on Cloud Academy

Are you looking to make a jump in your technical career? Want to get trained or certified on AWS, Azure, Google Cloud Platform, DevOps, Kubernetes, Python, or another in-demand skill? Then you'll want to mark your calendar. Starting Monday, June 22 at 12:00 a.m. PDT (3:00 a.m. EDT), ...

Read more
  • AWS
  • Azure
  • cloud academy content
  • complimentary access
  • GCP
  • on the house
Alisha Reyes
Alisha Reyes
— June 11, 2020

New Content: AZ-500 and AZ-400 Updates, 3 Google Professional Exam Preps, Practical ML Learning Path, C# Programming, and More

This month, our Content Team released tons of new content and labs in real cloud environments. Not only that, but we introduced our very first highly interactive "Office Hours" webinar. This webinar, Acing the AWS Solutions Architect Associate Certification, started with a quick overvie...

Read more
  • AWS
  • Azure
  • DevOps
  • Google Cloud Platform
  • Machine Learning
  • programming
Rebecca Willis
Rebecca Willis
— June 3, 2020

Azure vs. AWS: Which Certification Provides the Brighter Future?

More and more companies are using cloud services, prompting more and more people to switch their current IT position to something cloud-related. The problem is most people only have that much time after work to learn new technologies, and there are plenty of cloud services that you can ...

Read more
  • AWS
  • Azure
  • certification
Alisha Reyes
Alisha Reyes
— June 2, 2020

Blog Digest: 5 Reasons to Get AWS Certified, OWASP Top 10, Getting Started with VPCs, Top 10 Soft Skills, and More

Thank you for being a valued member of our community! We recently sent out a short survey to understand what type of content you would like us to add to Cloud Academy, and we want to thank everyone who gave us their input. If you would like to complete the survey, it's not too late. It ...

Read more
  • AWS
  • Azure
  • blog digest
  • Certifications
  • Cloud Academy
  • OWASP
  • OWASP Top 10
  • Security
  • VPCs