如何在 FIFO SQS 队列中完成批处理?

问题描述 投票:0回答:2

假设我有一个 FIFO SQS 和一个 lambda,它消耗来自 FIFO SQS 的一批消息。如here所述,该批次的最大大小限制为 10。我想知道这将如何运作。

假设我们有一些消息组 ID G,因此我们有 5 个消息组 - G1、G2、...、G5。

假设我们将批量大小设置为 10。这意味着一次需要从每个组中提取多条消息。假设 G1 中有两条消息 M1 和 M2。 M1 先来,M2 再来。

但是由于我们在同一批次中获取这两条消息,因此它们可能会被无序处理。

这是正确的期望吗?或者 FIFO 队列只会在批次中放入 5 条消息,然后将其发送给 lambda?

amazon-web-services batch-processing aws-sqs-fifo
2个回答
2
投票

该批次将按顺序提供给 AWS Lambda 函数。

假设A/B/C代表群组,数字代表消息编号。

假设消息按以下顺序发送:

A1 B1 A2 C1 A3 D1 C2 B2 A4 D2 C3 B3

如果批量大小设置为 10,则 Lambda 函数将接收前 10 条消息:

A1 B1 A2 C1 A3 D1 C2 B2 A4 D2

它们将按批次按顺序提供,因此您的代码应按给定的顺序处理消息。

虽然批次包含 A 组的多个条目,但它们仍保持消息发送的顺序。

即使有额外的消息等待,也不会触发其他 Lambda 函数,因为每个 Group 中都有未处理的消息。但是,如果发送了消息

E1
,则 Lambda 函数将被
E1
触发,因为它可以随时处理,而无需等待较早的消息被处理。

初始 Lambda 函数完成运行后,将再次使用

C3 B3
调用该函数,因为这些组中较早的消息现已处理完毕。


0
投票

当批量大小大于 1 且有多个消费者时,SQS 将通过阻止来自包含正在运行的消息的消息组的排队消息来避免创建竞争条件。

这意味着以下内容:

If Group A and Group B have messages queued,
And Group A and Group B have messages in flight,
And Consumer A receives a batch of 10 messages from Group A and Group B,
And Consumer B receives a batch of 10 messages from Group B,
And Consumer A finishes its batch of messages from Group A and Group B,
And Consumer B is still processing its batch of messages from Consumer B,
Then then the next batch of messages that Consumer A receives will not 
contain messages from Group B.

详细解释在这里: https://aws.amazon.com/blogs/compute/solving-complex-ordering-challenges-with-amazon-sqs-fifo-queues/

旁注:将批量大小设置为 1 最终会产生瓶颈。

© www.soinside.com 2019 - 2024. All rights reserved.