Search code examples
amazon-web-servicesamazon-sqs

is it possible to know how many times sqs messsage has been read


I have a use case to know how many times sqs message has been read in my code.

For example we read message from SQS, for abc reason/exception we cant process that message . Now the same message available in queue to read after visibility timeout.

This will create endless loop. Is there a way to know how many times particular sqs message has been read and returned back to queue.

I am aware this can be handled via dead letter queue. Since that requires more effort I am checking is there any other option

i dont want to retry the message if it fails more than x time and i want to delete it. Is it possible in SQS


Solution

  • When calling ReceiveMessage(), you can specify a list of AttributeNames that you would like returned.

    One of these attributes is ApproximateReceiveCount, which returns "the number of times a message has been received across all queues but not deleted".

    It is an 'approximate' count due to the highly parallel nature of SQS -- it is possible that the count is slightly off if a message was processed around the same time as this request.