Removing messages from the SQS queue after processing

My application sends messages to the AWS SQS queue for jobs that require some background processing. My handler accepts and processes the following messages:

$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 great, but it’s clear from the SQS console that the messages that my script retrieves are placed in the Messages in Flight category, and then after a while they return to Available Messages, and my script exits them again.

How to remove messages from the SQS queue or is it even better to mark them as completed?

+5
source share
2 answers

When you receive a message from the SQS queue, the message (by default) returns to the queue after 30 seconds. This applies to cases where the processing of a message fails and the message needs to be processed again.

Once the message has been successfully processed, use deleteMessage to delete the message. You will need the value of receiptHandle from the message when you received it from receiveMessage to delete the message.

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

Also keep in mind that Amazon SQS works in such a way that:

  • messages can be received out of order compared to how they were added to the queue
  • Messages can be received twice, so allow the message handler to handle these cases.
+10
source

You need to delete messages after their processing, which will be considered "marking them completed"; however, if something downstream is to act on this “completed” action, it is often for me to send a “complete” message to another SQS queue and then delete the message from the incoming queue. Thus, another process, whether now or on the road, can take measures that need to be taken when the first worker completes his work, i.e. Binds these processes together separately.

If nothing needs to be done next, just removing it is enough.

+2
source

Source: https://habr.com/ru/post/1213885/


All Articles