php - how to handle SQS re-sending the same message to the requestor
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.
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.