Iter Ator
Iter Ator

Reputation: 9298

Only one message is received from SQS ( nodejs aws sdk)

I created an SQS with default settings. I published two messages to it, and I would like to read them back in the same time. I tried it like this:


const sqsClient = new SQSClient({ region: REGION });

const params = {
  AttributeNames: ["SentTimestamp"],
  MaxNumberOfMessages: 5,
  MessageAttributeNames: ["All"],
  QueueUrl: queueURL,
  WaitTimeSeconds: 5,
};

const data = await sqsClient.send(new ReceiveMessageCommand(params));
const messages = data.Messages ?? [];

console.log(messages.length);

Unfortunately only one message is returned, no matter what I provide in MaxNumberOfMessages. What can cause this? How is it possible to fix this issue?

I was able to find a similar question, but it has only one answer, refering to a 3rd party library.

Upvotes: 1

Views: 1754

Answers (1)

Ervin Szilagyi
Ervin Szilagyi

Reputation: 16805

A ReceiveMessageCommand does not guarantee that you will get exactly the number of messages specified for MaxNumberOfMessages. In fact the documentation says the following:

Short poll is the default behavior where a weighted random set of machines is sampled on a ReceiveMessage call. Thus, only the messages on the sampled machines are returned. If the number of messages in the queue is small (fewer than 1,000), you most likely get fewer messages than you requested per ReceiveMessage call. If the number of messages in the queue is extremely small, you might not receive any messages in a particular ReceiveMessage response. If this happens, repeat the request.

You must use long-polling to receive multiple messages. This is essentially setting the WaitTimeSeconds to a greater value (5 seconds should be enough). And you must have a larger number of messages in the queue to be able to fetch multiple messages with one call.

To summarize:

  1. SQS is a distributed system, each call will poll one machine only.
  2. Messages are distributes on those machines, if you have a small number of messages, it might happen that you fetch only one message, or none.
  3. Test your code with a larger set of sent messages and put your receiving call in loop.

Upvotes: 1

Related Questions