AWS - SQS 批量大小和 Lambda 方法
AWS - SQS Batch Size and Lambda approach
如果我理解正确的话,Lambda 上的批量大小设置决定了从 SQS 一次扫描中接收多少消息。因此这个JSON(取自测试Lambda SQS);
{
"Records": [
{
"messageId": "19dd0b57-b21e-4ac1-bd88-01bbb068cb78",
"receiptHandle": "MessageReceiptHandle",
"body": "FAIL",
"attributes": {
"ApproximateReceiveCount": "1",
"SentTimestamp": "1523232000000",
"SenderId": "123456789012",
"ApproximateFirstReceiveTimestamp": "1523232000001"
},
"messageAttributes": {
},
"md5OfBody": "7b270e59b47ff90a553787216d55d91d",
"eventSource": "aws:sqs",
"eventSourceARN": "arn:aws:sqs:eu-west-1:123456789012:MyQueue",
"awsRegion": "eu-west-1"
}
]
}
有记录数组。如果我将批量大小设置为 5,那么如果 SQS 中有 5 条消息,它们将包含在数组中。如果有 10 条消息,那么 Lambda 将被调用两次,每个 Record 中有 5 条消息。
我现在有点困惑,不知道该采取什么方法。我的 Lambda 相当简单。这将是 Axios POST 向外部服务的请求。如果它出错了,我会抛出一个错误。我什至可以使用 axios-retry,让重试变得相当容易。
我应该使用批处理吗?天真地看着,我只需要 1 到 1。换句话说,消息到达。 Lambda 接受它。如果出错,稍后会自动重试。
相反,我将不得不遍历所有消息并尝试 Axios 请求。如果五条消息中的第三条失败怎么办,在这种情况下我会抛出一个错误并且 Lambda 停止。消息 4 和 5 会怎样?他们是否对 SQS 不满,然后又被再次执行?
all I need is 1 to 1. In other words, message arrives. Lambda takes
it. If it errors, it will be retried automatically a bit later.
我认为在上述情况下您不需要批处理。
What if the third of five messages fails, in that case I throw an
error and Lambda is stopped. What happens to messages four and five?
Are they resent to SQS and then again picked up for another execution?
根据 SQS visibility timeout and redrive policy 配置,如果您的 Lambda 出错,消息不会从队列中删除。 SQS会根据配置再次触发Lambda。如果您已配置 DLQ,则在达到 maxReceiveCount
后,失败的消息将添加到 DLQ 并从主队列中删除。
如果我理解正确的话,Lambda 上的批量大小设置决定了从 SQS 一次扫描中接收多少消息。因此这个JSON(取自测试Lambda SQS);
{
"Records": [
{
"messageId": "19dd0b57-b21e-4ac1-bd88-01bbb068cb78",
"receiptHandle": "MessageReceiptHandle",
"body": "FAIL",
"attributes": {
"ApproximateReceiveCount": "1",
"SentTimestamp": "1523232000000",
"SenderId": "123456789012",
"ApproximateFirstReceiveTimestamp": "1523232000001"
},
"messageAttributes": {
},
"md5OfBody": "7b270e59b47ff90a553787216d55d91d",
"eventSource": "aws:sqs",
"eventSourceARN": "arn:aws:sqs:eu-west-1:123456789012:MyQueue",
"awsRegion": "eu-west-1"
}
]
}
有记录数组。如果我将批量大小设置为 5,那么如果 SQS 中有 5 条消息,它们将包含在数组中。如果有 10 条消息,那么 Lambda 将被调用两次,每个 Record 中有 5 条消息。
我现在有点困惑,不知道该采取什么方法。我的 Lambda 相当简单。这将是 Axios POST 向外部服务的请求。如果它出错了,我会抛出一个错误。我什至可以使用 axios-retry,让重试变得相当容易。
我应该使用批处理吗?天真地看着,我只需要 1 到 1。换句话说,消息到达。 Lambda 接受它。如果出错,稍后会自动重试。
相反,我将不得不遍历所有消息并尝试 Axios 请求。如果五条消息中的第三条失败怎么办,在这种情况下我会抛出一个错误并且 Lambda 停止。消息 4 和 5 会怎样?他们是否对 SQS 不满,然后又被再次执行?
all I need is 1 to 1. In other words, message arrives. Lambda takes it. If it errors, it will be retried automatically a bit later.
我认为在上述情况下您不需要批处理。
What if the third of five messages fails, in that case I throw an error and Lambda is stopped. What happens to messages four and five? Are they resent to SQS and then again picked up for another execution?
根据 SQS visibility timeout and redrive policy 配置,如果您的 Lambda 出错,消息不会从队列中删除。 SQS会根据配置再次触发Lambda。如果您已配置 DLQ,则在达到 maxReceiveCount
后,失败的消息将添加到 DLQ 并从主队列中删除。