AWS
SQS
Message Delivery
Cloud Computing
Technology

SQS delivering a message only once

System Design practice on Codemia

Work through 120+ system design problems with detailed solutions, from rate limiters to multi-region storage.

Practice system design

Introduction

Amazon Simple Queue Service (SQS) is a fully managed message queuing service that enables the decoupling and scaling of microservices, distributed systems, and serverless applications. A common concern with any message queuing service is ensuring that messages are delivered reliably and, ideally, only once. In this article, we will delve into how SQS handles message delivery with a focus on ensuring that each message is delivered only once, exploring both the technical intricacies and using real-world examples to illustrate its application.

Understanding Amazon SQS

Amazon SQS offers two types of message queues:

  1. Standard Queues: This type delivers messages with at-least-once delivery and provides best-effort ordering.
  2. FIFO Queues: This type maintains strict message order and delivers messages exactly once.

We will focus on FIFO queues for their "exactly once" delivery guarantee and explore how SQS ensures this feature.

Exactly-Once Processing in FIFO Queues

Key Features and Mechanisms

  1. Deduplication: FIFO queues use content-based deduplication to prevent multiple deliveries of the same message. With a deduplication interval of 5 minutes, any message that shares a deduplication ID or has identical content as a previously processed message within this interval will not be delivered again.
  2. Message Grouping: FIFO queues manage messages in groups, ensuring ordered delivery and processing. Message Group IDs are used to maintain sequence within the same group, which is pivotal in systems where order is crucial.
  3. Automatic Visibility Timeout: After a consumer retrieves and processes a message, it needs to delete the message from the queue. If not deleted within the visibility timeout, the message becomes visible again and can be retrieved. By setting a reasonable visibility timeout, the system indirectly aids in the prevention of duplicate message processing.

Examples and Use Cases

Consider a financial transaction system using SQS FIFO queues for transaction logging. Each transaction message carries a unique transaction ID serving as its deduplication ID. When a transaction message is processed, its ID is stored, ensuring that any retry does not reprocess the transaction if it falls under the deduplication interval.

Challenges and Considerations

  • Deduplication ID Management: Properly managing deduplication IDs is crucial as it dictates the uniqueness of each message. An improper ID generation strategy could lead to unintended message duplication.
  • Performance: While FIFO queues ensure order and exact-once delivery, they are inherently slower than standard queues due to their additional logic for preserving order and ensuring uniqueness.
  • Error Handling and Retries: There must be careful management of message acknowledgments and retries to avoid message loss or duplication.

Comparing Standard and FIFO Queues

FeatureStandard QueueFIFO Queue
Delivery ModelAt-least-onceExactly-once delivery per deduplication interval
Message OrderBest-effort orderingStrict order within message groups
Use CaseHigh-throughput, unordered eventsOrder-sensitive tasks, transaction processing
DeduplicationNot supportedSupported with Deduplication ID or content-based
PerformanceHigher throughputLimited throughput due to ordering logic

Conclusion

Understanding how Amazon SQS handles message delivery is essential for implementing reliable messaging systems. While standard queues operate with at-least-once delivery, FIFO queues provide exactly-once processing by leveraging deduplication and strict order management. However, these benefits come with trade-offs related to throughput and complexity. Proper understanding and application of FIFO queues can greatly enhance the reliability and efficiency of critical applications requiring strict message processing integrity.

By taking advantage of SQS FIFO queues, developers can build robust, reliable messaging workflows that fit the needs of a variety of applications, from transaction processing to any other domain where message order and uniqueness are paramount.


Related reading
Course
Beginner
27 lessons
10 hours
System Design Fundamentals

Build a strong foundation in designing scalable, reliable distributed systems.

View the course
Track what you have practised

A free account saves your progress, solutions and study plan across every problem on Codemia.

System Design practice on Codemia

Work through 120+ system design problems with detailed solutions, from rate limiters to multi-region storage.

Practice system design

All Rights Reserved.