DEV Community

Data Tech Bridge
Data Tech Bridge

Posted on

Amazon SQS -Cheat Sheet

Amazon Simple Queue Service (SQS) is a fully managed message queuing service that enables you to decouple and scale microservices, distributed systems, and serverless applications. SQS offers two types of message queues: Standard and FIFO (First-In-First-Out).

Core Concepts of SQS

SQS is built around these fundamental components:

  • Queue: The container for messages
  • Message: The data being transported (up to 256KB)
  • Producer: Application that sends messages to the queue
  • Consumer: Application that processes messages from the queue
  • Visibility Timeout: Time during which a message is invisible after being received

SQS Features and Details

Feature Description Limits/Notes
Message Size Maximum size of a message 256KB
Extended Client Library For larger messages (up to 2GB) Stores message in S3, sends pointer
Message Retention Default retention period 4 days (configurable from 1 minute to 14 days)
Long Polling Reduces empty responses Wait time up to 20 seconds
Short Polling Immediate response May return empty responses
Visibility Timeout Default time 30 seconds (configurable up to 12 hours)
Dead Letter Queue For problematic messages After exceeding MaxReceiveCount
Encryption Server-side encryption SSE using KMS keys
Access Control Security mechanism IAM policies, SQS access policies
Message Deduplication For FIFO queues Content-based or provided ID
Message Groups For FIFO queues Ordered processing within groups
Throughput Standard queue Nearly unlimited TPS
Throughput FIFO queue 300 TPS (3,000 with batching)
Delay Queues Postpone delivery Up to 15 minutes
Cost Model Pay per request No minimum fee

Standard vs FIFO Queue Comparison

Feature Standard Queue FIFO Queue
Message Order Best-effort ordering Strict ordering
Delivery At-least-once delivery Exactly-once processing
Throughput Unlimited (with scaling) 300 TPS (3,000 with batching)
Use Case High throughput applications Applications requiring ordering
Deduplication Not supported natively Supported for 5-minute interval
Naming Any name Must end with .fifo suffix
Message Groups Not supported Supported for parallel processing
Batching Supported Supported (up to 10 messages)
Price Lower Higher

SQS Components and Architecture

                    +-------------------+
                    |     SQS Queue     |
                    +-------------------+
                    |                   |
+-------------+     |  +-----------+    |     +-------------+
|  Producer   | --> |  | Messages  | -- | --> |  Consumer   |
+-------------+     |  +-----------+    |     +-------------+
                    |                   |
                    +-------------------+
                         |         |
                         v         v
                  +-------+     +-------+
                  |  DLQ  |     |  SNS  |
                  +-------+     +-------+
Enter fullscreen mode Exit fullscreen mode

Important Points for the DEA-C01 Exam

  1. Message Lifecycle: Messages in SQS remain in the queue until a consumer processes and explicitly deletes them.

  2. Visibility Timeout: When a consumer receives a message, it becomes invisible to other consumers for the duration of the visibility timeout.

  3. Extending Visibility Timeout: Use ChangeMessageVisibility API to extend the visibility timeout if processing takes longer than expected.

  4. Dead Letter Queues (DLQ): Configure a DLQ to capture messages that can't be processed after a specified number of attempts.

  5. Long Polling vs Short Polling: Long polling reduces API calls and costs by waiting for messages to arrive, while short polling returns immediately.

  6. Message Attributes: SQS supports up to 10 metadata attributes per message for additional information.

  7. Delay Queues: Messages can be delayed up to 15 minutes before becoming visible to consumers.

  8. Message Retention: Messages are automatically deleted after the retention period (default 4 days, max 14 days).

  9. Batching: Send, receive, and delete operations can be batched up to 10 messages to improve throughput and reduce costs.

  10. FIFO Queue Throughput: FIFO queues support up to 300 transactions per second (TPS), or 3,000 TPS with batching.

  11. Message Deduplication: FIFO queues provide content-based deduplication or explicit deduplication IDs with a 5-minute deduplication interval.

  12. Message Groups: FIFO queues use message group IDs to maintain ordering within specific groups while allowing parallel processing.

  13. Cost Optimization: Use batch operations and long polling to reduce costs and improve efficiency.

  14. Queue Depth: Monitor queue depth to scale consumers appropriately and prevent message backlog.

  15. Encryption: SQS supports server-side encryption (SSE) using AWS KMS keys for data protection at rest.

  16. Access Control: Use IAM policies and SQS access policies to control access to queues.

  17. Cross-Account Access: SQS supports cross-account access through IAM policies and queue policies.

  18. Temporary Queues: Use the Temporary Queue Client to create short-lived request-response patterns.

  19. Queue Naming: FIFO queue names must end with .fifo suffix; standard queues have no naming restrictions.

  20. Throttling Implementation: SQS automatically handles throttling when you exceed service limits, with appropriate backoff strategies.

  21. Throughput Characteristics: Standard queues offer nearly unlimited throughput with at-least-once delivery semantics.

  22. Latency Characteristics: SQS typically provides single-digit millisecond latency for publishing and receiving messages.

  23. Replayability: SQS doesn't natively support message replay; implement replay patterns using message retention and separate queues.

  24. Rate Limits: To overcome rate limits, implement exponential backoff, use multiple queues, or batch operations.

  25. Data Ingestion Pipelines: SQS can serve as a buffer in data ingestion pipelines to handle traffic spikes and ensure smooth processing.

Example Calculation: Consumer Scaling

If your SQS queue receives 1,000 messages per minute, and each message takes 30 seconds to process:

Processing capacity needed = (1,000 messages/minute × 30 seconds/message) / 60 seconds/minute
                          = 500 concurrent processing capacity
Enter fullscreen mode Exit fullscreen mode

Therefore, you would need at least 500 concurrent consumers to keep up with the incoming message rate.

CloudWatch Metrics for SQS Monitoring

Metric Description Recommended Alarm
ApproximateNumberOfMessagesVisible Messages available for retrieval >1000 for extended periods
ApproximateAgeOfOldestMessage Age of oldest message in queue >5 minutes (application-dependent)
NumberOfMessagesReceived Rate of message retrieval Sudden drops
NumberOfMessagesSent Rate of message production Sudden spikes or drops
NumberOfEmptyReceives Empty receive requests High values indicate inefficient polling
ApproximateNumberOfMessagesNotVisible Messages being processed High values may indicate processing issues
NumberOfMessagesDeleted Rate of message deletion Should correlate with NumberOfMessagesReceived
SentMessageSize Size of messages sent Approaching 256KB limit

Implementing Throttling and Overcoming Rate Limits

When working with SQS, you may encounter service limits and throttling. Here are strategies to handle these situations:

  1. Implement Exponential Backoff: When receiving throttling errors, use exponential backoff to retry operations.

  2. Batch Operations: Use batch operations to send, receive, and delete messages in groups of up to 10.

  3. Distribute Load: Spread message processing across multiple queues to distribute the load.

  4. Request Rate Monitoring: Monitor your request rates using CloudWatch metrics to identify potential throttling issues.

  5. Queue Sharding: Implement queue sharding for high-throughput applications to distribute load across multiple queues.

Replayability of Data Ingestion Pipelines with SQS

SQS can be used in data ingestion pipelines to provide replayability through these patterns:

  1. Source-Preservation Pattern: Keep source data intact in S3 or another durable store.

  2. Multi-Queue Pattern: Use separate queues for different processing stages to enable partial replays.

  3. DLQ with Redrive: Configure Dead Letter Queues and implement redrive functionality to replay failed messages.

  4. Event Sourcing: Implement event sourcing patterns where all events are stored durably before processing.

  5. Checkpointing: Implement checkpointing in your consumers to track processing progress.

AWS GenAI LIVE image

How is generative AI increasing efficiency?

Join AWS GenAI LIVE! to find out how gen AI is reshaping productivity, streamlining processes, and driving innovation.

Learn more

Top comments (0)

Sentry image

See why 4M developers consider Sentry, “not bad.”

Fixing code doesn’t have to be the worst part of your day. Learn how Sentry can help.

Learn more

👋 Kindness is contagious

Please leave a ❤️ or a friendly comment on this post if you found it helpful!

Okay