Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

aws FIFO queue returns empty queue even though it has messages available

I have a FIFO queue with over 2 mil messages available. And I want to process them with lambda functions but 9 out of 10 times I poll messages I get a reply that the queue is empty. Which is definitely not true. I tried to change to long poll but it didn't helped. Here is my code for polling messages.

import { create as listenToSqsQueue } from "sqs-consumer";

listenToSqsQueue({
    queueUrl: Config.aws.sqsurl,

    handleMessage: async function (message, done){
        // do some work with `message`s
        Promise.resolve(invokePoller(functionName, message, callback));
        done();
      }
    ,         
    batchSize: 10
}).on("empty", function() {
    callback(undefined, "Queue is empty");
}).on("error", function(error: Error, message: any) {
    console.error(error, message);
    callback(error)
}).start();
like image 881
Angela Tim Avatar asked May 19 '18 20:05

Angela Tim


People also ask

What causes empty receives SQS?

Amazon SQS charges are based on request volume and data transferred in and out of Amazon SQS. A consumer polling an SQS queue continuously results in empty receives. These empty receives are charged per Amazon SQS pricing even if messages aren't sent or received from your SQS queue.

Why are messages not visible in SQS?

To prevent other consumers from processing the message again, Amazon SQS sets a visibility timeout, a period of time during which Amazon SQS prevents other consumers from receiving and processing the message. The default visibility timeout for a message is 30 seconds. The minimum is 0 seconds. The maximum is 12 hours.

How does the AWS FIFO SQS queue send messages only once without any duplicates?

Standard queues provide at-least-once delivery, which means that each message is delivered at least once. FIFO queues provide exactly-once processing, which means that each message is delivered once and remains available until a consumer processes it and deletes it. Duplicates are not introduced into the queue.

What is the throughput limit for FIFO queues in Amazon SQS?

Today, Amazon SQS announces the general availability of high throughput mode for FIFO queues, allowing you to process up to 3000 messages per second per API action.


1 Answers

I would suggest you to try out a small test as below steps to understand this behavior of FIFO queues yourself.

  1. Create a FIFO queue for testing purposes
  2. Send two messages to the queue
  3. Invoke receive-message API of the queue. You should receive the first message that you entered to the queue
  4. Invoke receive-message API multiple times and for subsequent attempts, you get empty responses. After 30 seconds (visibility timeout), you will get the same message, if you invoke the receive-message API. However, you will never receive the second message from the queue.
  5. Delete the message that you received in the step 3
  6. Invoke receive-message API and you should receive the second message that you sent to the queue

The reason that your queue consumer receives empty responses for receive-message API is the queue consumer has not deleted the received message after processing it. If your FIFO queue delivered other messages while the already delivered message is not deleted, it could violate the guarantee of "First-In-First-Out Delivery". Because the second message could be processed before the first message is processed. This behavior becomes more apparent if you consider a situation where there are multiple queue consumers.

In a summary, I would suggest you to consider deleting a message as a notification to the FIFO queue to instruct that the queue consumer has successfully processed the message and FIFO queue is allowed to deliver next message in the queue. To fix client code, you may modify it to delete the message after it is successfully processed.

like image 54
Denis Weerasiri Avatar answered Sep 28 '22 10:09

Denis Weerasiri