Microservices are great for some workflows. At Spectre, we use it for periodic high-concurrency compute jobs, processing thousands of media files. In that area, Amazon offers a pretty decent solution with AWS Lambda. However, Lambda has a few quirks that can range from midly annoying to downright frustrating.
One of those is Lambda’s insistance on automatically retrying failed invokations triggered from “asynchronous invocations” (e.g. S3).
Until the Lambda team gives us some kind of environment variable or setting to disable auto retries, here is a bit of code you can use in your Node Lambdas to mitigate this behaviour:
This code actually leverages Lambda’s quirks to make sure retried Lambda are aborted as soon as possible. It works because :
- Lambda not only reuses containers, it reuses whole Node processes, keeping global variables instanced and reusable. Useful for caching. Not so much for security and deterministic behaviour.
- Retried lambdas share the same request Id (e.g. 63f33da6–7833–11e7-a7a4–0badbb709c07)