是否有可能知道 sqs 消息被阅读了多少次

is it possible to know how many times sqs messsage has been read

我有一个用例可以知道在我的代码中读取了多少次 sqs 消息。

例如,我们从 SQS 读取消息,对于 abc reason/exception 我们无法处理该消息。现在,在可见性超时后队列中可以读取相同的消息。

这将产生无限循环。有没有办法知道特定 sqs 消息被读取并返回队列的次数。

我知道这可以通过死信队列来处理。因为这需要更多的努力,所以我正在检查是否还有其他选择

如果消息失败超过 x 次,我不想重试该消息,我想将其删除。在 SQS

中可以吗

调用 ReceiveMessage() 时,您可以指定要返回的 AttributeNames 列表。

其中一个属性是 ApproximateReceiveCount,其中 returns“所有队列都收到消息但未删除的次数”。

由于 SQS 的高度并行特性,它是一个 'approximate' 计数 -- 如果一条消息与此请求大约同时处理,则该计数可能会略有偏差。

可以通过查看邮件的 approximateReceiveCount 属性手动执行此操作,see this question on how to do so. You just need to implement the logic to read the count and decide whether to try processing the message or delete it. Note however that receiveCount is affected by more than just programmatically processing messages:

话虽这么说 DLQ 正是针对此用例 的预制解决方案。这不是很多额外的工作:您所要做的就是 create another SQS queue, set it as the DLQ of your processing queue, and set the number of retries。然后,DLQ 处理您所有的重新驱动逻辑, 而不是在 n 失败后删除消息,它们被移动到 DLQ,您可以在其中手动查看它们以了解原因他们失败了,在队列上设置指标警报,如果你想手动将消息重新驱动到你的处理队列中。或者只是忽略它们,直到它们根据其保留策略从队列中老化 - 重要的是 DLQ 为您提供 选项 能够在事后查看哪些消息失败,而完全删除它们则不会。