Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Deleting messages from SQS queue after processing

My application sends messages to a AWS SQS queue for jobs that need some sort of background processing. My processing daemon receives and processes the messages like this:

$result = $sqsClient->receiveMessage([
    'QueueUrl' => \Myapp\Config::get('sqs.queue'),
    'WaitTimeSeconds' => 20,
    'MaxNumberOfMessages' => 1
]);

if (isset($result['Messages'])) {
    foreach ($result->getPath('Messages/*/Body') as $messageBody) {
        $handler = new \Myapp\Handler();
        $handler->dispatch($messageBody);
    }
}

This works fine, however I can see from the SQS console that the messages that my script retrieves are put into the "Messages in Flight" category, and then after a while they're put back into "Messages Available", and my script ends up picking them up again.

How can I remove the messages from the SQS queue, or even better mark them as completed?

like image 833
John Dorean Avatar asked Feb 22 '15 00:02

John Dorean


People also ask

When should I delete SQS message?

When you receive a message from an SQS queue, the message will (by default) return to the queue 30 seconds later. This is to handle cases where processing of the message crashes and the message needs to be processed again. Once your message is successfully processed, use deleteMessage to delete the message.

Does Lambda delete SQS message after processing?

messages will be deleted from SQS immediately after the successful execution of a batch by lambda.

How long can a message be retained in an SQS queue?

You can configure the Amazon SQS message retention period to a value from 1 minute to 14 days. The default is 4 days. Once the message retention quota is reached, your messages are automatically deleted.

What does purge in SQS mean?

If you don't want to delete an Amazon SQS queue but need to delete all of the messages from it, purge the queue. The message deletion process takes up to 60 seconds. We recommend waiting for 60 seconds regardless of your queue's size. Important. When you purge a queue, you can't retrieve any of the deleted messages.


2 Answers

When you receive a message from an SQS queue, the message will (by default) return to the queue 30 seconds later. This is to handle cases where processing of the message crashes and the message needs to be processed again.

Once your message is successfully processed, use deleteMessage to delete the message. You'll need the receiptHandle value from the message when you received it from receiveMessage in order to delete the message.

If typical processing of your message may take more than 30 seconds, then you can configure your queue to increase that "return to queue" time. This is called "Default Visibility Timeout" in the SQS queue configuration.

Also be aware that Amazon SQS works in such a way that:

  1. messages may be received out-of-order compared to how they were added to the queue
  2. messages may be received twice, so allow your message processor to handle these cases
like image 150
Matt Houser Avatar answered Oct 06 '22 00:10

Matt Houser


You do need to delete messages after you process them - that would be considered 'marking them completed'; however, if something downstream needs to act on this 'completed'' action, its not uncommon for me to post a 'completion' message to another SQS queue, and then delete the message from the incoming queue. In this way another process, either now or down the road can take action that needs to be done when the first worker completes its work, i.e. chaining those processes together in a decoupled manner.

If there is nothing downstream that needs to be done, simply deleting it is enough.

like image 35
E.J. Brennan Avatar answered Oct 05 '22 23:10

E.J. Brennan