$30 off During Our Annual Pro Sale. View Details »

J-Fall 2022: Ten performance improvements for your Java based AWS Lambda functions

J-Fall 2022: Ten performance improvements for your Java based AWS Lambda functions

More and more enterprise organizations are moving toward the Cloud or want to adopt a more cloud-native operating model. Adopting serverless compute services like AWS Lambda is high on developers' wish lists.
Most of these organizations use Java for their existing applications, but Java is known for its high cold start times and high memory footprint in Serverless environments. Because of this, some developers switch to a different programming language when working with AWS Lambda, but you don't always have to do that. In this talk, I will share some of the lessons I learned that will help improve the performance of these Java-based Lambda functions.

After this talk, you should have some inspiration to squeeze even more performance out of your existing Java based serverless functions.

Jeroen Reijn

November 04, 2022
Tweet

More Decks by Jeroen Reijn

Other Decks in Technology

Transcript

  1. 14 performance

    improvements for your

    Serverless Java functions


    Jeroen Reijn
    # jfall.

    View Slide

  2. Foto: Annette Bernhardt (CC BY-SA 2.0)
    My name is Jeroen Reijn


    #Luminis #Java #AWS #Serverless | @jreijn

    View Slide

  3. Foto: Annette Bernhardt (CC BY-SA 2.0)
    Serverless functions
    🚀
    Photo by Vladimir Anikeev on Unsplash

    View Slide

  4. Foto: Annette Bernhardt (CC BY-SA 2.0)
    Java = Slow?
    🤔
    Photo by Razvan Cristea on Unsplash

    View Slide

  5. Foto: Annette Bernhardt (CC BY-SA 2.0)
    Cold 🥶 Starts
    Photo by Hakan Aldrin on Unsplash

    View Slide

  6. What are cold starts?
    Source: https://aws.amazon.com/blogs/compute/operating-lambda-performance-optimization-part-1/

    View Slide

  7. When do they happen?
    Source: https://aws.amazon.com/blogs/compute/operating-lambda-performance-optimization-part-1/

    View Slide

  8. How often do they happen?
    Source: https://aws.amazon.com/blogs/compute/operating-lambda-performance-optimization-part-1/

    View Slide

  9. Foto: Annette Bernhardt (CC BY-SA 2.0)
    So what can we do?

    View Slide

  10. Foto: Annette Bernhardt (CC BY-SA 2.0)
    1. More memory

    =

    More vCPUs
    Photo by Christian Wiediger on Unsplash

    View Slide

  11. Memory vs vCPUs
    Source: https://www.sentiatechblog.com/aws-re-invent-2020-day-3-optimizing-lambda-cost-with-multi-threading

    View Slide

  12. Memory vs vCPUs

    View Slide

  13. Memory vs vCPUs
    Source: https://catalog.workshops.aws/java-on-aws-lambda/en-US/02-accelerate/power-tuning

    View Slide

  14. Foto: Annette Bernhardt (CC BY-SA 2.0)
    2. Static initialization
    Photo by Michael Dziedzic on Unsplash

    View Slide

  15. Static Initialization

    View Slide

  16. Static Initialization

    View Slide

  17. Static Initialization

    View Slide

  18. Foto: Annette Bernhardt (CC BY-SA 2.0)
    3. Use lightweight
    dependencies
    Photo by Pedro Vit on Unsplash

    View Slide

  19. Lightweight dependencies

    View Slide

  20. Lightweight dependencies

    View Slide

  21. Lightweight dependencies

    View Slide

  22. Foto: Annette Bernhardt (CC BY-SA 2.0)
    4. Precon
    fi
    guration
    Photo by Rima Krucieneon Unsplash

    View Slide

  23. Be explicit in configuration

    View Slide

  24. Be explicit in configuration

    View Slide

  25. Be explicit in configuration

    View Slide

  26. Be explicit in configuration

    View Slide

  27. Be explicit in configuration

    View Slide

  28. Foto: Annette Bernhardt (CC BY-SA 2.0)
    5. Tiered Compilation
    Photo by Patti Black on Unsplash

    View Slide

  29. Tiered compilation
    Interpreter
    C1: No pro
    fi
    ling
    C1: Limited Pro
    fi
    ling
    C1: Full Pro
    fi
    ling
    C2
    0
    1
    2
    3
    4
    Compilation level
    Slower
    Faster

    View Slide

  30. Tiered compilation

    View Slide

  31. Tiered compilation

    View Slide

  32. Tiered compilation
    Settings Type # of
    invocations
    p90 (ms) p95 (ms) p99 (ms)
    Default settings Cold start 754 5,212 5,338 5,517
    Default settings Warm start 35,247 58 93 255
    Tiered compilation
    stopping at level 1
    Cold start 383 2,071 2,086 2,221
    Tiered compilation
    stopping at level 1
    Warm start 35,618 23 32 86
    Source: https://aws.amazon.com/blogs/compute/increasing-performance-of-java-aws-lambda-functions-using-tiered-compilation/

    View Slide

  33. Tiered compilation
    Settings Type # of
    invocations
    p90 (ms) p95 (ms) p99 (ms)
    Default settings Cold start 754 5,212 5,338 5,517
    Default settings Warm start 35,247 58 93 255
    Tiered compilation
    stopping at level 1
    Cold start 383 2,071 2,086 2,221
    Tiered compilation
    stopping at level 1
    Warm start 35,618 23 32 86
    Source: https://aws.amazon.com/blogs/compute/increasing-performance-of-java-aws-lambda-functions-using-tiered-compilation/

    View Slide

  34. Tiered compilation
    Settings Type # of
    invocations
    p90 (ms) p95 (ms) p99 (ms)
    Default settings Cold start 754 5,212 5,338 5,517
    Default settings Warm start 35,247 58 93 255
    Tiered compilation
    stopping at level 1
    Cold start 383 2,071 2,086 2,221
    Tiered compilation
    stopping at level 1
    Warm start 35,618 23 32 86
    Source: https://aws.amazon.com/blogs/compute/increasing-performance-of-java-aws-lambda-functions-using-tiered-compilation/

    View Slide

  35. Foto: Annette Bernhardt (CC BY-SA 2.0)
    6. Provisioned
    Concurrency
    Photo by Sigmund on Unsplash

    View Slide

  36. Concurrency
    As demand increases, the Lambda service
    increases concurrent executions


    • One function instance handles one
    request


    • Concurrency is a measure of
    concurrent executions

    View Slide

  37. Lambda initialisation

    View Slide

  38. Provisioned Concurrency
    Source: https://aws.amazon.com/blogs/compute/operating-lambda-performance-optimization-part-1/

    View Slide

  39. Provisioned Concurrency

    View Slide

  40. Autoscaling

    View Slide

  41. Autoscaling

    View Slide

  42. Foto: Annette Bernhardt (CC BY-SA 2.0)
    7. Frameworks
    Photo by Dayne Topkin on Unsplash

    View Slide

  43. Which Framework?

    View Slide

  44. Foto: Annette Bernhardt (CC BY-SA 2.0)
    8. No Framework
    Photo by Gemma Evans on Unsplash

    View Slide

  45. Foto: Annette Bernhardt (CC BY-SA 2.0)
    9. Package size
    Photo by Nam Anh on Unsplash

    View Slide

  46. Package size
    Jar size Libraries + Classpath scanning

    View Slide

  47. Foto: Annette Bernhardt (CC BY-SA 2.0)
    10. GraalVM

    AOT Native Image
    Photo by Patti Black on Unsplash

    View Slide

  48. GraalVM

    View Slide

  49. GraalVM
    Source: https://www.graalvmonlambda.com/walkthrough/graalvm-performance/

    View Slide

  50. Foto: Annette Bernhardt (CC BY-SA 2.0)
    11. CRaC
    Photo by Malcolm Lightbody on Unsplash

    View Slide

  51. Coordinated Restore at Checkpoint- CRAC
    • Creates a checkpoint (make an image of, snapshot) a Java instance while
    it is executing and then restores the state of the JVM from this snapshot


    • Part of OpenJDK


    • Experimental


    • Micronaut recently added support

    View Slide

  52. Foto: Annette Bernhardt (CC BY-SA 2.0)
    12. Parallelize
    Photo by the blowup on Unsplash

    View Slide

  53. Parallelize
    Leverage those vCPUs!

    View Slide

  54. Foto: Annette Bernhardt (CC BY-SA 2.0)
    13. Switch to ARM
    Photo by Christian Wiediger on Unsplash

    View Slide

  55. Foto: Annette Bernhardt (CC BY-SA 2.0)
    14. Be careful with
    layers
    Photo by Clark Van Der Beken on Unsplash

    View Slide

  56. Layers can add (a lot) of latency
    Removed OTEL layer

    View Slide

  57. Foto: Annette Bernhardt (CC BY-SA 2.0)
    Overall impact
    Photo by César Couto on Unsplash

    View Slide

  58. Migrating a Spring Boot application
    Source: https://catalog.workshops.aws/java-on-aws-lambda/en-US/01-migration/results

    View Slide

  59. Thanks for your attention


    Please rate my session in the J-Fall app
    # jfall.
    Reach out at @jreijn

    View Slide