Enterprise Java

Insane AWS Lambda Speed Up

What makes this post annoying is that we’ve suffered from a slow Lambda cold start for a very long time, and the solution was literally a few seconds’ work.

I’ve written before about reducing bloat in Lambdas, especially the MySQL driver. However, the JVM is still quite slow to start.

I’ve learned to package AWS Lambdas using ZIP, rather than an uber jar, since that also seems to help.

However, there was a tiered compilation trick I’d read about, which suggested that you could get the JVM to do less up front optimisation on start-up, and therefore get on with things faster. In general, I’d be prepared to accept a lambda running a few milliseconds slower per request if most requests could be completed very quickly.

Thanks to some excellent help from the folks at AWS, who showed me this repo, the biggest improvement to our Lambda performance came from adding an environment variable.

Let me just kick myself again!

One environment variable was all we needed to ask to improve performance by a ridiculous quantity. That said, the previous improvements may also have been enormously useful.

Here’s all I added:

Environment:
  Variables:
    JAVA_TOOL_OPTIONS: '-XX:+TieredCompilation -XX:TieredStopAtLevel=1'

This was added within each lambda with a java11 runtime where I wanted startup speed instead of throughput.

I didn’t make the change on one of our batch processes where throughput was much more important.

Also Worth Trying

This made a difference, but I’d done a few things before this point:

  • Increase the memory size to 1Gb – this comes with CPU performance improvement too
  • Reduce all dependencies down to the minimum
  • Use MariaDB driver over MySQL
  • Use TinyLog in place of Log4j/Sl4j implementation

Published on Java Code Geeks with permission by Ashley Frieze, partner at our JCG program. See the original article here: Insane AWS Lambda Speed Up

Opinions expressed by Java Code Geeks contributors are their own.

Ashley Frieze

Software developer, stand-up comedian, musician, writer, jolly big cheer-monkey, skeptical thinker, Doctor Who fan, lover of fine sounds
Subscribe
Notify of
guest

This site uses Akismet to reduce spam. Learn how your comment data is processed.

0 Comments
Oldest
Newest Most Voted
Inline Feedbacks
View all comments
Back to top button