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.
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:
- Standard Queues: This type delivers messages with at-least-once delivery and provides best-effort ordering.
- 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
- 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.
- 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.
- 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
| Feature | Standard Queue | FIFO Queue |
| Delivery Model | At-least-once | Exactly-once delivery per deduplication interval |
| Message Order | Best-effort ordering | Strict order within message groups |
| Use Case | High-throughput, unordered events | Order-sensitive tasks, transaction processing |
| Deduplication | Not supported | Supported with Deduplication ID or content-based |
| Performance | Higher throughput | Limited 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
- SSH EC2 asking for password
- SSH fingerprint verification for Amazon AWS EC2 server with ECDSA?
- SSH into Kubernetes cluster running on Amazon
- SSH to Amazon EC2 instance using PuTTY in Windows
- SSH to Elastic Beanstalk instance
- SSL CERTIFICATE_VERIFY_FAILED in aws cli
- Starting minikube in ec2 shows X Sorry, Kubernetes v1.18.0 requires conntrack to be installed in root''s path
- StatefulSets vs Deployments for stateless applications on Kubernetes

System Design Fundamentals
Build a strong foundation in designing scalable, reliable distributed systems.
View the courseTrack 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.