php - how to handle SQS re-sending the same message to the requestor

623

Has someone come up with an elegant way in PHP to recognize that SQS has just sent you the same message from the queue that it has sent previously? This could happen if the SQS has never received a 'deletemessage' request sent by the requestor because the original SQS queue could have gone down, so its replacement will resend the same message as per Amazon docs. Would appreciate any pointers, code samples in PHP etc.

673

Answer

Solution:

When you process a message you receive from SQS, there's a message id that is the same every time you receive the message, and a receipt handle (used to send the delete message) that changes each time. Saving the message ID along with the work you do as a result of the message allows your appplication to be aware that the work should not be repeated.

Of course, you need to set the visibility timeout on yoir queue high enough that you won't fail to delete each message or extend its invisibility before the timeout expires.

A recent addition to SQS is the ability to divert messages to a different queue -- they call it a "dead letter queue" -- once they've been delivered via the original queue a configurable number of times.

This seems like an easy enough workaround.

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

It's always possible (though I have not caught it happening) that SQS could still deliver the message more than once, because, being a massive distributed syatem, SQS is designed to deliver messages "at least" once, so you have to accommodate that possibility. Saving the message ID in a column in my database that has a unique constraint on it, which makes inserting a duplicate impossible.

Amazon SQS is engineered to provide “at least once” delivery of all messages in its queues. Although most of the time each message will be delivered to your application exactly once, you should design your system so that processing a message more than once does not create any errors or inconsistencies. 

https://aws.amazon.com/sqs/faqs/

People are also looking for solutions to the problem: Escape quotes on Javascript when variable is passed in from PHP

Source

Didn't find the answer?

Our community is visited by hundreds of web development professionals every day. Ask your question and get a quick answer for free.

Ask a Question

Write quick answer

Do you know the answer to this question? Write a quick response to it. With your help, we will make our community stronger.

Similar questions

Find the answer in similar questions on our website.