AWS Certified Developer - Associate
Application Integrations
AWS SQS Overview
In this lesson, we explore the Amazon Simple Queue Service (SQS), a fully managed messaging service that decouples microservices to build scalable and resilient architectures. SQS is particularly useful when you need to buffer communications between components of distributed systems, smoothing out spikes in traffic and reducing bottlenecks.
Imagine an e-commerce application composed of various services. For instance, the cart service handles a user's shopping cart, and when a purchase is initiated, it sends a message to trigger a payment service. The payment service then invokes the invoice service, which generates an invoice. Other services like inventory, labeling, dispatch, and tracking take over thereafter to process and deliver the order. In such microservice architectures, running operations sequentially can become a performance bottleneck.
If a surge in user activity—such as during a holiday sale—overwhelms the payment service, the sequential design can lead to degraded performance and increased costs. SQS resolves this issue by acting as a message queue: once the purchase is initiated, the cart service sends a message to the queue and continues processing immediately without waiting for the payment service to respond.
When the payment service has the capacity, it retrieves the next message from the queue. This decoupling ensures that spikes in one service do not overwhelm others.
Key Features of SQS
SQS is designed to decouple components within distributed applications. Producers can send messages to the queue without knowing the specifics of the consumers, promoting flexible and loosely coupled system design. By buffering messages, SQS helps smooth out traffic bursts and supports asynchronous processing in serverless and event-driven architectures.
Terminology and Components
- Queue: Serves as a buffer that holds messages from producers until consumers are ready to process them.
- Messages: Units of communication up to 256 KB in size. They can include metadata as key-value pairs.
- Producers: Entities (such as EC2 instances, Lambda functions, or EventBridge events) that send messages to the queue.
- Consumers: Entities that poll and process messages from the queue.
- Dead Letter Queue: A secondary queue that stores messages which could not be processed successfully after multiple attempts.
- Configuration Options: Settings such as timeouts and message visibility that help prevent duplicate processing.
When using AWS Lambda functions as both producers and consumers, one function sends a message to the SQS queue, and another function later processes and deletes the message.
Queue Types: Standard vs. FIFO
SQS offers two types of queues: Standard and FIFO. Each type is suited to different use cases.
Standard Queues
Standard queues provide:
- Best-Effort Ordering: The order in which messages are sent might not be preserved.
- At-Least-Once Delivery: Messages can be delivered more than once.
- Unlimited Throughput: Ideal for high-volume applications.
For example, if messages 1, 2, and 3 are sent sequentially in a standard queue, they may arrive out of order, and duplicates might occur.
Both Standard and FIFO queues share several features:
- Maximum Retention Period: Up to 14 days.
- Maximum Message Size: 256 kilobytes.
- Low Latency: Ensuring rapid message processing.
FIFO Queues
FIFO (First-In-First-Out) queues are ideal for scenarios requiring:
- Strict Ordering: Messages are processed exactly in the order they are sent.
- Exactly-Once Processing: Each message is processed just once, preventing duplicates.
- Limited Throughput: Supports up to 3000 messages per second with batching or 300 messages per second without batching. Higher throughput modes are available with configuration.
- Message Grouping: Allows messages to be processed in parallel while maintaining order within each group.
- Message Deduplication: Eliminates duplicate messages based on a deduplication ID.
SQS Extended Client Library
For messages that exceed the standard 256 KB size limit, the Amazon SQS Extended Client Library provides a solution. When a message’s payload is too large, the library automatically uploads the content to an Amazon S3 bucket and sends a reference to that object via SQS. Consumers then retrieve the full message from S3 using the provided reference.
Key features include:
- Support for messages up to 2 GB by storing payloads in S3.
- Seamless integration with existing SQS workflows.
- Customizable options for S3 bucket policies, encryption, and lifecycle management.
- Backward compatibility with standard SQS clients.
- Awareness of potential cost and performance considerations due to S3 usage.
Integration with Auto Scaling
You can integrate SQS with EC2 Auto Scaling Groups to dynamically adjust processing capacity based on message volume. By configuring a CloudWatch alarm to monitor the "ApproximateNumberOfMessages" metric, you can automatically scale the number of consumer instances accordingly.
Access Policies
SQS supports fine-grained access policies to control which entities can publish or consume messages. For example, the following policy grants specific AWS accounts the permission to send messages to the queue:
{
"Version": "2012-10-17",
"Id": "QueuePolicy",
"Statement": [
{
"Sid": "Allow-SendMessage",
"Effect": "Allow",
"Principal": {
"AWS": [
"arn:aws:iam::111122223333:root",
"arn:aws:iam::444455556666:root"
]
},
"Action": "sqs:SendMessage",
"Resource": "arn:aws:sqs:us-east-1:123456789012:MyQueue"
}
]
}
Note
This configuration is particularly useful for enabling secure cross-account access to your SQS queues.
Summary
SQS is a fully managed messaging queuing service that enhances microservice architectures, distributed systems, and serverless applications. Key takeaways include:
- Producers send messages to a queue, and consumers process and delete them.
- Standard queues provide best-effort ordering with at-least-once delivery and unlimited throughput.
- FIFO queues ensure strict ordering, exactly-once processing, and support message grouping and deduplication.
- The SQS Extended Client Library allows handling of larger messages by leveraging Amazon S3.
- Access policies enable granular control over who can publish or consume messages.
Watch Video
Watch video content