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();
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.
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.
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.
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.
I would suggest you to try out a small test as below steps to understand this behavior of FIFO queues yourself.
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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With