Home > OS >  With AWS SQS, why do subsequent calls to ReceiveMessageAsync not return any messages?
With AWS SQS, why do subsequent calls to ReceiveMessageAsync not return any messages?

Time:05-12

Firstly, I know that when using SQS to process messages, I should be deleting messages after processing them. However, I would assume that if I have multiple threads, each thread should be able to receive messages, process them, and then delete them later.

So, why is it that if I receive messages, without deleting them, I cannot retrieve any more until the visibility timeout period has expired?

Or, to put it another way, how can I process more than 10 messages simultaneously?

I have hundreds of messages, and would have expected this code to return 30 messages:

private static async Task TryToDisplayAllMessagesButForSomeReasonOnlyTheFirstRequestWorksAsync()
{
    var client = new AmazonSQSClient(RegionEndpoint.EUWest1);
    var request = new ReceiveMessageRequest
    {
        QueueUrl = QUEUE_URL,
        MaxNumberOfMessages = 10,
        VisibilityTimeout = 10
    };
    var messages1 = await client.ReceiveMessageAsync(request);
    Console.WriteLine($"{messages1.Messages.Count} messages received");
    var messages2 = await client.ReceiveMessageAsync(request);
    Console.WriteLine($"{messages2.Messages.Count} messages received");
    Thread.Sleep(11000);
    var messages3 = await client.ReceiveMessageAsync(request);
    Console.WriteLine($"{messages3.Messages.Count} messages received");
}

Instead, it returns 20, because I cannot receive more until the VisibilityTimeout period has expired.

Output:

10 messages received
0 messages received
10 messages received

Thanks in advance :)

CodePudding user response:

If multiple messages in your FIFO queue have the same Message Group ID, then in order to ensure FIFO ordering of the messages, once you retrieve some of those messages with that group ID the rest of the messages will not be visible until you delete the ones you have retrieved from the queue.

  • Related