Search code examples
amazon-web-servicesaws-lambdaamazon-sqs

How to tell when Lambdas complete processing of all messages in SQS


Currently I have a process where a Lambda (A) gets triggered which has logic to find out what customers need to have another lambda (B) run for (via a queue). For any run there could be 3k to 4k messages placed on the SQS Queue by Lambda A to be picked up by Lambda B to process. As Lambda B communicates with an external Api, the concurrency is set to 10 for Lambda B so as not to overload the Api. The whole process completes in 35 to 45 minutes.

My problem is how to tell when all the processing is complete?


Solution

  • If you don't need timely information, you could check out the CloudWatch Metrics that SQS offers, e.g.:

    ApproximateNumberOfMessagesVisible

    The number of messages available for retrieval from the queue. Reporting Criteria: A non-negative value is reported if the queue is active.

    and

    ApproximateNumberOfMessagesNotVisible

    The number of messages that are in flight. Messages are considered to be in flight if they have been sent to a client but have not yet been deleted or have not yet reached the end of their visibility window. Reporting Criteria: A non-negative value is reported if the queue is active.

    If the sum of these two metrics hits zero, no messages are in the Queue, and processing should be done.

    If you need more timely information, the producer of the messages could increment a counter item in DynamoDB with the number of messages added, and each Lambda decrements that counter once it's done. You could then add a Lambda to the DynamoDB Stream of that table with a filter and do something when the value changes to zero again. This is, however, much more complex.

    A third option could be to transform the whole thing into a stepfunction and use a map state with a parallelization factor to work on the tasks. The drawback is that the length of the list it can work on is limited afaik.