Skip to content
Cloud Efficiency Hub

Recursive Invocation Loop Between Lambda and SQS

The short version

When a Lambda function processes messages from an SQS queue but fails to handle certain messages properly, the same messages may be returned to the queue and retried repeatedly.

Christine Oji

AWS service
AWS Lambda
Category
Compute
Reference
CER-0207
Type
Inefficient Architecture

Explanation

Why the waste happens and who it affects.

In some cases, especially if the Lambda is also writing messages back to the same or a chained queue, this can create a recursive invocation loop. This loop results in high invocation counts, prolonged execution, and unnecessary costs, particularly if retries continue without a termination strategy.

Lambda recursive loop detection, on by default at no charge, stops a Lambda-SQS loop after about 16 invocations in the same chain of requests when the function uses a supported AWS SDK, and records it in the RecursiveInvocationsDropped metric. Loops still cost money until then, and SQS keeps retrying the message until maxReceiveCount is exceeded, so a redrive policy and DLQ on the source queue are still needed.

Billing model

The pricing dimensions that drive this cost.

Billed based on the number of invocations and duration of execution time in milliseconds, rounded up to the nearest 1ms. Repeated or unintended invocations directly increase cost.

How to detect

8 checks to find it in your estate.

  • Sudden spike in Lambda invocations without corresponding increase in business traffic
  • High volume of messages visible in an SQS queue without corresponding successful Lambda completions
  • Lambda metrics showing consistent re-execution of the same payloads
  • Lack of configured dead-letter queues or maximum receive count thresholds
  • Application behavior where Lambda publishes messages back to the originating or an indirectly connected SQS queue
  • Reprocessing patterns identifiable by message IDs or payload similarity in logs
  • Account-wide cost variance alerts pointing to Lambda or SQS service anomalies
  • Non-zero RecursiveInvocationsDropped metric, or functions whose recursive loop detection is set to Allow

How to fix

5 ways to remove the waste.

  • Introduce a dead-letter queue (DLQ) to route failed messages after a maximum number of retries
  • Set a maximumReceiveCount on the SQS redrive policy to avoid indefinite reprocessing
  • Refactor the Lambda logic to prevent re-enqueuing the same message unintentionally
  • Monitor DLQ metrics and alarms to catch patterns before they become runaway costs
  • Perform architecture reviews for recursive patterns in asynchronous messaging flows

Documentation

Vendor references for pricing and configuration.