so I am trying to build a queue (fifo) with a worker lambda. Simple enough and it works. But in case of an error, I want to reliably mark the failed workload as failed in a database (no retries!).
For some errors, I could just use a try-catch block. But in case of other issues (timeout e.g.), the lambda function just fails. The sqs message is “stuck” in “In-Flight” until visibility timeout is reached. Because the visibility timeout is quite high (lambda itself can run ~10min), the message gets put into the SQSs DLQ very late.
Lambda Destinations don’t work because it is not triggered asynchronously.
Is there a reliable way to immediately react to a failed lambda execution when triggered by SQS?
You could probably trigger something off an event bridge message. You can get an alarm raised when the lambda fails so you should be able hook onto that alarm to do something.
The message should appear in the DLQ after the visibility timeout expires. There is no way to speed this up if your process is hanging.
I’m getting the impression you don’t want to write much more code – if you do, I’d figure out WHY it hangs, and try breaking up the process into smaller chunks that are retryable over making a catch all error handler (which I’ve never seen implemented in a way that is pretty and understandable to all skill levels of programmers)
Avoiding that, my initial suggestion would be to attach a lambda to the deal letter queue that reads the message and makes the desired DB update… Should be just a few lines of code.
SQS doesn’t know that message processing failed though. It has a DLQ which I could use, but the message is added to the DLQ only when the visibility timeout passed (and no retries are configured). Until then, the group of messages with the same groupId is blocked, because the message is still “in-flight”.
But I want to run an action immediately after the worker function failed.
Oh, I have no problem with writing more code or a different solution. In fact, I did something similar without SQS. But considering that SQS is the go-to service for a queue, I figured I should go with that, but I faced the above mentioned “issue”. I’m just wondering if there is a way to handle failed lambda executions (and their payload) as fast as possible.
The catch-all solution is not something I seriously consider, as it is really not pretty as you mentioned
The issue is not so much SQS, it generally works very, very well. The issues are with integration, because SQS expects a response to know if the message is processed successfully or not. I see questions like this all the time – how do I fix SQS because it behaves a way I don’t want… SQS is a VERY simple service… The issue is that your design pattern doesn’t quite work with it.
If every message is tied to a DB entry, I’d have my lambda start up, update the DB with an in-progress record, enqueued a follow-up message for $LAMBDA_DURATION_SECONDS into the future, then delete the initial message from the queue.
You’ve now disconnected the non-retryable SQS system from your message flow, started the DB transaction, and made a little-code follow up that avoids managing timers alongside a processing thread.
The second message can check the state, and respond as desired, retry, whatever you want.
All this said, if it runs for up to 10 minutes, it sounds like you should break this into steps (using AWS tools, separate queues+lambdas, or your own message format if you want a mono-lambda).. maybe it’s not possible, but if this is an important process for you, it’s probably worth trying to add resiliency. I love AWS, but it loves to “blip” (AZ outage, service degradation, random hotkey throttle in Dynamo, random temporary capacity limitations, etc.) That’s probably the hardest lesson I had to deal with for the first 2 years building on AWS…
Just guessing but maybe separate retryable setup/information gathering from unreturnable processing/actions., And then follow it up with retryable notification/post-processing
Something I’ve seen people do is change the visibility timeout of the message in the lambda itself if they can somehow tell that the invoke is going to fail.
For them, they could tell that if lambda is taking more than X minutes, it’ll fail so they just made a call to sqs will would unblock their FIFO queue.