We have somewhat recently started to move some of our data processing pipeline to use queues to manage individual bits of data, whereas previously we had timed lambdas that would pull all data since last change.
While making this change, we noticed that queues didn’t work quite as we had anticipated first of all - we thought lambda would just pull items off the queue as the lambdas had availability. Instead, it seems the aws managed lambda trigger grabs a chunk of messages (up to ten) and throws it at the lambda service. If lambda doesn’t have availability, the message gets throttled, then replayed after a backoff time, up til our configured replay “error” limit (five). After that, it’s thrown into our dead letter queue.
We see a handful of message per day end up in the dead letter queue as a result of throttling. We then throw these back into the main queue (we have a process to do so every handful of hours). However, we weren’t 100% sure throttling was the reason for things being pushed over since nothing indicates why the messages are moved over - we just assumed as much because we weren’t getting any error logs for those messages. We contacted Amazon support to ask about this, and they were able to actually confirm the messages were in fact “errored” as a result of throttling.
We asked further into their recommendations for this - this must be a common problem right? They first off suggested upping our replay limit, which seemed an obvious no go. Replays occur for any failure, so that would just hammer our lambdas with bad requests when they came through. Asked also if there’s any way to differentiate the errors because we don’t care for throttling, we’d happily let those retry a dozen times if needed - but no. The other suggestion they had was to manage the queue ourselves from our lambdas. Build our own code within our lambdas to pull messages and then delete them after processing. This seems really counter-intuitive, though - why would every AWS consumer build their own infrastructure?
So I guess my question is, is this what others are doing? Are you using the built in lambda triggers? Are you creating your own code for managing queue consumption? Do you see these sorts of throttling, or is there anything we could do differently? Are there any difference with other services to manage this?
Edit: to clarify - I’m not looking to stop all throttling, I’m mostly curious to know if people are manually managing their queue receive/delete in their own lambdas, or if they are using the SQS lambda trigger, or another service
For the particular lambda in question, we have the concurrency set to 7 which we found a decent compromise of availability vs load on our database. This gist of the problem is the same though. As our load scales, we’ll hit more throttles. We can scale our concurrency up, we can scale our database up, but the same problem arises as load increases. My question isn’t focused so much on why throttling is occurring, but instead if this architectural approach is correct or if we should be building our own, or using a different AWS service.
Lambda concurrency and sqs is tricky. My comments assume you are using a standard queue, not FIFO…
My first question is do you really need the DLQ? Can you get “bad” messages on the queue that won’t process without some other intervention?
If you don’t have a DLQ, the message will be retried until the message expiration time is reached. Pauses between retry will increase exponentially, but the message will remain until expiration.
If you do need the DLQ, then you may need to add some automation to the DLQ to determine if the message was out there for throttling or a data issue. If for throttling, just push it back on the main queue.
You can also change the sqs redrive policy to increase the number of “failed” attempts before moving to the DLQ. If it’s 5 now, change it to 10 and see if your numbers are reduced sufficiently.
Yes, we can have “error” messages that we need to manually investigate, which is why we have the DLQ vs an endless retry loop.
add some automation to the DLQ
We are doing this - the messages are picked up and replayed every six hours (I believe). That has historically given us enough time to get in and pull out bad messages and figure them out, otherwise they just get tossed back in the pool for another go.
increase the number of “failed” attempts before moving
This was also suggested by Amazon, but we don’t think this is a good suggestion (for us at least) because then actual failures end up getting replayed more times before being tossed out of the queue. Extra db work, potential for data corruption, and extra error notifications to use.
One way would be to embed the Lambda into a Step Function instead of consuming an SQS. When the Lambda Service throws an exception (whether because of Concurrency or some other reason), you can have the Step Function catch the Exceptions and respond differently.
We’re designing an ElasticSearch based workflow and looking at doing the above for the same reason (we want the requests to be handled but without overloading the ElasticSearch cluster). We’ll catch the specific Concurrency exception (I think it’s Lambda.TooManyRequests?) exception and pass it to a Wait State.
All other exceptions can be caught and handled differently.
Yes, this is a known issue. In fact, it was my first major issue when using AWS.
The Lambda “poller” has no idea when it pulls items off the queue whether Lambda can instantiate another process. It also doesn’t look at the items in flight.
Most of the time I see this issue when people limit the concurrency of the target Lambda in an attempt to purposefully limit concurrency.
If you are seeing more than 1000 concurrent executions, you can ask support to raise the concurrency limit.
On the other hand, if you are trying to constrain the number of parallel executions, give up on Lambda. If you want to stay serverless, your easiest alternative is Fargate.
This is an interesting idea. We had started to go down the route of StepFunctions initially because our pipeline is largely linear (A → B → C). We had gone away from it because we ended up only doing one of the steps initially, and had encountered this throttling issue and not realized the same was the case for SQS. I’ll have to look into this again and see if we can split out those errors.
For a small workflow, with few routes and that will likely never change, yeah SFN doesn’t make much sense. But soon as you start thinking about potential modifications to the workflow, suddenly SFN states sounding appealing.
I see this issue when people limit the concurrency of the target Lambda
Yes, we are doing this. We could open hundreds more, but we’re currently limiting to seven instances concurrently, primarily for the sake of our database not getting thrashed.
Could you explain why Fargate is a better option in this scenario? I looked a bit into it, but I’m definitely not too familiar with Fargate or what its use case / differentiation from the other options is.