Search code examples
amazon-web-servicesamazon-sqs

AWS SQS and visibility timeout during an exception


Im curious to know, is the visibility timeout respected if an exception is thrown by the app after taking a message off the sqs qeue..

Im of the impression it is respected, so if an exception is thrown in the application say validation failed, the message remains on queue before being moved to the DLQ? ie until timeout elapses.

is that correct?


Solution

  • When your application calls ReceiveMessage(), a message is returned and that message is made 'invisible' in the queue. When your application has finished processing the message, it should call DeleteMessage() to remove the message from the queue.

    If that is not done within the invisibility timeout period, then the message will 'reappear' on the queue so that it can be re-processed. This means that if your application throws an exception and does not complete processing the message, then the message will be re-processed.

    However, if this happens multiple times, then the message will be moved to the Dead Letter Queue. This prevents a situation where the message continually fails to be processed. When configuring the SQS Queue, you can configure the number of retries before sending the message to the Dead Letter Queue.

    Amazon SQS requires the timeout because that's how it knows that processing has failed. It has no insight into the actual application failure -- it can only assume it since the timeout period has passed. For a 'quicker' response, you can use a very small timeout period, and then have your application send a 'heartbeat' signal to SQS at frequent intervals, telling it that the message is still being processed, and therefore resetting the invisibility timeout period.