Amazon SQS
Amazon Simple Queue Service (SQS) is a fully managed message queuing service that enables decoupling and scaling of microservices, distributed systems, and serverless applications.
Queue Types
| Type | Throughput | Ordering | Deduplication |
|---|---|---|---|
| Standard | Unlimited | Best-effort | At-least-once |
| FIFO | 3,000 msg/s | Guaranteed | Exactly-once |
Key Concepts
- Messages: Up to 256 KB of text data
- Visibility timeout: Time a message is hidden after being read
- Dead-letter queue: Destination for messages that can't be processed
- Long polling: Reduce empty responses and costs
Common Patterns
Decoupling Services
Web Server → SQS → Worker → Database
Load Leveling
Handle traffic spikes by queuing work for steady processing.
Fan-out with SNS
SNS Topic → [SQS Queue 1, SQS Queue 2]
Each queue processes independently.
Example: Processing Messages
const sqs = new AWS.SQS();
// Send
await sqs.sendMessage({
QueueUrl: 'https://sqs.us-east-1.amazonaws.com/123456789/my-queue',
MessageBody: JSON.stringify({ task: 'process_order', orderId: '12345' })
}).promise();
// Receive
const { Messages } = await sqs.receiveMessage({
QueueUrl: 'https://sqs.us-east-1.amazonaws.com/123456789/my-queue',
MaxNumberOfMessages: 10,
WaitTimeSeconds: 20 // Long polling
}).promise();
What We Like
- Reliability: Messages are stored redundantly across multiple AZs
- Scalability: Handles any volume without configuration
- Cost: Pay only for what you use, no minimum fees
- Lambda integration: Native trigger support for serverless processing
What We Don't Like
- 256 KB limit: Large payloads need S3 integration (Extended Client Library)
- Visibility timeout complexity: Requires careful tuning
- No message filtering: All consumers receive all messages (use SNS + SQS for filtering)
- FIFO limitations: Lower throughput, regional restrictions
Best Practices
- Use long polling to reduce costs and empty responses
- Set appropriate visibility timeouts based on processing time
- Configure dead-letter queues for failed message handling
- Use FIFO queues only when ordering is critical
- Enable server-side encryption for sensitive data