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

サーバーレスJava パフォーマンス選手権 at AWS Dev Day 2023 Tokyo

サーバーレスJava パフォーマンス選手権 at AWS Dev Day 2023 Tokyo

x-blood

June 26, 2023
Tweet

More Decks by x-blood

Other Decks in Programming

Transcript

  1. © 2023, Amazon Web Services, Inc. or its affiliates. All rights reserved.
    © 2023, Amazon Web Services, Inc. or its affiliates. All rights reserved.
    サーバーレスJava
    パフォーマンス選⼿権
    佐藤 靖幸
    C - 4 - 2
    デロイト トーマツ ウェブサービス株式会社

    View Slide

  2. © 2023, Amazon Web Services, Inc. or its affiliates. All rights reserved.
    本セッションについて
    パフォーマンス選⼿権 レギュレーション
    エントリー選⼿紹介
    パフォーマンス選⼿権 開催︕
    まとめ
    アジェンダ

    View Slide

  3. © 2023, Amazon Web Services, Inc. or its affiliates. All rights reserved.
    ⾃⼰紹介
    佐藤 靖幸
    得意領域
    バックエンド開発
    クラウドインフラ
    (Amazon Web Services)
    好きなAWSサービス
    Amazon VPC・AWS Lambda
    Amazon ECS・Amazon EKS
    © 佐藤靖幸 2023

    View Slide

  4. © 2023, Amazon Web Services, Inc. or its affiliates. All rights reserved.
    © 2023, Amazon Web Services, Inc. or its affiliates. All rights reserved.
    本セッションについて

    View Slide

  5. © 2023, Amazon Web Services, Inc. or its affiliates. All rights reserved.
    本セッションについて
    サーバーレスJavaは実装⽅法・起動⽅法に様々な選択肢があります。
    本セッションではサーバーレスJavaの実装⽅法と起動⽅法における
    代表的な⽅法をご紹介し、それぞれのベンチマークを取得して
    パフォーマンスを⽐較します
    併せて、それぞれの実装⽅法・起動⽅法の
    特徴とトレードオフをご紹介します

    View Slide

  6. © 2023, Amazon Web Services, Inc. or its affiliates. All rights reserved.
    併せて閲覧をおすすめする資料
    AWS Summit Tokyo 2023
    Java on Lambda のコールドスタート を乗り越える、
    これからの サーバーレスアプリケーション
    JJUG CCC 2023
    サーバーレスJavaの今 ~SnapStartとWeb Adapterを寄せて~

    View Slide

  7. © 2023, Amazon Web Services, Inc. or its affiliates. All rights reserved.
    © 2023, Amazon Web Services, Inc. or its affiliates. All rights reserved.
    パフォーマンス選⼿権
    レギュレーション

    View Slide

  8. © 2023, Amazon Web Services, Inc. or its affiliates. All rights reserved.
    AWS Lambda
    Runtime : Java 17 (Amazon Corretto 17)
    Timeout : 10 sec
    Memory : 2048 MB
    HTTP EndPoint : API Gateway HTTP API
    Database : DynamoDB
    Develop Environment : MacBook Air(M1, 2020)

    View Slide

  9. © 2023, Amazon Web Services, Inc. or its affiliates. All rights reserved.
    AWS アーキテクチャ構成
    三層アプリケーションをAWS Lambdaで実装
    データソースはAmazon DynamoDBを利⽤

    View Slide

  10. © 2023, Amazon Web Services, Inc. or its affiliates. All rights reserved.
    パフォーマンス測定⽅法
    K6を利⽤。同時に200リクエストをAPI Gatewayを対象に実⾏
    以下のカテゴリでパフォーマンスを測定
    • コールドスタートタイム(平均・ミリ秒)
    • E2E 90%タイル レイテンシー(平均・秒)
    • パッケージのビルド時間(秒)
    • ウォームアップ(合計・数)
    • パッケージサイズ(MB)

    View Slide

  11. © 2023, Amazon Web Services, Inc. or its affiliates. All rights reserved.
    © 2023, Amazon Web Services, Inc. or its affiliates. All rights reserved.
    エントリー選⼿の紹介

    View Slide

  12. © 2023, Amazon Web Services, Inc. or its affiliates. All rights reserved.
    パフォーマンス選⼿権 エントリー選⼿⼀覧
    1. Pure Java
    https://github.com/x-blood/awsdevday2023tokyo-sls-framework
    2. Pure Java SnapStart
    https://github.com/x-blood/awsdevday2023tokyo-sls-pure-java-snapstart
    3. Framework
    https://github.com/x-blood/awsdevday2023tokyo-sls-framework
    4. Framework SnapStart
    https://github.com/x-blood/awsdevday2023tokyo-sls-framework-snapstart
    5. Framework Native Image
    https://github.com/x-blood/awsdevday2023tokyo-sls-framework-native-image
    6. Framework Web Adapter
    https://github.com/x-blood/awsdevday2023tokyo-sls-framework-web-adapter
    7. Framework Web Adapter + Native Image
    https://github.com/x-blood/awsdevday2023tokyo-sls-framework-web-adapter-native-image

    View Slide

  13. © 2023, Amazon Web Services, Inc. or its affiliates. All rights reserved.
    1. Pure Java
    シンプルなJavaのLambda Hander
    ライブラリ管理にはMavenを利⽤
    Pros
    • 必要なライブラリを取捨選択
    • 軽量なパッケージサイズ
    Cons
    • アーキテクチャ移⾏が容易なクラス設計の推奨

    View Slide

  14. © 2023, Amazon Web Services, Inc. or its affiliates. All rights reserved.
    Pure Javaのコード設計(おさらい)
    Lambdaを呼び出すイベントの種類ごとに
    AWS SDK for Javaのイベントハンドラを利⽤
    イベントハンドラーを使うことで開発者体験が向上

    View Slide

  15. © 2023, Amazon Web Services, Inc. or its affiliates. All rights reserved.
    2. Pure Java SnapStart
    シンプルなJavaのLambda Handerに加えて、
    Lambda SnapStartを⽤いたJVM実⾏環境のスナップショットを利⽤
    Pros
    • リストアスタート時間による起動時間の減少
    Cons
    • JVMスナップショット特有の考慮点が必要

    View Slide

  16. © 2023, Amazon Web Services, Inc. or its affiliates. All rights reserved.
    SnapStart(おさらい)
    SnapStartを有効化すると、新しいLambdaバージョン発⾏時に
    ⾃動的にスナップショットを⽣成
    バージョンまたはエイリアス指定でLambdaを起動することで
    JVM環境の起動を⾼速化

    View Slide

  17. © 2023, Amazon Web Services, Inc. or its affiliates. All rights reserved.
    SnapStartの考慮点①
    ⼀意性の確保
    ⼀意となる情報( UUIDなど)の⼀意性の確保
    ネットワーク確⽴
    スナップショット復元後のネットワーク確⽴の担保
    外部API
    データベース接続

    View Slide

  18. © 2023, Amazon Web Services, Inc. or its affiliates. All rights reserved.
    SnapStartの考慮点②
    クラスのプリローディング
    スナップショット作成時に多くのクラスを
    事前ローディングしておくことでパフォーマンスが向上
    初期化中にクラスを事前ロードできない場合は、
    ダミー呼び出しを使⽤してクラスを事前ロード

    View Slide

  19. © 2023, Amazon Web Services, Inc. or its affiliates. All rights reserved.
    3. Framework
    AWS Lambdaでアプリケーションフレームワークを利⽤
    本セッションのエントリー選⼿はQuarkusを利⽤
    Pros
    • アプリケーションフレームワークの⾼度な機能
    • Pure Javaと⽐較してコンテナワークロードへの移植性の向上
    Cons
    • パッケージサイズの肥⼤化に伴うコールドスタート時間の増加

    View Slide

  20. © 2023, Amazon Web Services, Inc. or its affiliates. All rights reserved.
    Frameworkのサーバーレス統合
    AWS SDK for Javaのイベントハンドラクラスを活⽤しつつも
    フレームワークと統合
    拡張機能が提供されLambdaの機能拡張やビルドの簡潔さに寄与

    View Slide

  21. © 2023, Amazon Web Services, Inc. or its affiliates. All rights reserved.
    4. Framework SnapStart
    AWS Lambdaでアプリケーションフレームワークの機能を活⽤しつつ
    Lambda SnapStartも適⽤
    Pros
    • 肥⼤化したパッケージのスナップショット取得による
    コールドスタート時間の短縮
    Cons
    • JVMスナップショット特有の考慮点を
    フレームワークの範囲まで拡⼤

    View Slide

  22. © 2023, Amazon Web Services, Inc. or its affiliates. All rights reserved.
    Frameworkによるプリローディング機能
    パッケージのビルドを⾏う際に
    フレームワークが提供するAWS Lambda 統合機能を利⽤して
    プリローディング⽤のメタ情報を⾃動⽣成

    View Slide

  23. © 2023, Amazon Web Services, Inc. or its affiliates. All rights reserved.
    5. Framework Native Image
    Native Imageに最適化可能なアプリケーションフレームワークを活⽤
    アプリケーションフレームワークをNative Imageに
    Pros
    • アプリケーションフレームワークのNative Image最適化による
    ⾼速なコールドスタートを実現
    Cons
    • Graal VM特有のメタデータ管理を
    アプリケーションフレームワークの範囲まで拡⼤

    View Slide

  24. © 2023, Amazon Web Services, Inc. or its affiliates. All rights reserved.
    Native Imageの考慮点
    Javaの動的機能(リフレクションなど)を利⽤する場合は
    到達可能メタデータ(reflect-config.json)を⽣成
    トレース・エージェントツールを使⽤して
    メタデータを⾃動⽣成
    $JAVA_HOME/bin/java -
    agentlib:native-image-agent=config-
    output-dir=${OUTPUT_PATH}

    View Slide

  25. © 2023, Amazon Web Services, Inc. or its affiliates. All rights reserved.
    Framework × Native Image
    フレームワークを利⽤することで
    Native Imageビルドを簡潔に
    ビルドオプションやビルド⽤イメージ指定が可能

    View Slide

  26. © 2023, Amazon Web Services, Inc. or its affiliates. All rights reserved.
    6. Framework Web Adapter
    アプリケーションフレームワークをAWS Lambda Web Adapterで
    ほぼそのまま動作させる
    Pros
    • コンテナワークロード(ECS・EKSなど)への移⾏コストがない
    Cons
    • HTTPプロトコル前提
    EventBridge、SQS、StepFunctionsなどを利⽤する場合は
    統合パターンを考慮
    • コールドスタートの考慮

    View Slide

  27. © 2023, Amazon Web Services, Inc. or its affiliates. All rights reserved.
    Web Adapter(おさらい)
    フレームワークを(ほぼそのまま)
    Lambdaで動かす

    View Slide

  28. © 2023, Amazon Web Services, Inc. or its affiliates. All rights reserved.
    Web Adapterの考慮点
    (2023年5⽉30⽇時点)
    API Gateway, Lambda Functions URL,
    Application Load Balancer のみをサポート
    Amazon SQS、Amazon CloudWatch Eventなどによる
    インテグレーションは従来のイベントハンドラーを
    利⽤したハンドリングが実装観点においては
    より簡潔になる

    View Slide

  29. © 2023, Amazon Web Services, Inc. or its affiliates. All rights reserved.
    Dockerfileの例
    DockerfileにAWS Lambda Web Adapterの拡張機能を
    コピーする⼀⽂を加える(これだけ)

    View Slide

  30. © 2023, Amazon Web Services, Inc. or its affiliates. All rights reserved.
    7. Framework Web Adapter × Native Image
    AWS Lambda Web Adapterで動作させた
    アプリケーションフレームワークをNative Imageに
    Pros
    • JVM実⾏環境が不要になり、JVM環境初期化フェーズが不要に
    • コンテナイメージの場合はイメージサイズを軽量化することが可能
    • アプリケーションフレームワークを(ほぼそのまま)動かせる上に
    ⾼速なコールドスタートを実現
    Cons
    • Graal VM特有のメタデータ管理が必要

    View Slide

  31. © 2023, Amazon Web Services, Inc. or its affiliates. All rights reserved.
    Native ImageのDockerfile
    Native Image⽤のベースイメージを利⽤することで
    コンテナサイズを軽量化

    View Slide

  32. © 2023, Amazon Web Services, Inc. or its affiliates. All rights reserved.
    補⾜︓Native Imageのピーク時スループット
    Native ImageはJIT(Just In Time)のように実⾏しながら
    最適化を⾏うことができないため、
    パフォーマンス最適化を⾏っていない場合
    JITモードよりピーク時スループットが低い
    サーバーレスJavaはFaaSであるため
    JVM実⾏環境はリクエストごとに
    分散することが強み
    0
    5
    10
    15
    20
    25
    30
    Startup Speed
    Peak Throughput
    Max Latency
    Package Size
    Memory Footprint
    AOT JIT

    View Slide

  33. © 2023, Amazon Web Services, Inc. or its affiliates. All rights reserved.
    © 2023, Amazon Web Services, Inc. or its affiliates. All rights reserved.
    パフォーマンス選⼿権

    View Slide

  34. © 2023, Amazon Web Services, Inc. or its affiliates. All rights reserved.
    コールドスタートタイム(平均・ミリ秒)
    2,223.30
    965.13
    2,329.84
    506.67 413.18
    3,940.32
    584.34
    0.00
    500.00
    1,000.00
    1,500.00
    2,000.00
    2,500.00
    3,000.00
    3,500.00
    4,000.00
    4,500.00
    1. Pure Java 2. Pure Java SnapStart 3. Framework 4. Framework SnapStart 5. Framework Native
    Image
    6. Framework Web
    Adapter
    7. Framework Web
    Adapter × Native Image
    コールドスタートタイム(平均・ミリ秒)
    コールドスタートタイム(平均・ミリ秒)
    Native Imageが最も⾼速なコールドスタートタイムを獲得

    View Slide

  35. © 2023, Amazon Web Services, Inc. or its affiliates. All rights reserved.
    E2E 90%タイル レイテンシー(秒)
    2.58
    1.83
    2.71
    1.28
    0.81
    4.42
    1.01
    0.00
    0.50
    1.00
    1.50
    2.00
    2.50
    3.00
    3.50
    4.00
    4.50
    5.00
    1. Pure Java 2. Pure Java SnapStart 3. Framework 4. Framework SnapStart 5. Framework Native
    Image
    6. Framework Web
    Adapter
    7. Framework Web
    Adapter × Native Image
    E2E 90%タイル レイテンシー(秒)
    E2E 90%タイル レイテンシー(秒)
    レイテンシーはネイティブイメージが短く、Jarは⻑い

    View Slide

  36. © 2023, Amazon Web Services, Inc. or its affiliates. All rights reserved.
    パッケージのビルド時間(秒)
    4.34 3.65 4.35 3.80
    336.00
    5.89
    327.00
    0.00
    50.00
    100.00
    150.00
    200.00
    250.00
    300.00
    350.00
    400.00
    1. Pure Java 2. Pure Java SnapStart 3. Framework 4. Framework SnapStart 5. Framework Native
    Image
    6. Framework Web
    Adapter
    7. Framework Web
    Adapter × Native Image
    パッケージのビルド時間(秒)
    パッケージのビルド時間(秒)
    Jarは4秒前後。Native Imageは3~5分でビルド時間がネックに

    View Slide

  37. © 2023, Amazon Web Services, Inc. or its affiliates. All rights reserved.
    0 0 0 0 0 0
    1
    0
    0.2
    0.4
    0.6
    0.8
    1
    1.2
    1. Pure Java 2. Pure Java SnapStart 3. Framework 4. Framework SnapStart 5. Framework Native
    Image
    6. Framework Web
    Adapter
    7. Framework Web
    Adapter × Native Image
    ウォームアップ(合計・数)
    ウォームアップ(合計・数)
    ウォームアップ(合計)
    Native Imageがウォームアップ数を獲得。 Jarはウォームアップ数0

    View Slide

  38. © 2023, Amazon Web Services, Inc. or its affiliates. All rights reserved.
    パッケージサイズ(MB)
    12.20 12.20 14.20 14.20
    62.30
    24.10
    40.00
    0.00
    10.00
    20.00
    30.00
    40.00
    50.00
    60.00
    70.00
    1. Pure Java 2. Pure Java SnapStart 3. Framework 4. Framework SnapStart 5. Framework Native
    Image
    6. Framework Web
    Adapter
    7. Framework Web
    Adapter × Native Image
    パッケージサイズ(MB)
    パッケージサイズ(MB)
    Pure Javaが最も軽量なパッケージサイズを実現可能

    View Slide

  39. © 2023, Amazon Web Services, Inc. or its affiliates. All rights reserved.
    まとめ
    サーバーレスJavaに銀の弾丸はない
    しかしながら、ネイティブイメージに変換することで
    他のランタイムに引けを取らないまでに
    コールドスタートを⾼速化
    開発メンバーの習熟度から技術スタックを選択し
    AWS Lambda Web Adapter、Native Imageを
    プロダクションで活⽤︕よりよいサーバーレスJavaライフを︕

    View Slide

  40. © 2023, Amazon Web Services, Inc. or its affiliates. All rights reserved.
    Appendix GitHub
    1. Pure Java
    https://github.com/x-blood/awsdevday2023tokyo-sls-framework
    2. Pure Java SnapStart
    https://github.com/x-blood/awsdevday2023tokyo-sls-pure-java-snapstart
    3. Framework
    https://github.com/x-blood/awsdevday2023tokyo-sls-framework
    4. Framework SnapStart
    https://github.com/x-blood/awsdevday2023tokyo-sls-framework-snapstart
    5. Framework Native Image
    https://github.com/x-blood/awsdevday2023tokyo-sls-framework-native-image
    6. Framework Web Adapter
    https://github.com/x-blood/awsdevday2023tokyo-sls-framework-web-adapter
    7. Framework Web Adapter + Native Image
    https://github.com/x-blood/awsdevday2023tokyo-sls-framework-web-adapter-native-image

    View Slide

  41. © 2023, Amazon Web Services, Inc. or its affiliates. All rights reserved.
    Appendix 1. Pure Java Benchmark Details
    AWS Lambda Category Result Value
    Cold Starts Count 200
    Total Duration avg (ms) 2,223.30 ms
    Init Duration avg (ms) 1,605.63 ms
    Restore Duration avg (ms) -
    Max Memory Used avg (mb) 156.27 MB
    Build Times (sec) 4.35 sec
    Binary Size (mb) 12.20 MB
    E2E Category Result Value
    Duration avg (sec) 2.48 sec
    Duration 90% (sec) 2.58 sec
    Duration 95% (sec) 2.62 sec
    https://github.com/x-blood/awsdevday2023tokyo-sls-framework

    View Slide

  42. © 2023, Amazon Web Services, Inc. or its affiliates. All rights reserved.
    Appendix 2. Pure Java SnapStart Benchmark Details
    AWS Lambda Category Result Value
    Cold Starts Count 200
    Total Duration avg (ms) 965.13 ms
    Init Duration avg (ms) -
    Restore Duration avg (ms) 277.04 ms
    Max Memory Used avg (mb) 128.01 MB
    Build Times (sec) 3.65 sec
    Binary Size (mb) 12.20 MB
    E2E Category Result Value
    Duration avg (sec) 1.60 sec
    Duration 90% (sec) 1.83 sec
    Duration 95% (sec) 1.90 sec
    https://github.com/x-blood/awsdevday2023tokyo-sls-pure-java-snapstart

    View Slide

  43. © 2023, Amazon Web Services, Inc. or its affiliates. All rights reserved.
    Appendix 3. Framework Benchmark Details
    AWS Lambda Category Result Value
    Cold Starts Count 200
    Total Duration avg (ms) 2,329.84 ms
    Init Duration avg (ms) 2,242.34 ms
    Restore Duration avg (ms) -
    Max Memory Used avg (mb) 173.69 MB
    Build Times (sec) 4.35 sec
    Binary Size (mb) 14.20 MB
    E2E Category Result Value
    Duration avg (sec) 2.60 sec
    Duration 90% (sec) 2.71 sec
    Duration 95% (sec) 2.73 sec
    https://github.com/x-blood/awsdevday2023tokyo-sls-framework

    View Slide

  44. © 2023, Amazon Web Services, Inc. or its affiliates. All rights reserved.
    Appendix 4. Framework SnapStart Benchmark Details
    AWS Lambda Category Result Value
    Cold Starts Count 200
    Total Duration avg (ms) 506.67 ms
    Init Duration avg (ms) -
    Restore Duration avg (ms) 262.40 mx
    Max Memory Used avg (mb) 134.76 MB
    Build Times (sec) 3.80 sec
    Binary Size (mb) 14.20 MB
    E2E Category Result Value
    Duration avg (sec) 1.17 sec
    Duration 90% (sec) 1.28 sec
    Duration 95% (sec) 1.31 sec
    https://github.com/x-blood/awsdevday2023tokyo-sls-framework-snapstart

    View Slide

  45. © 2023, Amazon Web Services, Inc. or its affiliates. All rights reserved.
    Appendix 5. Framework Native Image Benchmark Details
    AWS Lambda Category Result Value
    Cold Starts Count 200
    Total Duration avg (ms) 413.18 ms
    Init Duration avg (ms) 269.52 ms
    Restore Duration avg (ms) -
    Max Memory Used avg (mb) 79.01 MB
    Build Times (sec) 336.00 sec
    Binary Size (mb) 62.30 MB
    E2E Category Result Value
    Duration avg (sec) 0.76 sec
    Duration 90% (sec) 0.81 sec
    Duration 95% (sec) 0.82 sec
    https://github.com/x-blood/awsdevday2023tokyo-sls-framework-native-image

    View Slide

  46. © 2023, Amazon Web Services, Inc. or its affiliates. All rights reserved.
    Appendix 6. Framework Web Adapter Benchmark Details
    AWS Lambda Category Result Value
    Cold Starts Count 200
    Total Duration avg (ms) 3940.32 ms
    Init Duration avg (ms) 2217.41 ms
    Restore Duration avg (ms) -
    Max Memory Used avg (mb) 206.41 MB
    Build Times (sec) 5.89 sec
    Binary Size (mb) 24.10 MB
    E2E Category Result Value
    Duration avg (sec) 4.14 sec
    Duration 90% (sec) 4.42 sec
    Duration 95% (sec) 4.57 sec
    https://github.com/x-blood/awsdevday2023tokyo-sls-framework-web-adapter

    View Slide

  47. © 2023, Amazon Web Services, Inc. or its affiliates. All rights reserved.
    Appendix 7. Framework Web Adapter Native Image Benchmark Details
    AWS Lambda Category Result Value
    Cold Starts Count 199
    Total Duration avg (ms) 584.34 ms
    Init Duration avg (ms) 471.34 ms
    Restore Duration avg (ms) -
    Max Memory Used avg (mb) 76.54 MB
    Build Times (sec) 327.00 sec
    Binary Size (mb) 40.00 MB
    E2E Category Result Value
    Duration avg (sec) 0.96 sec
    Duration 90% (sec) 1.01 sec
    Duration 95% (sec) 1.05 sec
    https://github.com/x-blood/awsdevday2023tokyo-sls-framework-web-adapter-native-image

    View Slide

  48. © 2023, Amazon Web Services, Inc. or its affiliates. All rights reserved.
    Thank you!
    © 2023, Amazon Web Services, Inc. or its affiliates. All rights reserved.
    佐藤 靖幸
    @yasai_ls

    View Slide

  49. © 2023, Amazon Web Services, Inc. or its affiliates. All rights reserved.
    Please complete
    the session survey
    © 2022, Amazon Web Services, Inc. or its affiliates. All rights reserved.

    View Slide