amazon-web-servicesamazon-sqsmessage-bus

Does Amazon SQS Receive Message Process Make it Unavailable?


I'm new to SQS but I have worked with Azure Service Bus, and if I remember correctly, once you 'pop' a message from the queue it essentially no longer exists in the queue so you don't have to worry about multiple consumers getting the same message (please excuse me if this is incorrect).

I'm following the Amazon .NET SDK example for 'Receiving and Deleting' a message from the queue, which to me implies that until it is deleted it may be possible for another consumer to pick it up. Is this correct? Is there a way to remove it from the queue at the same time it is received or otherwise protect it from duplicate consuming?

// Receive a single message from the queue.
var receiveMessageRequest = new ReceiveMessageRequest
{
    AttributeNames = { "GlobalTransactionId", "Amount", "TransactionDate" },
    MaxNumberOfMessages = 1,
    MessageAttributeNames = { "All" },
    QueueUrl = queueUrl,
    VisibilityTimeout = 0,
    WaitTimeSeconds = 0,
};

var receiveMessageResponse = await client.ReceiveMessageAsync(receiveMessageRequest);

// Delete the received message from the queue.
var deleteMessageRequest = new DeleteMessageRequest
{
    QueueUrl = queueUrl,
    ReceiptHandle = receiveMessageResponse.Messages[0].ReceiptHandle,
};

var deleteMessageResponse = await client.DeleteMessageAsync(deleteMessageRequest);

Solution

  • As per the Amazon SQS message lifecycle:

    Your consumer could call ReceiveMessage() and then immediately call DeleteMessage() for each of the messages it received. However, this has the risk that the messages were not correctly consumed. It is your choice.

    It is not possible to "remove it from the queue at the same time it is received".

    To "protect it from duplicate consuming", ensure that the visibility timeout is several times greater than the time it takes to process the message. That is, the time between Receiving the message and Deleting it.