Skip to content

Unable to fetch data from SQS queue.

0

Despite 220k+ mesages in SQS queue. I am unable to get messages from queue everytime I requests data. Why is

var response = SQS_Client.ReceiveMessage(SQS_ReceiveMessageRequest);

not returning data everytime I call receiveMessages function?

asked a year ago577 views
3 Answers
1

If you're not getting an error message, then most likely, the messages have already been fetched but not deleted from the queue. They will only become visible again after the visibility timeout has expired: https://docs.aws.amazon.com/AWSSimpleQueueService/latest/SQSDeveloperGuide/sqs-visibility-timeout.html

I suggest you first check in your queue's metrics how many messages are visible.

On another note, to ensure you receive your messages, you should perform a long poll and preferably with the maximum wait time, if that's convenient: https://docs.aws.amazon.com/AWSSimpleQueueService/latest/SQSDeveloperGuide/sqs-short-and-long-polling.html. With a short poll, you aren't guaranteed to receive messages, particularly if only a small number of them is present. In this case, having hundreds of thousands of messages in the queue, this is very unlikely to be related to your immediate issue, however.

EXPERT
answered a year ago
  • No, I am not getting any errors. Its just that ReceiveMessage api returns an empty array. Secondly, I have validated the possibility of messages not getting deleted also. Its is working perfectly. Any message that is read once is being deleted surely. I also have implemented long pooling with WaitTimeSeconds = 10s and VisibilityTimeout = 60s.

    What I am unable to understand is that why is it that if I run several instances at the same time, only one instance gets served and others get empty array. Moroever, if I run only a single instance of my service, still the ReceiveMessage api was not getting data everytime. When I am saying "everytime" it means that I am fetching messages from queue in a loop. Some times it does fetch data while other times it doesn't .

    The last thing I want to add is that I was facing this issue for around 6 - 7 hours. and then suddenly, everything started working smoothly. I didn't change anything at all and ReceiveMessage api started giving data everytime it was being called. Moreover, all the instances that were running started getting data and I was able to clear 400k+ messages within 30minutes. This is the most curious part. What was going on between the time when sqs queue was not providing data and than started giving messages?

    Can you please comment on this?

1

Hello,

If you're unable to fetch messages from your SQS queue despite having 220k+ messages, try these steps:

Visibility Timeout:

  • Messages might be fetched but not deleted, making them temporarily invisible. Check and adjust the visibility timeout.

Long Polling:

  • Use long polling to increase the chances of receiving messages:
var receiveMessageRequest = new ReceiveMessageRequest
{
    QueueUrl = "YOUR_QUEUE_URL",
    WaitTimeSeconds = 20,  // Long polling
    MaxNumberOfMessages = 10 // Adjust as needed
};
var response = SQS_Client.ReceiveMessage(receiveMessageRequest);

Delete Messages:

  • Ensure you delete messages after processing to prevent them from becoming invisible:
var deleteMessageRequest = new DeleteMessageRequest
{
    QueueUrl = "YOUR_QUEUE_URL",
    ReceiptHandle = "MESSAGE_RECEIPT_HANDLE"
};
SQS_Client.DeleteMessage(deleteMessageRequest);

Check Permissions:

  • Verify that your SQS client has the necessary permissions to receive messages.
EXPERT
answered a year ago
EXPERT
reviewed a year ago
  • Long pooling is already implemented and Visibility Timeout is set to 60s. I also have validatred if messages are being deleted or not. Any messages that is read from queue is being deleted surely. Lastly, as per permissions, yes sqs client has all necessary permissions to receive messages.

    Most surprising part is that I faced the issue around 6 - 7 hours and that suddenly everything started working smothly. ReceiveMessage api started responding everytime and multiple instances of my service started working concurrently. Now I have no idea what exactly was the issue during the hours when I was facing this issue. What is the root cause and how can I avoid it from happening next time.

    Any suggestions or comments are more than welcome. I am totally clueless what caused the issue to happen.

0

Are you using a FIFO queue? If you are, could it be that all the messages have the same group ID and that there is another consumer reading messages from the queue? Are all consumers not receiving messages? What is the visibility timeout?

AWS
EXPERT
answered a year ago
EXPERT
reviewed a year ago
  • Yes, I am using a FIFO queue. All messages have unique messageIDs. Visibility timeout is 60s. Sueprisingly, the issue got fixed automatically after 6 - 7 hours. There was no change in code or in queue and somehow, ReceiveMessage api started returning data everytime it was being called. I am not sure what exactly was the issue that was stopping ReceiveMessage api to return data despite queue size reached 400k+. How can I avoid this next time and why did this happen in the first place?

  • If you are using FIFO and you had many messages (>20K) belonging to the same group ID at the top of the queue, no other consumer will receive messages until that number goes down. When SQS looks for the next message, it checks for messages with a group ID which is not being processed at the moment, but it only looks at the first 20K messages.

    This may be the reason for what you have seen.

You are not logged in. Log in to post an answer.

A good answer clearly answers the question and provides constructive feedback and encourages professional growth in the question asker.