Long polling helps reduce the cost of using Amazon SQS by eliminating the number of empty responses […] and false empty responses […].
But it also says:
Reduce false empty responses by querying all—rather than a subset of—Amazon SQS servers.
One says it eliminates the number of false empty responses and the other says it just reduces it. What is actually the case? Is it guaranteed that the request is querying all sqs servers when using long polling (even if wait time is just 1 second)?
Well the reason it is vague is that it depends a lot on timing and how many clients there are.
The main things at play here are 1) are there currently any messages in the queue at all. 2) are there currently any messages in the queue that were received by another client and are still in their visibility timeout window. 3) are there any other clients who might be ahead of you in the queue.
if there are no messages in the queue at the moment you send a request, long polling will keep the connection open for a specific amount of time, if any messages arrive during that time you will receive them (up to the configured number per request).
if another client is currently handling the only message(s) available and the message(s) are still hidden due to the visibility timeout, then this scenario acts the same as #1 accept you’re waiting for both new messages and old ones that are hidden (assuming the other client doesn’t then delete the hidden messages).
if other clients consume all of the available messages then it’s back to scenario 1 or 2 depending on their actions.
As for multiple servers, I can only assume there is some sort of message propagation that occurs through the region’s AZs and that short pulling must only be hitting a single AZ. I’d assume the type of queue (standard/FIFO) effects this behavior.
Thank you for the response. But I don’t think it addresses my issue with the documentation.
I know I can get empty messages for multiple reasons (no messages in the queue, other clients, visibility timeout, etc.), but my concern is with false empty messages. Meaning that there are available messages but they are not returned. This is an issue with short polling, as only a (random?) subset of sqs servers are accessed to get messages. The documentation is not clear wheather it is an issue with long polling or not.
In my scenario, I only have one client reading messages from the queue and I am wondering if I can be sure that if a long polling receiveMessages request returns nothing, the queue is in fact empty or if I have to make multiple calls to make sure.
The docs say “In rare cases, you might receive empty responses even when a queue still contains messages” with long polling, so if your question is about a guarantee of never receiving false empty message, I think that’s your answer.
The AWS docs folks are generally responsive, so if you don’t find this page as clear as you’d like you should submit feedback to try to get it changed
To further u/farski ‘s response, I’m not sure weather or not long-polling fixes this issue. But most workflows will long-poll, process responses (if there are any), then long-poll again, continually. SQS is an asynchronous system so you’d need to build your clients on the assumption that messages may or may not be there. So it almost doesn’t matter if you get a false empty, because you should be building the clients to handle that situation.
If you’re interested the BBC built a consumer that is a good proof of concept for an SQS consumer.
Thank you. The problem I am facing is, that we get huge spikes of small events. They must be merged depending on some properties of the events and then processed in bulk. But they occur only 2 or 3 times a week. So I don’t want to have a dedicated EC2 instance running in the background polling. Instead, I thought about starting a “long” running lambda function to handle the spike. As soon as the first is added, the lambda is started and will poll the queue until the spike is over (roughly 3-4 minutes). I just needed to know if I can stop the lambda as soon as no message is returned. But I guess I will wait another 30s to make sure.
Ah, yeah that is a tough solution. I almost feel like SQS/lambda might not be ideal for that. Maybe instead you could do a really small EC2 instance that only receives these messages (I know you said you didn’t want to, but it might be more reliable. Something like a t3.micro is only ≈$7.60/month). Then once you debounce them all it could call a lambda to do the heavy lifting? Or if there is a way to get the events into a Redis instance you could use Lists and BLPOP, that could be much more versatile.