Is receiving number of messages from a SQS limited to 10 per batch call

Viewed 6263

I am developing an application where I am using Amazon SQS. I am new to AWS, so I have one small doubt regarding receiving messages from an SQS. I have configured an SQS and want to receive messages from it by using long polling. My question is how many numbers of messages I can get from the SQS in max?

I have set the MaxNumberOfMessages to 50 and the ReceiveMessageWaitTimeSeconds to 20.

Q1. Shall I get a max of 50 messages from the SQS?

Q2. How does ReceiveMessageWaitTimeSeconds matter? I read in the AWS documentation that short polling is = 0 sec and long polling is anything more than 0 second. Does it mean that I can set ReceiveMessageWaitTimeSeconds = 5 also to ensure long polling?

I also read the difference between short polling and long polling as the former one will only query a subset of SQS servers and the latter will query all the servers. So does the seconds matter except the fact that it will wait for 5 seconds or 20 seconds?

I have gone through the below AWS docs:

The sample code is given below:

CreateQueueRequest createQueueRequest = new CreateQueueRequest().withQueueName(queueName);
 String queueUrl = sqs.createQueue(createQueueRequest).getQueueUrl();
 ReceiveMessageRequest receiveMessageRequest = new ReceiveMessageRequest(queueUrl);
 receiveMessageRequest.setMaxNumberOfMessages(50);
 receiveMessageRequest.setReceiveMessageWaitTimeSeconds(20);
 List<Message> messages = sqs.receiveMessage(receiveMessageRequest).getMessages();
 for (Message message : messages) {
      // I'm a message from SQS
 }
2 Answers

Long Polling simply means that Amazon SQS will wait a certain amount of time before returning a result when there are no messages available. That way, instead of continually asking for a message, SQS will delay the response until either the configured time has elapsed, or a message is available.

It's a bit like having children in the back seat of the car saying "Are we there yet?" once every second. Long Polling increases the time between them asking.

The maximum number of messages that will be provided via a ReceiveMessage() call is 10. The documentation says: "Valid values: 1 to 10."

"setMaxNumberOfMessages" : this is to provide an upper limit on the number of message SQS can send to you at one go. It can send less than the set limit, but will never exceed the limit in one response.

"ReceiveMessageWaitTimeSeconds": If this is set to zero, you will receive a lot of empty responses if there are no messages available. However, setting this to greater than zero enables a long poll. For example if you set this parameter to 5, queue will wait for 5 seconds at least if it did not get a message (thereby avoiding giving you empty response), but If it did get a message within 5 seconds, it will send the response immediately.

But, if the queue could not get a message even after 5 seconds, only then will it respond with empty message. So, this timeout has to be tweaked depending on how many messages you expect to receive, if the messages are fewer, then you can wait for longer.

Related