sqs
Compare original and translation side by side
🇺🇸
Original
English🇨🇳
Translation
ChineseAmazon SQS Core Knowledge
Amazon SQS 核心知识
Full Reference: See advanced.md for Java/Python/Go producers, Spring Cloud AWS consumers, Lambda integration, IAM policies, and CloudWatch monitoring.
Deep Knowledge: Usewith technology:mcp__documentation__fetch_docsfor comprehensive documentation.sqs
完整参考:查看 advanced.md 了解 Java/Python/Go 生产者、Spring Cloud AWS 消费者、Lambda 集成、IAM 策略、CloudWatch 监控相关内容。
深度知识:使用指定 technology 为mcp__documentation__fetch_docs获取完整文档。sqs
Quick Start (LocalStack)
快速开始(LocalStack)
yaml
undefinedyaml
undefineddocker-compose.yml
docker-compose.yml
services:
localstack:
image: localstack/localstack
ports:
- "4566:4566"
environment:
- SERVICES=sqs
- DEFAULT_REGION=us-east-1
volumes:
- localstack_data:/var/lib/localstack
volumes:
localstack_data:
```bashservices:
localstack:
image: localstack/localstack
ports:
- "4566:4566"
environment:
- SERVICES=sqs
- DEFAULT_REGION=us-east-1
volumes:
- localstack_data:/var/lib/localstack
volumes:
localstack_data:
```bashCreate queue
Create queue
aws --endpoint-url=http://localhost:4566 sqs create-queue
--queue-name orders-queue
--queue-name orders-queue
aws --endpoint-url=http://localhost:4566 sqs create-queue
--queue-name orders-queue
--queue-name orders-queue
Create FIFO queue
Create FIFO queue
aws --endpoint-url=http://localhost:4566 sqs create-queue
--queue-name orders-queue.fifo
--attributes FifoQueue=true,ContentBasedDeduplication=true
--queue-name orders-queue.fifo
--attributes FifoQueue=true,ContentBasedDeduplication=true
undefinedaws --endpoint-url=http://localhost:4566 sqs create-queue
--queue-name orders-queue.fifo
--attributes FifoQueue=true,ContentBasedDeduplication=true
--queue-name orders-queue.fifo
--attributes FifoQueue=true,ContentBasedDeduplication=true
undefinedCore Concepts
核心概念
| Concept | Description |
|---|---|
| Standard Queue | At-least-once, best-effort ordering |
| FIFO Queue | Exactly-once, strict ordering |
| Visibility Timeout | Message lock period |
| Dead Letter Queue | Failed message destination |
| Long Polling | Efficient message retrieval |
| Message Groups | FIFO ordering within group |
| 概念 | 描述 |
|---|---|
| Standard Queue | 至少一次投递,尽力排序 |
| FIFO Queue | 恰好一次投递,严格排序 |
| Visibility Timeout | 消息锁定周期 |
| Dead Letter Queue | 失败消息的存储目的地 |
| Long Polling | 高效的消息检索方式 |
| Message Groups | FIFO 队列中组内严格排序 |
Queue Types Comparison
队列类型对比
| Feature | Standard | FIFO |
|---|---|---|
| Throughput | Unlimited | 300 msg/s (3000 with batching) |
| Ordering | Best-effort | Strict (per group) |
| Delivery | At-least-once | Exactly-once |
| Deduplication | None | 5-minute window |
| 特性 | 标准队列 | FIFO 队列 |
|---|---|---|
| 吞吐量 | 无限制 | 300 条/秒(批量发送可达 3000 条/秒) |
| 排序能力 | 尽力排序 | 严格排序(组维度) |
| 投递模式 | 至少一次投递 | 恰好一次投递 |
| 去重能力 | 无 | 5 分钟窗口去重 |
Producer Pattern (Node.js)
生产者实现(Node.js)
typescript
import { SQSClient, SendMessageCommand, SendMessageBatchCommand } from '@aws-sdk/client-sqs';
const client = new SQSClient({
region: 'us-east-1',
// For LocalStack: endpoint: 'http://localhost:4566',
});
const queueUrl = 'https://sqs.us-east-1.amazonaws.com/123456789/orders-queue';
// Send single message
await client.send(new SendMessageCommand({
QueueUrl: queueUrl,
MessageBody: JSON.stringify(order),
MessageAttributes: {
'OrderType': {
DataType: 'String',
StringValue: order.type,
},
'CorrelationId': {
DataType: 'String',
StringValue: correlationId,
},
},
DelaySeconds: 0,
}));
// Send batch (up to 10)
await client.send(new SendMessageBatchCommand({
QueueUrl: queueUrl,
Entries: orders.map((order, index) => ({
Id: `msg-${index}`,
MessageBody: JSON.stringify(order),
MessageAttributes: {
'OrderType': { DataType: 'String', StringValue: order.type },
},
})),
}));
// FIFO queue
const fifoQueueUrl = 'https://sqs.us-east-1.amazonaws.com/123456789/orders-queue.fifo';
await client.send(new SendMessageCommand({
QueueUrl: fifoQueueUrl,
MessageBody: JSON.stringify(order),
MessageGroupId: order.customerId, // Required for FIFO
MessageDeduplicationId: order.orderId, // Or use ContentBasedDeduplication
}));typescript
import { SQSClient, SendMessageCommand, SendMessageBatchCommand } from '@aws-sdk/client-sqs';
const client = new SQSClient({
region: 'us-east-1',
// For LocalStack: endpoint: 'http://localhost:4566',
});
const queueUrl = 'https://sqs.us-east-1.amazonaws.com/123456789/orders-queue';
// Send single message
await client.send(new SendMessageCommand({
QueueUrl: queueUrl,
MessageBody: JSON.stringify(order),
MessageAttributes: {
'OrderType': {
DataType: 'String',
StringValue: order.type,
},
'CorrelationId': {
DataType: 'String',
StringValue: correlationId,
},
},
DelaySeconds: 0,
}));
// Send batch (up to 10)
await client.send(new SendMessageBatchCommand({
QueueUrl: queueUrl,
Entries: orders.map((order, index) => ({
Id: `msg-${index}`,
MessageBody: JSON.stringify(order),
MessageAttributes: {
'OrderType': { DataType: 'String', StringValue: order.type },
},
})),
}));
// FIFO queue
const fifoQueueUrl = 'https://sqs.us-east-1.amazonaws.com/123456789/orders-queue.fifo';
await client.send(new SendMessageCommand({
QueueUrl: fifoQueueUrl,
MessageBody: JSON.stringify(order),
MessageGroupId: order.customerId, // Required for FIFO
MessageDeduplicationId: order.orderId, // Or use ContentBasedDeduplication
}));Consumer Pattern (Node.js)
消费者实现(Node.js)
typescript
import { SQSClient, ReceiveMessageCommand, DeleteMessageCommand } from '@aws-sdk/client-sqs';
const client = new SQSClient({ region: 'us-east-1' });
async function pollMessages() {
while (true) {
const response = await client.send(new ReceiveMessageCommand({
QueueUrl: queueUrl,
MaxNumberOfMessages: 10,
WaitTimeSeconds: 20, // Long polling
VisibilityTimeout: 30,
MessageAttributeNames: ['All'],
AttributeNames: ['All'],
}));
if (!response.Messages) continue;
for (const message of response.Messages) {
try {
const order = JSON.parse(message.Body!);
await processOrder(order);
// Delete on success
await client.send(new DeleteMessageCommand({
QueueUrl: queueUrl,
ReceiptHandle: message.ReceiptHandle!,
}));
} catch (error) {
// Message will return to queue after visibility timeout
console.error('Processing failed:', error);
}
}
}
}typescript
import { SQSClient, ReceiveMessageCommand, DeleteMessageCommand } from '@aws-sdk/client-sqs';
const client = new SQSClient({ region: 'us-east-1' });
async function pollMessages() {
while (true) {
const response = await client.send(new ReceiveMessageCommand({
QueueUrl: queueUrl,
MaxNumberOfMessages: 10,
WaitTimeSeconds: 20, // Long polling
VisibilityTimeout: 30,
MessageAttributeNames: ['All'],
AttributeNames: ['All'],
}));
if (!response.Messages) continue;
for (const message of response.Messages) {
try {
const order = JSON.parse(message.Body!);
await processOrder(order);
// Delete on success
await client.send(new DeleteMessageCommand({
QueueUrl: queueUrl,
ReceiptHandle: message.ReceiptHandle!,
}));
} catch (error) {
// Message will return to queue after visibility timeout
console.error('Processing failed:', error);
}
}
}
}Dead Letter Queue
死信队列配置
typescript
// Create DLQ
await client.send(new CreateQueueCommand({
QueueName: 'orders-dlq',
}));
// Main queue with DLQ
await client.send(new CreateQueueCommand({
QueueName: 'orders-queue',
Attributes: {
RedrivePolicy: JSON.stringify({
deadLetterTargetArn: 'arn:aws:sqs:us-east-1:123456789:orders-dlq',
maxReceiveCount: '3',
}),
VisibilityTimeout: '30',
MessageRetentionPeriod: '1209600', // 14 days
},
}));hcl
undefinedtypescript
// Create DLQ
await client.send(new CreateQueueCommand({
QueueName: 'orders-dlq',
}));
// Main queue with DLQ
await client.send(new CreateQueueCommand({
QueueName: 'orders-queue',
Attributes: {
RedrivePolicy: JSON.stringify({
deadLetterTargetArn: 'arn:aws:sqs:us-east-1:123456789:orders-dlq',
maxReceiveCount: '3',
}),
VisibilityTimeout: '30',
MessageRetentionPeriod: '1209600', // 14 days
},
}));hcl
undefinedTerraform
Terraform
resource "aws_sqs_queue" "orders_dlq" {
name = "orders-dlq"
message_retention_seconds = 1209600
}
resource "aws_sqs_queue" "orders" {
name = "orders-queue"
visibility_timeout_seconds = 30
message_retention_seconds = 1209600
redrive_policy = jsonencode({
deadLetterTargetArn = aws_sqs_queue.orders_dlq.arn
maxReceiveCount = 3
})
}
undefinedresource "aws_sqs_queue" "orders_dlq" {
name = "orders-dlq"
message_retention_seconds = 1209600
}
resource "aws_sqs_queue" "orders" {
name = "orders-queue"
visibility_timeout_seconds = 30
message_retention_seconds = 1209600
redrive_policy = jsonencode({
deadLetterTargetArn = aws_sqs_queue.orders_dlq.arn
maxReceiveCount = 3
})
}
undefinedLambda Integration
Lambda 集成
typescript
// Lambda handler
export const handler = async (event: SQSEvent): Promise<SQSBatchResponse> => {
const batchItemFailures: SQSBatchItemFailure[] = [];
for (const record of event.Records) {
try {
const order = JSON.parse(record.body);
await processOrder(order);
} catch (error) {
// Report partial batch failure
batchItemFailures.push({ itemIdentifier: record.messageId });
}
}
return { batchItemFailures };
};yaml
undefinedtypescript
// Lambda handler
export const handler = async (event: SQSEvent): Promise<SQSBatchResponse> => {
const batchItemFailures: SQSBatchItemFailure[] = [];
for (const record of event.Records) {
try {
const order = JSON.parse(record.body);
await processOrder(order);
} catch (error) {
// Report partial batch failure
batchItemFailures.push({ itemIdentifier: record.messageId });
}
}
return { batchItemFailures };
};yaml
undefinedserverless.yml
serverless.yml
functions:
orderProcessor:
handler: handler.handler
events:
- sqs:
arn: !GetAtt OrdersQueue.Arn
batchSize: 10
functionResponseType: ReportBatchItemFailures
undefinedfunctions:
orderProcessor:
handler: handler.handler
events:
- sqs:
arn: !GetAtt OrdersQueue.Arn
batchSize: 10
functionResponseType: ReportBatchItemFailures
undefinedWhen NOT to Use This Skill
不适用场景
Use alternative messaging solutions when:
- Event streaming with replay - Use Kinesis or Kafka
- Cross-cloud or hybrid cloud - Use Kafka, RabbitMQ, or Pulsar
- Complex routing patterns - RabbitMQ provides better routing
- Message ordering across all messages - FIFO queues have throughput limits
- Real-time low latency (<10ms) - Use Redis or NATS
- On-premise deployment - Use RabbitMQ or ActiveMQ
- Message size >256KB - Use S3 with SQS Extended Client
遇到以下情况时请选择替代消息方案:
- 需要可回放的事件流 - 使用 Kinesis 或 Kafka
- 跨云或混合云部署 - 使用 Kafka、RabbitMQ 或 Pulsar
- 复杂路由需求 - RabbitMQ 提供更完善的路由能力
- 全量消息全局排序 - FIFO 队列存在吞吐量限制
- 超低延迟实时场景(<10ms) - 使用 Redis 或 NATS
- 本地部署场景 - 使用 RabbitMQ 或 ActiveMQ
- 消息大小超过 256KB - 搭配 SQS Extended Client 使用 S3 存储大消息
Anti-Patterns
反模式说明
| Anti-Pattern | Why It's Bad | Solution |
|---|---|---|
| Short polling (WaitTime=0) | Wasteful API calls, higher cost | Use long polling (WaitTimeSeconds=20) |
| No DLQ configured | Failed messages lost | Always configure dead letter queue |
| Visibility timeout too short | Duplicate processing | Set timeout > max processing time |
| Visibility timeout too long | Slow failure recovery | Balance with processing time |
| Processing before delete | Message reprocessed on crash | Delete only after successful processing |
| FIFO for high throughput | Limited to 300 msg/s | Use Standard queue if ordering not critical |
| No batching | Higher latency and cost | Batch up to 10 messages per request |
| Polling in Lambda | Wasted invocations | Use Lambda event source mapping |
| No IAM policies | Security risk | Apply least privilege IAM policies |
| 反模式 | 缺陷 | 解决方案 |
|---|---|---|
| 短轮询(WaitTime=0) | API 调用冗余、成本更高 | 使用长轮询(WaitTimeSeconds=20) |
| 未配置死信队列 | 失败消息会丢失 | 始终配置死信队列 |
| 可见性超时设置过短 | 重复处理消息 | 超时时间设置为大于最大处理耗时 |
| 可见性超时设置过长 | 故障恢复速度慢 | 结合处理耗时合理配置 |
| 处理消息前先删除 | 程序崩溃时消息会被重复处理 | 仅在处理成功后删除消息 |
| 高吞吐量场景使用 FIFO 队列 | 吞吐量上限仅 300 条/秒 | 非强排序需求使用标准队列 |
| 不使用批量发送 | 延迟高、成本高 | 单请求最多批量发送 10 条消息 |
| Lambda 中自行轮询队列 | 浪费函数调用资源 | 使用 Lambda 事件源映射能力 |
| 未配置 IAM 策略 | 存在安全风险 | 遵循最小权限原则配置 IAM 策略 |
Quick Troubleshooting
快速排障指南
| Issue | Likely Cause | Fix |
|---|---|---|
| Messages not appearing | Wrong queue URL or permissions | Verify URL and IAM permissions |
| Duplicate messages | Standard queue behavior or visibility timeout | Implement idempotent processing |
| Messages delayed | Delay seconds set or queue backlog | Check DelaySeconds, scale consumers |
| Messages in DLQ | Max receives exceeded | Check processing logic, increase retries |
| Visibility timeout errors | Message processing too slow | Extend visibility timeout |
| Throughput limited | FIFO queue limit | Use Standard queue or batch messages |
| High costs | Short polling or frequent sends | Use long polling, batch operations |
| Access denied | Missing IAM permissions | Add sqs:SendMessage/ReceiveMessage |
| Message size limit | Payload >256KB | Use SQS Extended Client with S3 |
| 问题 | 可能原因 | 修复方案 |
|---|---|---|
| 消息未出现在队列中 | 队列 URL 错误或权限不足 | 校验队列 URL 和 IAM 权限 |
| 消息重复投递 | 标准队列默认特性或可见性超时过短 | 实现消费幂等逻辑 |
| 消息投递延迟 | 设置了延迟时间或队列存在积压 | 检查 DelaySeconds 配置,扩容消费者 |
| 消息进入死信队列 | 超过最大消费次数 | 检查消费逻辑,调整重试次数 |
| 可见性超时错误 | 消息处理耗时过长 | 延长可见性超时时间 |
| 吞吐量受限 | 达到 FIFO 队列上限 | 切换为标准队列或使用批量发送 |
| 使用成本过高 | 短轮询或高频单次发送 | 使用长轮询、批量操作 |
| 访问被拒绝 | 缺少 IAM 权限 | 新增 sqs:SendMessage/ReceiveMessage 权限 |
| 消息大小超限 | 负载超过 256KB | 搭配 S3 使用 SQS Extended Client |
Production Checklist
生产环境检查清单
- IAM policies with least privilege
- Server-side encryption enabled
- Dead letter queue configured
- Visibility timeout set appropriately
- Long polling enabled (20s)
- Message retention configured
- CloudWatch alarms set up
- DLQ monitoring alerts
- VPC endpoints (if needed)
- Access logging enabled
- 配置了最小权限的 IAM 策略
- 开启了服务端加密
- 配置了死信队列
- 可见性超时配置合理
- 开启了长轮询(20s)
- 配置了消息保留周期
- 配置了 CloudWatch 告警
- 配置了死信队列监控告警
- 按需配置了 VPC 端点
- 开启了访问日志
Key Metrics to Monitor
核心监控指标
| Metric | Alert Threshold |
|---|---|
| ApproximateNumberOfMessagesVisible | > 10000 |
| ApproximateAgeOfOldestMessage | > 3600s |
| NumberOfMessagesReceived | Anomaly |
| NumberOfMessagesSent | Anomaly |
| ApproximateNumberOfMessagesNotVisible | > expected |
| 指标 | 告警阈值 |
|---|---|
| 队列中待消费消息数(ApproximateNumberOfMessagesVisible) | > 10000 |
| 最旧消息留存时间(ApproximateAgeOfOldestMessage) | > 3600s |
| 消费消息数(NumberOfMessagesReceived) | 异常波动 |
| 发送消息数(NumberOfMessagesSent) | 异常波动 |
| 处理中消息数(ApproximateNumberOfMessagesNotVisible) | 超过预期值 |
Reference Documentation
参考文档
Deep Knowledge: Usewith technology:mcp__documentation__fetch_docsfor comprehensive documentation.sqs
Available topics: , , , ,
basicsproducersconsumersdlqproduction深度知识:使用指定 technology 为mcp__documentation__fetch_docs获取完整文档。sqs
可用主题:、、、、
basicsproducersconsumersdlqproduction