AWS:多个实例读取 SQS

AWS: multiple instances reading SQS

简单问题: 我想 运行 Amazon 上的一个自动缩放组,它会启动多个实例来处理来自 SQS 队列的消息。但是我怎么知道这些实例没有处理相同的消息呢?

我可以在处理完消息后将其从队列中删除。但是如果它还没有被删除并且仍然被一个实例处理,另一个实例可以下载相同的消息并处理它,在我看来。

您可以收到重复的消息,但只能收到“on rare occasions”。所以你应该以幂等性为目标。

除了 SQS 多次错误传递同一消息的可能性相当小(尽管不太可能,您仍然需要考虑),我怀疑您的问题源于对 SQS 的概念不熟悉共 "visibility timeout."

Immediately after the component receives the message, the message is still in the queue. However, you don't want other components in the system receiving and processing the message again. Therefore, Amazon SQS blocks them with a visibility timeout, which is a period of time during which Amazon SQS prevents other consuming components from receiving and processing that message.

http://docs.aws.amazon.com/AWSSimpleQueueService/latest/SQSDeveloperGuide/AboutVT.html

这就是防止多个队列运行器看到相同消息的原因。一旦可见性超时到期,消息将再次传递给队列消费者,除非您删除它,或者它超过配置的最大传递次数(此时它被删除或进入单独的死信队列,如果您配置了一个).如果作业花费的时间超过配置的可见性超时,您的消费者还可以向 SQS 发送请求以更改该单个消息的可见性超时。


更新:

由于最初写了这个答案,SQS 已经在一些 AWS 区域引入了 FIFO 队列。这些操作使用与上述相同的逻辑,但具有保证的按顺序传递和额外的安全措施以保证不会发生偶尔的重复消息传递。

FIFO (First-In-First-Out) queues are designed to enhance messaging between applications when the order of operations and events is critical, or where duplicates can't be tolerated. FIFO queues also provide exactly-once processing but are limited to 300 transactions per second (TPS).

http://docs.aws.amazon.com/AWSSimpleQueueService/latest/SQSDeveloperGuide/FIFO-queues.html

将应用程序切换到 FIFO 队列确实需要更改一些代码,并且需要创建一个新队列——现有队列无法切换到 FIFO。