We are running a lot of serverless applications in AWS and I was really happy that AWS finally introduce Java 11 support for AWS Lambda.
I guess many teams already switched to Java 11 and think that it brings some performance improvement. AWS Lambda Java 11 was announced in November 2019. Lambda functions are written in Java 11 run on Amazon Linux 2, the latest generation of Amazon Linux, and Amazon Corretto 11. Sounds like it should be faster than old Java 8 stuff.
Unfortunately, there is no performance comparison between Java 8 and 11 runtimes for AWS Lambda.
Let's compare AWS Lamda's performance for Java 8 and 11 runtimes.
What our Lambda will do?
API Gateway+AWS Lambda+DynamoDB
This is the most popular architecture use case for AWS Lambda.
Let's write the code.
All code you can see as usual in my GitHub (https://github.com/Aleksandr-Filichkin/blog-aws-lambda-java-8-vs-11)
So Handler class implement RequestHandler and overrides one public handleRequest.
This is not a mistake, this is the AWS Lambda killer feature. Load classes, warm-up all clients on startup. Why? Because of AWS provides all available CPU(CPU Burst) during Lambda initialization(first lambda instance invocation). It reduces initialization significantly!
I highly recommend you to watch this excellent AWS Lambda Java tuning video
I improved the code as much as possible.
- Use CPU burst on Lambda startup
- Get rid of IoC frameworks such as Spring/Guice/Dagger 1
- Reduce jar file and load as fewer classes as possible. (Jar size is 6Mb, quite small for Java)