Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Amazon Simple Queue Service (SQS)

I created a queue in SQS, added two messages (serialized PHP arrays: array('filename' => 0, ...) and array('filename' => 1, ...)). I'm using the newest version of amazon SDK for PHP from their git repo.

The problem is that when I use receive_message function with these options:

MaxNumberOfMessages = 10
VisibilityTimeout = 0 // other values doesn't change much

I get only the first message, repeated 10 times:

<ReceiveMessageResponse>
−
<ReceiveMessageResult>
−
<Message>
<MessageId>82523332-75e0-444d-ae8f-55ccd5580beb</MessageId>
−
<ReceiptHandle>
v5iiyMGi3b6RunVNVvjOQOV+ZDqRV7sNLzj5pUAEj1brIAkucpYiGaM8UIdOEis9Kouh4s+cAkSAd7MhbJKPGM6SdKYE993x2Lf/DwEbhkfmzRxOevzUsyJCrrVdTSTSx0cNUqqV6Cgr/Asi72t/UOhbdXhTp3kaCaZfd2weymg=
</ReceiptHandle>
<MD5OfBody>ced185420292fbd06b32ea6e35da3d21</MD5OfBody>
−
<Body>
a:3:{s:8:"priority";i:2;s:8:"filename";i:0;s:11:"task_ticket";s:0:"";}
</Body>
</Message>
−
<Message>
<MessageId>82523332-75e0-444d-ae8f-55ccd5580beb</MessageId>
−
<ReceiptHandle>
v5iiyMGi3b6RunVNVvjOQOV+ZDqRV7sNLzj5pUAEj1brIAkucpYiGaM8UIdOEis9Kouh4s+cAkSAd7MhbJKPGM6SdKYE993x2Lf/DwEbhkfmzRxOevzUsyJCrrVdTSTSx0cNUqqV6Cgr/Asi72t/UOhbdXhTp3kaCaZfd2weymg=
</ReceiptHandle>
<MD5OfBody>ced185420292fbd06b32ea6e35da3d21</MD5OfBody>
−
<Body>
a:3:{s:8:"priority";i:2;s:8:"filename";i:0;s:11:"task_ticket";s:0:"";}
</Body>
</Message>
    ...and so on, always with "filename";i:0

I'm 100% that there are only 2 messages in the queue (I deleted it and recreated to be sure) and yet I get only the first one, populated many times. This changes from time to time and sometimes I get the second one mixed in the list. If I leave VisibilityTimeout as default 3 (or other non-zero value) the first one disappears for a while (as expected) and then I get the second one repeated many times.

get_queue_size returns 2, which is true.

I also tried Amazon Scratchpad and just made API calls with the same results. So, is SQS broken or I'm doing something totally wrong?

like image 578
Ian Avatar asked Jan 21 '23 06:01

Ian


1 Answers

I believe this is expected behavior because you have set VisibilityTimeout = 0. Typically you would set the timeout value to be the expected duration to process a message. You must call delete on a read message before the visibility timeout expires or the message will be automatically re-queued.

In more complex systems a separate thread might be used to extend the timeout period for a single message if the initial timeout was not long enough.

As it sounds like you are just starting, it's important that you write your message processing code to account for reading the same message multiple times. Not only can your message get re-queued automatically but SQS will occasionally return a duplicate message.

like image 113
Uriah Carpenter Avatar answered Jan 29 '23 11:01

Uriah Carpenter