Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Efficient way to check whether SQS queue is empty

I have a SQS Queue from which messages are read by multiple hosts. I want to run some job (business logic) after all the messages in the queue have been processed.

How can I check that the queue is empty?

Yes, I can check for ApproximateNumberOfMessages and ApproximateNumberOfMessagesNotVisible queue attributes but these are approximate numbers. I want to stop my hosts polling for messages in the queue when there are no messages left and then run the required job.

Any ideas? Thanks

like image 964
rightCoder Avatar asked May 10 '16 17:05

rightCoder


People also ask

What is number of empty receives in SQS?

To determine if empty receives are contributing to high Amazon SQS charges, review the Amazon CloudWatch NumberOfEmptyReceives metric for the SQS queue. A high number of empty receives indicates that a consumer is making a high number of ReceiveMessage requests.

What is queue visibility timeout in SQS?

Every Amazon SQS queue has the default visibility timeout setting of 30 seconds. You can change this setting for the entire queue. Typically, you should set the visibility timeout to the maximum time that it takes your application to process and delete a message from the queue.

How long can a message be retained in an SQS queue?

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.

How reliable is the storage of message data on AWS SQS?

Reliably deliver large volumes of data, at any level of throughput, without losing messages or needing other services to be available. Securely send sensitive data between applications and centrally manage your keys using AWS Key Management.


2 Answers

You could simply note empty receives from the API response while you're polling. Concerning CloudWatch, there is another metric that would be a better fit for this. From the documentation:

NumberOfEmptyReceives

The number of ReceiveMessage API calls that did not return a message.

Units: Count

Valid Statistics: Average, Minimum, Maximum, Sum, Data Samples (displays as Sample Count in the Amazon SQS console)

Some additional info:

  • This metric only gets populated every 5 minutes. If you set up an alarm based on this metric, this means your minimum period should be 5 minutes.
  • Sum is the most sensible statistic for your use case. If NumberOfEmptyReceives > 0, your polling job checked the queue and received no messages.

I personally used this metric to set up a cloudwatch alarm that will scale down an autoscaling group that hosts my polling job after the sum of NumberOfEmptyReceives > 0 for several consecutive periods. I like doing consecutive periods because it makes it more evident that the queue was not only empty, but has stayed empty.

like image 56
Anthony Neace Avatar answered Sep 19 '22 05:09

Anthony Neace


You could trigger your post business logic on the cloud watch metric related to queue depth. When the depth is 0 then you can send a SNS notification or start a lambda function.

Also this cloud watch metric is better then others since this is actual message count reported by sqs service.

like image 32
Rohit Avatar answered Sep 19 '22 05:09

Rohit