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
— January 6, 2020

New on Cloud Academy: Red Hat, Agile, OWASP Labs, Amazon SageMaker Lab, Linux Command Line Lab, SQL, Git Labs, Scrum Master, Azure Architects Lab, and Much More

Happy New Year! We hope you're ready to kick your training in overdrive in 2020 because we have a ton of new content for you. Not only do we have a bunch of new courses, hands-on labs, and lab challenges on AWS, Azure, and Google Cloud, but we also have three new courses on Red Hat, th...

Read more
  • agile
  • AWS
  • Azure
  • Google Cloud Platform
  • Linux
  • OWASP
  • programming
  • red hat
  • scrum
Alisha Reyes
Alisha Reyes
— December 24, 2019

Cloud Academy’s Blog Digest: Azure Best Practices, 6 Reasons You Should Get AWS Certified, Google Cloud Certification Prep, and more

Happy Holidays from Cloud Academy We hope you have a wonderful holiday season filled with family, friends, and plenty of food. Here at Cloud Academy, we are thankful for our amazing customer like you.  Since this time of year can be stressful, we’re sharing a few of our latest article...

Read more
  • AWS
  • azure best practices
  • blog digest
  • Cloud Academy
  • Google Cloud
Avatar
Guy Hummel
— December 12, 2019

Google Cloud Platform Certification: Preparation and Prerequisites

Google Cloud Platform (GCP) has evolved from being a niche player to a serious competitor to Amazon Web Services and Microsoft Azure. In 2019, research firm Gartner placed Google in the Leaders quadrant in its Magic Quadrant for Cloud Infrastructure as a Service for the second consecuti...

Read more
  • AWS
  • Azure
  • Google Cloud Platform
Alisha Reyes
Alisha Reyes
— December 10, 2019

New Lab Challenges: Push Your Skills to the Next Level

Build hands-on experience using real accounts on AWS, Azure, Google Cloud Platform, and more Meaningful cloud skills require more than book knowledge. Hands-on experience is required to translate knowledge into real-world results. We see this time and time again in studies about how pe...

Read more
  • AWS
  • Azure
  • Google Cloud
  • hands-on
  • labs
Alisha Reyes
Alisha Reyes
— December 5, 2019

New on Cloud Academy: AWS Solution Architect Lab Challenge, Azure Hands-on Labs, Foundation Certificate in Cyber Security, and Much More

Now that Thanksgiving is over and the craziness of Black Friday has died down, it's now time for the busiest season of the year. Whether you're a last-minute shopper or you already have your shopping done, the holidays bring so much more excitement than any other time of year. Since our...

Read more
  • AWS
  • AWS solution architect
  • AZ-203
  • Azure
  • cyber security
  • FCCS
  • Foundation Certificate in Cyber Security
  • Google Cloud Platform
  • Kubernetes
Avatar
Cloud Academy Team
— December 4, 2019

Understanding Enterprise Cloud Migration

What is enterprise cloud migration? Cloud migration is about moving your data, applications, and even infrastructure from your on-premises computers or infrastructure to a virtual pool of on-demand, shared resources that offer compute, storage, and network services at scale. Why d...

Read more
  • AWS
  • Azure
  • Data Migration
Wendy Dessler
Wendy Dessler
— November 27, 2019

6 Reasons Why You Should Get an AWS Certification This Year

In the past decade, the rise of cloud computing has been undeniable. Businesses of all sizes are moving their infrastructure and applications to the cloud. This is partly because the cloud allows businesses and their employees to access important information from just about anywhere. ...

Read more
  • AWS
  • Certifications
  • certified
Avatar
Andrea Colangelo
— November 26, 2019

AWS Regions and Availability Zones: The Simplest Explanation You Will Ever Find Around

The basics of AWS Regions and Availability Zones We’re going to treat this article as a sort of AWS 101 — it’ll be a quick primer on AWS Regions and Availability Zones that will be useful for understanding the basics of how AWS infrastructure is organized. We’ll define each section,...

Read more
  • AWS
Avatar
Dzenan Dzevlan
— November 20, 2019

Application Load Balancer vs. Classic Load Balancer

What is an Elastic Load Balancer? This post covers basics of what an Elastic Load Balancer is, and two of its examples: Application Load Balancers and Classic Load Balancers. For additional information — including a comparison that explains Network Load Balancers — check out our post o...

Read more
  • ALB
  • Application Load Balancer
  • AWS
  • Elastic Load Balancer
  • ELB
Albert Qian
Albert Qian
— November 13, 2019

Advantages and Disadvantages of Microservices Architecture

What are microservices? Let's start our discussion by setting a foundation of what microservices are. Microservices are a way of breaking large software projects into loosely coupled modules, which communicate with each other through simple Application Programming Interfaces (APIs). ...

Read more
  • AWS
  • Docker
  • Kubernetes
  • Microservices
Nisar Ahmad
Nisar Ahmad
— November 12, 2019

Kubernetes Services: AWS vs. Azure vs. Google Cloud

Kubernetes is a popular open-source container orchestration platform that allows us to deploy and manage multi-container applications at scale. Businesses are rapidly adopting this revolutionary technology to modernize their applications. Cloud service providers — such as Amazon Web Ser...

Read more
  • AWS
  • Azure
  • Google Cloud
  • Kubernetes
Avatar
Stuart Scott
— October 31, 2019

AWS Internet of Things (IoT): The 3 Services You Need to Know

The Internet of Things (IoT) embeds technology into any physical thing to enable never-before-seen levels of connectivity. IoT is revolutionizing industries and creating many new market opportunities. Cloud services play an important role in enabling deployment of IoT solutions that min...

Read more
  • AWS
  • AWS IoT Events
  • AWS IoT SiteWise
  • AWS IoT Things Graph
  • IoT