hamncheez
hamncheez

Reputation: 739

AWS SQS FIFO short polling receiveMessage not returning any messages

My code is simple:

  const params = {
    AttributeNames: [
      'All',
    ],
    MaxNumberOfMessages: 10,
    MessageAttributeNames: [
      'All',
    ],
    QueueUrl: SQS_QUEUE_URL,
    VisibilityTimeout: 20,
    WaitTimeSeconds: 10,
  }
   
  const messages = await SQS.receiveMessage(params).promise();
  console.log({messages});

However, unless WaitTimeSeconds === 20, I cannot get ANY messages from SQS. My messages looks like this:

  messages: {
    ResponseMetadata: { RequestId: 'a1b6eb4e-7986-5c77-abff-b3e857329498' }
  }

If I set WaitTimeSeconds: 20 then I can get my messages. Any other value will not return any messages. I have this running on a cron every minute, and I've let it run for hours without successfully retrieving anything (unless the wait time is 20 seconds). Any suggestions?

Upvotes: 1

Views: 2029

Answers (1)

John Rotenstein
John Rotenstein

Reputation: 269081

You have selected to use a FIFO queue.

From Using the Amazon SQS message group ID - Amazon Simple Queue Service:

The message group ID is the tag that specifies that a message belongs to a specific message group. Messages that belong to the same message group are always processed one by one, in a strict order relative to the message group (however, messages that belong to different message groups might be processed out of order).

This means that if a message is current "in-flight" (meaning that it has been received from the queue, but not yet deleted), then the SQS FIFO queue will not provide another message with the same MessageGroupId. This is to ensure that messages are processed in-order. For example, the in-flight message might exceeds its Invisibility Timeout and return to the queue, so other messages with the same MessageGroupId cannot be processed until that first message has been deleted.

If you do not require this level of isolation, then give each message a different MessageGroupId. This will keep the messages in-order and will make all messages available.

It also sounds like you are playing around with the queue a bit, possibly retrieving messages without deleting them. If so, the messages will be 'invisible' for 20 seconds (VisibilityTimeout: 20). Any attempt to retrieve messages during this period will not return these messages. That might be why you sometimes do not receive messages.

Upvotes: 5

Related Questions