How To Reduce Cold Starts for Java Serverless Applications in AWS • Vadym Kazulkin • GOTO 2024

Vadym Kazulkin

Learn proven techniques to reduce cold start times in Java serverless apps on AWS Lambda, from SnapStart and GraalVM to memory optimization and priming strategies.

Key takeaways
  • Cold starts remain a significant challenge for Java serverless applications, with initial startup times of 2-3.5 seconds without optimizations

  • AWS SnapStart can reduce cold start times significantly:

    • Creates snapshots during deployment
    • Restores snapshots during execution
    • Free to use with managed Java on AWS Lambda
    • Can reduce cold starts to under 1 second
  • GraalVM native image compilation offers the best cold start performance:

    • Produces smaller deployment packages
    • Requires additional configuration for reflection
    • Build process needs 6-10GB memory
    • Can achieve sub-500ms cold starts
  • Memory allocation impacts cold start performance:

    • 1GB is optimal for most cases
    • Increasing beyond 1GB shows diminishing returns
    • CPU allocation is tied to memory settings
  • Priming technique further improves cold start times:

    • Initialize resources in static blocks
    • Pre-warm JSON marshalling
    • Initialize AWS clients during deployment
    • Can reduce cold starts below 100ms when combined with SnapStart
  • Best practices for reducing cold starts:

    • Use Lambda layers for dependencies
    • Remove unnecessary dependencies
    • Initialize clients and resources early
    • Consider async programming patterns
    • Package only what’s needed
  • Cold start frequency considerations:

    • Typically affects ~1% of invocations
    • AWS recycles containers periodically
    • Cache invalidation can trigger new cold starts
    • Multi-region deployments need separate optimization
  • HTTP client choice impacts performance:

    • AWS CRT client performs best for serverless
    • Apache client has more features but slower startup
    • Default URL connection client is not optimal
  • Java serverless adoption is growing:

    • From 4% to 10% of Lambda functions in past 3 years
    • Major frameworks now support serverless deployment
    • AWS actively investing in Java serverless tooling
  • Monitoring and measurement is crucial:

    • Test with production-like workloads
    • Consider P90/P99 latencies
    • Re-measure after Java/AWS updates
    • Account for regional differences