Tackling Java cold startup times on AWS Lambda with GraalVM

Click for: original source

Have you ever tried running a Java application on AWS Lambda? Well, even the simplest Java application takes significant time to start up at first. The reason behind is simple. AWS has to prepare a runtime environment for your application when it executes the first time. This is called cold-start. By Arnold Galovics.

When the environment is already prepared, the JVM is ready to go and only your application code needs to be invoked. This is called warm-start. Although after some time, a warm Lambda environment will be killed by AWS in case theres no invocation, and the same cold-start will occur.

I was always wondering what GraalVM is capable of. Ive read several articles how great it is, and how unbeliavable it speeds up Java applications. Ive tried it myself with a simple Spring Boot app, and well. The startup time was really impressive. But I didnt really get why startup time is so crucial for a normal Spring Boot application. Then it hit me, how great it would be if GraalVM is used in the serverless world with AWS Lambda.

The article then covers:

  • The idea
  • Custom Lambda runtimes
  • Putting the puzzle together
  • Complicating it with DynamoDB

Running native images in the Lambda environment is impressive. However it definitely brings in some complexity if you deal with libraries that are using reflection. The good thing is, GraalVM provides a special agent that can be attached to your application to record what classes/resources/etc are being used during runtime and generate an initial version of the configuration files. You can find the full code here on GitHub. See the charts comparing cold starts for default Java runtime and native code versus Node.js implementation. Excellent read!

[Read More]

Tags java performance programming learning containers software-architecture