admin管理员组文章数量:1291319
I have an AWS lambda function that consumes data from an AWS SQS queue. If this lambda finds a problem when processing the data of a message, then this message has to be added in a dead letter queue.
The documentation I found is not clear about how can I make the lambda send the message to the Dead Letter Queue. How is that acplished?
Should I use the sendMessage() method, like I'd do to insert in a standard queue, or is there a better approach?
I have an AWS lambda function that consumes data from an AWS SQS queue. If this lambda finds a problem when processing the data of a message, then this message has to be added in a dead letter queue.
The documentation I found is not clear about how can I make the lambda send the message to the Dead Letter Queue. How is that acplished?
Should I use the sendMessage() method, like I'd do to insert in a standard queue, or is there a better approach?
Share Improve this question edited Jan 28, 2019 at 18:52 AriJnr asked Jan 28, 2019 at 18:46 AriJnrAriJnr 731 silver badge7 bronze badges 3- To the best of my knowledge, you cannot send a message to a DLQ. SQS does this, based on a policy that you define for problematic messages. if you want a manual DLQ then you could simply create a new queue purely for that purpose. – jarmod Commented Jan 28, 2019 at 19:15
- @jarmod - you can indeed send a message to it - see my answer below – Krease Commented Jan 31, 2019 at 6:17
- @Krease thanks for the correction. I’ll have to go try this out. – jarmod Commented Jan 31, 2019 at 13:17
2 Answers
Reset to default 8AWS will automatically send messages to your dead-letter-queue (DLQ) for you if receiveMessage
returns that message too many times (configurable on the queue with maxReceiveCount
property) - typically this happens if you receive a message, but don't delete it (if for example, you had some exception in processing it). This is the simplest way to use a DLQ - by letting AWS put messages there for you.
However, there's nothing wrong with manually sending a message to a DLQ. There's nothing special about it - it's just another queue - you can send and receive messages from it, or even give it its own DLQ!
Manually sending messages to a DLQ is useful in several scenarios, the simplest one being your case: when you know the message is broken (and want to save time trying to reprocess it). Another example is if you need to quickly burn through old items in your main queue but still save those messages for processing later - enabling you to catch up from backlog by processing more recent events first.
The key things to remember when manually sending a message to a DLQ are:
- Send the message to the queue FIRST
- Mark the message as consumed in the original queue (using
deleteMessage
) so AWS's automatic mechanisms don't put it there for you later.- if you delete the message from the original queue first, there is a small chance the message is lost (ie: if you crash or have an error before storing the message elsewhere)
You are not supposed to send messages to the dead letter queue, messages that fail to process too many times will get there on their own see here
The point is you get the message, fail on it, don't delete it, and after maxReceiveCount
times it will redrive it to the DLQ.
Note that you can simply send it to the DLQ (Hinted on by the documentation see where it says The NumberOfMessagesSent and NumberOfMessagesReceived for a Dead-Letter Queue Don't Match
) however it seems like an abuse, to me at least.
TLDR: You're not supposed to send it yourself, the queue needs to be configured with a DLQ and Amazon will do it for you after a set amount of failures.
本文标签:
版权声明:本文标题:javascript - How can a lambda function that consumes a SQS queue send the message to a Dead Letter Queue? - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1741507206a2382392.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论