8

I am using the AWS SDK in PHP to communicate with an SQS queue. At the moment the queue just has simple test messages contained within it. I am attempting to read the next 10 messages from the queue. To do this I have set MaximumNumberOfMessages to 10 and also set the WaitTimeSeconds to 20.

My understanding of this should be that the SqsClient will connect and consume messages from the queue until it has either the number of messages (10) or reaches the timeout (20) seconds.

However the client is returning almost instantly and with only 3-4 messages (now there are +20 messages in the queue). When there were only 4-5 messages it would return with just one message.

I have also set the VisibilityTimeout to just be 1 second, and am not running the test script in a loop, just firing manually from the CLI.

The array I am passing to SqsClient receiveMessage is :

[
        'QueueUrl' => $this->uri,
        'MaxNumberOfMessages' => 10,
        'VisibilityTimeout' => 1,
        'WaitTimeSeconds' => 20,
]

Any ideas why the call isn't waiting for the full 20 seconds at least (for small number of queue messages), and not returning more than a few messages (for a fuller queue) ?

Thanks

Graeme
  • 1,643
  • 15
  • 27
  • 1
    According to [the manual page](http://docs.aws.amazon.com/AWSSimpleQueueService/latest/APIReference/API_ReceiveMessage.html): `MaxNumberOfMessages` is: "_The maximum number of messages to return. Amazon SQS never returns more messages than this value but may return fewer._". It doesn't _have_ to return the maximum. – Henders Sep 13 '16 at 13:50

1 Answers1

17

My understanding (and observations while using SQS) is different that yours. Just because you have set a MaxMessages to 10, doesn't mean you will always get 10, you will get upto 10, but could be less.

The WaitTimeInSeconds is how long it will wait before it will return with no messages, but since it is finding messages in your case, it is returning immediately.

The purpose of the 'WaitTimeInSeconds' is to cut down on the number of calls you need to make in a tight loop asking 'do you have any messages for me' and constantly getting back none.

E.J. Brennan
  • 45,870
  • 7
  • 88
  • 116
  • 1
    Ah, so it won't wait until it receives the requested amount? Simply will wait the specified length of time if it has nothing to retrieve? Makes sense, although I'd find my expected behavior more useful :) Thanks – Graeme Sep 13 '16 at 13:51
  • 1
    Correct. That is my experience when using it. – E.J. Brennan Sep 13 '16 at 13:51
  • 1
    In my case I am calling receiveMessage with waitTime as 20 seconds. I am calling this within a while(true) loop and my assumption was that if there are no messages to be returned the loop iteration would be blocked for atleast the wait time of 20 seconds. But what I could see is the receiveMessage within the block is getting called continuously and there is no waiting happening. – shah1988 Jul 04 '18 at 14:09