For context, I’ve set up a very simple flow lambda → SQS → lambdas (up to 50 concurrent).
Each lambda that is invoked by SQS processes 1 message.
The message visibility timeout is set to 10 minutes.
There’s also a deadletter queue configured to receive messages that failed to be processed three times.
This process executes a couple of times during the day: the filler lambda fills the queue with about 100k messages and the processing all the messages takes about 15 minutes to complete.
It works great, but a couple of days ago I screwed up something in the processing lambda code and this caused about 5% of messages to not be able to be processed.
These messages were added to the deadletter queue as expected, but before being sent there it seems they managed to starve out other messages. Instead of the usual 15 minute, the whole process took 1 hour and 15 minutes.
I’m not sure how this is possible as my visibility timeout is set to 10 minutes, so as far as I know the lambdas should’ve processed the majority of other messages before the failed messages were even retried, but for some reason they didn’t.
Does anyone have an idea what could’ve caused this?
Standard queues have a limit of about 120,000 messages in-flight. But if you only had about 5k failed messages, it shouldn’t have been affected by this limit.
Were the failed messages causing Lambda to immediately fail, or was the function running until it’s timeout? Lambda can take some time to scale up and has a default limit of 1,000 concurrent requests.
If your function timeout was set to something like 5 minutes and each function processes no more than one message at a time then it might take 25 minutes for those 5,000 requests to timeout (when it finally scaled to 1,000 concurrent functions).
These would block the processing of further messages and if you had a 10 minute visibility timeout, then those messages would be back at the start of the queue again by the time a function was free until it was eventually delivered to DLQ.
The way Lambda Service processes SQS is interesting.
By default, there will be 5 “workers”. You can configure the batch size. Each Lambda function will process the entire batch before marking them completed.
So, if you have a batch of 10 messages, and message #10 fails, the entire batch will be failed.
One “work around” is to have the lambda function tell SQS to delete the successful messages as they are completed. Then when the Lambda service tried to fail the whole batch, the ones already deleted will not reappear.
Then there is a bit of scale management that the Lambda service will do. You can control maximum concurrent Lambda functions, and the service will not scale greater than this number. But, you can also allow the service to scale based upon the oldest message and queue size.
Ok, then there is one other thing. If the Lambda service identifies failed batches, it may scale in the number of workers to limit the likelihood of successive failures.
Note, the lambda function timeout needs to be long enough for the entire batch, rather than a single message time. So, give a little extra in the lambda timeout so it doesn’t timeout causing the batch failures as well.
I missed that you have concurrency limit set at 50
And I missed that you have a batch size of 1.
I couldn’t understand how your total job time increased 5x because of some failures and redrives.
Look back at cloudwatch and see how many concurrent functions were actually running, and if the failures reduced that numbers (associating the error times with concurrent execution metrics)
This is actually a common gotcha with SQS and it’s likely due to how SQS handles message ordering + retries in standard queues. Even though your visibility timeout is 10 mins, failed messages that get retried can end up “jumping the queue” because SQS tries to ensure at least once delivery. When messages fail and get requeued, they often get higher internal priority than newer messages since they’re older and have been attempted before.
With 5k messages failing (5% of 100k), you’re essentially creating a situation where these messages are constantly being retried and consuming your lambda concurrency. Since you have 50 concurrent lambdas, a significant portion could be tied up processing these retry attempts, effectively throttling the processing of fresh messages. To fix this, I’d recommend either increasing your lambda concurrency limit, implementing a circuit breaker pattern to fail fast when you detect issues, or using a DLQ with fewer retry attempts (maybe 1-2 instead of 3). You might also want to look into using SQS FIFO queues if strict message ordering is important for your use case.