CodePark/ questions / Lambda & serverless
START PRACTICINGPRACTICE
Backend question · Cloud & AWS › Lambda & serverless

A Lambda function consumes an SQS queue with a batch size of 10. Explain what this handler does, what happens without it when one message fails, and how the queue's visibility timeout should relate to the function timeout.

export const handler = async (event) => {
  const batchItemFailures = [];
  for (const record of event.Records) {
    try {
      await processOrder(JSON.parse(record.body));
    } catch {
      batchItemFailures.push({ itemIdentifier: record.messageId });
    }
  }
  return { batchItemFailures };
};

This handler reports only the failed messages, so Lambda retries only those. Without it, one failure makes the whole batch run again.

Lambda polls the queue and calls the function with a batch of messages. By default, if the handler throws, Lambda treats the whole batch as failed. Every message becomes visible in the queue again after the visibility timeout. So the nine messages that succeeded are processed again.

This handler uses partial batch responses. You must turn on ReportBatchItemFailures on the event source mapping. Then Lambda deletes the successful messages and returns only the listed message IDs to the queue. Processing must still be idempotent (safe to run twice with the same result), because SQS delivers each message at least once and sometimes more.

The visibility timeout is how long a received message stays hidden from other consumers. Set it well above the function timeout. AWS recommends at least six times the function timeout. Otherwise a message can reappear and be processed a second time while the first call is still running.

Also configure a redrive policy with a maxReceiveCount. Then poison messages (messages that fail every time) move to a DLQ (dead-letter queue) instead of retrying forever.

  • By default, a thrown error fails the whole batch.
  • A partial batch response returns only the failed message IDs, and it needs ReportBatchItemFailures turned on.
  • Handlers must be idempotent, because SQS delivers at least once.
  • The visibility timeout should be well above the function timeout, and AWS suggests at least 6 times.
  • Use a redrive policy and a DLQ for poison messages.