Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

When should I delete messages in SQS?

My application consists of:

  • 1 Amazon SQS message queue
  • n workers

The workers have the following logic:

 1. Wait for message from SQS queue
 2. Perform task described in message
 3. Delete the message from the SQS queue
 4. Go to (1)

I want each message to be received by only one worker to avoid redundant work.

Is there a mechanism to mark a message as "in progress" using SQS, so that other pollers do not receive it?

Alternatively, is it appropriate to delete the message as soon as it is received?

 1. Wait for message from SQS queue
 2. Delete the message from the SQS queue
 3. Perform task described in message
 4. Go to (1)

If I follow this approach, is there a way to recover received but unprocessed messages in case a worker crashes (step (3) fails)?


This question is specific to Spring, which contains all sorts of magic.

like image 493
sdgfsdh Avatar asked Nov 15 '19 13:11

sdgfsdh


People also ask

Do we need to delete message from SQS?

Depending on your application's needs, you might have to use short or long polling to receive messages. Amazon SQS doesn't automatically delete a message after retrieving it for you, in case you don't successfully receive the message (for example, if the consumers fail or you lose connectivity).

How long can messages stay in SQS?

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.

Does SQS automatically delete a message from a queue?

Amazon SQS automatically deletes messages left in a queue longer than the retention period configured for the queue. The ReceiptHandle is associated with a specific instance of receiving a message. If you receive a message more than once, the ReceiptHandle is different each time you receive a message.

Does Lambda need to delete SQS message?

Example Amazon SQS message event (FIFO queue)If your function successfully processes the batch, Lambda deletes the messages from the queue. By default, if your function encounters an error while processing a batch, all messages in that batch become visible in the queue again.


1 Answers

An SQS message is considered to be "inflight" after it is received from a queue by a consumer, but not yet deleted from the queue. These messages are not visible to other consumers.

In SQS messaging, a message is considered in "inflight" if:

  1. You the consumer have received it, and
  2. the visibility timeout has not expired and
  3. you have not deleted it.

SQS is designed so that you can call ReceiveMessage and a message is given to you for processing. You have some amount of time (the visibility timeout) to perform the processing on this message. During this "visibility" timeout, if you call ReceiveMessage again, no worker will be returned the message you are currently working with. It is hidden.

Once the visibility timeout expires the message will be able to be returned to future ReceiveMessage calls. This could happen if the consumer fails in some way. If the process is successful, then you can delete the message.

The number of messages that are hidden from ReceiveMessage call is the "inflight" number. Currently a SQS queue is set by default to allow a max of 120,000 messages to be "inflight".

http://docs.amazonwebservices.com/AWSSimpleQueueService/latest/SQSDeveloperGuide/AboutVT.html

like image 89
Rodrigo Murillo Avatar answered Nov 15 '22 05:11

Rodrigo Murillo