Hello,
Something I often stumble on is the question "Why shall I use SQS FIFO?" and "How shall we implement this?".
I thought in this short tutorial that I will explain a couple of use cases and how to implement a FIFO queue.
First of all, what is a SQS FIFO?
FIFO stands for First-In First-Out and is about the ordering of messages in the queue. This is mainly used when the order of operations and events is critical, or where duplicates can't be tolerated.
As shown above the messages are sent in the order 1, 2, 3, 4 and are therefore consumed in the order 1, 2, 3, 4.
Examples of use cases
- Make sure that user-entered commands are run in the right order
- Sending notifications to a customer where order is critical
- Integrating with a third-party system where events need to be processed in-order
Some good points to think of
- Limited throughput: 300 msg/second without batching, 3000 msg/second with batching
- Exactly-once send capability (by removing duplicates)
- Messages are processed in order by the consumer
- Name of queue must end with .fifo
How to set up a SQS FIFO queue
- First go to the AWS Console and search for SQS and select "Create queue". In here select FIFO and make sure to use the extension .fifo for your queue name as this is mandatory
- Next, you can add the desired settings as you want. I can mention one setting that is good to know about and that is the "Content-based deduplication" - this is used if the same message is sent within an interval of 5 minutes. You can read more about this settings at the AWS documentation
- Next, click create queue
Great, we do now have our queue. Let's try it out!
Try our new queue SQS FIFO queue
Click on "Send and receive messages" (top right).
Now you can for example add the following:
- Message body: Message 1
- Message group ID: Tutorial
- Message deduplication ID: 1
Click "Send message"
Now add a second message and click "Send message"
- Message body: Message 2
- Message group ID: Tutorial
- Message deduplication ID: 2
Now add a third message and click "Send message"
- Message body: Message 3
- Message group ID: Tutorial
- Message deduplication ID: 3
Now add a fourth message and click "Send message"
- Message body: Message 4
- Message group ID: Tutorial
- Message deduplication ID: 4
Now we have sent four messages in the following order and these are now ready to be received:
- Message 1
- Message 2
- Message 3
- Message 4
If you now click "Poll for messages" you will get a list of all the four messages.
If you click on the bottom one and then select "Body" you'll get the first message saying "Message 1", the second one from the bottom the second message etc. We have now guaranteed the receive order from how we sent them.
That's it, hope you found this useful and know how and when to use a FIFO queue.
Top comments (0)