In one of my recent project I got an opportunity to work on a Java based Lambda function. Here I am sharing few tips to reduce the cost and solution to the common AWS Lambda problems like Cold start.

  • Keep them warm!
    • Use provisioned concurrency – AWS feature.
    • Use Periodic ping e.g., health checks!
  • Load as much as you can/need during runtime bootstrap since you get boosted CPU access during that time but mind the 10 second limit
  • Keep JAR size as small as possible, avoid including unnecessary jars and classes.
  • Use libraries which are optimized for serverless for example micronaut.io, Spring Cloud Function. Avoid using any type of extra jar for small support, instead build them (for example avoid using Spring only for dependency injection).
  • Avoid reflection.
  • You get 2 core CPU after 1.8 GB memory – use Java concurrency with higher memory configuration to achieve true parallelism.
  • Use event driven architecture whenever possible
  • Use latest Java version (like Java 11 LTS) which has Modules support to reduce the size of deployable application, async HTTP client, and other performance fixes.