Upgrade to Pro — share decks privately, control downloads, hide ads and more …

モダンアプリケーションにおける分散トランザクションの動機と実装 @ AWS Dev Day 2023 Tokyo

モダンアプリケーションにおける分散トランザクションの動機と実装 @ AWS Dev Day 2023 Tokyo

AWS Dev Day 2023 Tokyo のE-1セッションのスライドです。
分散トランザクションの動機と実装について喋りました。

Yuji Nomura

June 24, 2023
Tweet

More Decks by Yuji Nomura

Other Decks in Technology

Transcript

  1. © 2023, Amazon Web Services, Inc. or its affiliates. All

    rights reserved. D E V D AY J U N E 2 3 , 2 0 2 3
  2. © 2023, Amazon Web Services, Inc. or its affiliates. All

    rights reserved. © 2023, Amazon Web Services, Inc. or its affiliates. All rights reserved. モダンアプリケーションにおける 分散トランザクションの 動機と実装パターン Yuji Nomura E - 1 Senior Solutions Architect, Enterprise Majority Focused Amazon Web Service Japan
  3. © 2023, Amazon Web Services, Inc. or its affiliates. All

    rights reserved. 本セッションについて • 話すこと • トランザクションの分離についての俯瞰的な考察 • (ほとんど)話さないこと • 各パターンの具体的な AWS サービス実装 • 対象聴衆 • マイクロサービスの設計イメージについて知りたい⽅ • 分散トランザクションの最近の状況について俯瞰的に知りたい⽅
  4. © 2023, Amazon Web Services, Inc. or its affiliates. All

    rights reserved. • デプロイの独⽴性とモダンアプリケーションの⽬的 • マイクロサービスの実装とトランザクションへの影響 • トランザクション分割の⽅針 • トランザクション分割実装パターン ⽬次
  5. © 2023, Amazon Web Services, Inc. or its affiliates. All

    rights reserved. ビジネスにおける価値 原則 プラクティス デプロイの独⽴性 結果整合性 トランザクション の分割 俊敏性 スケーラ ビリティ 弾⼒性 前半 データレイヤーのプラクティス マイクロサービス アーキテクチャ
  6. © 2023, Amazon Web Services, Inc. or its affiliates. All

    rights reserved. 後半 コレオグラフィ オーケストレーション 同⼀ DB の トランザクション処理 データの関係性 補償要件・エラー要件 ⼀貫性 Saga パターン 疎 密 緩い 厳しい 強い 弱い 分散合意系 結果整合 強整合 Event データ伝播
  7. © 2023, Amazon Web Services, Inc. or its affiliates. All

    rights reserved. © 2023, Amazon Web Services, Inc. or its affiliates. All rights reserved. デプロイの独⽴性と モダンアプリケーションの⽬的 デプロイの独⽴性 結果整合性 トランザクションの 分割 俊敏性 スケーラビ リティ 弾⼒性 データレイヤーのプラクティス マイクロサービス アーキテクチャ
  8. © 2023, Amazon Web Services, Inc. or its affiliates. All

    rights reserved. Independently Deployable (デプロイの独⽴性・独⽴デプロイ可能性・独⽴してデプロイ可能) • コード単位 • 開発プロセス単位 • リソース単位 ビルド・デプロイ 開発プロセス Int main(void) { printf(”Hello, world!”); printf(“Bye, world!”); return 0; } コード ---------- ---------- ---------- ---------- ---------- bin bin 本番環境 リソース
  9. © 2023, Amazon Web Services, Inc. or its affiliates. All

    rights reserved. Independently Deployable (デプロイの独⽴性・独⽴デプロイ可能性・独⽴してデプロイ可能) • メリット • チームの⾃律性 • 開発からリリースまでのスピード感 • 可⽤性・スケーラビリティ・弾⼒性
  10. © 2023, Amazon Web Services, Inc. or its affiliates. All

    rights reserved. 現代のビジネス要求 • 俊敏性 • スケーラビリティ • 弾⼒性
  11. © 2023, Amazon Web Services, Inc. or its affiliates. All

    rights reserved. 現代のビジネス要求 • 俊敏性 • スケーラビリティ • 弾⼒性 モダンアプリケーションアーキテクチャによる 原則化、プラクティス化 マイクロサービス サーバーレス コンテナ イベント駆動 AWS Lambda デプロイの独⽴性は原則として下⽀え
  12. © 2023, Amazon Web Services, Inc. or its affiliates. All

    rights reserved. ⽬的の明確化 • 分割⾃体は⽬的ではない • 組織にとっての⽬標を指標化する
  13. © 2023, Amazon Web Services, Inc. or its affiliates. All

    rights reserved. デプロイの独⽴性の実装 • マイクロサービスアーキテクチャ • ビジネスドメインでの分解 • ⼩さく、かつ1つの役割に専念 • ⾃律性(疎結合) • 独⽴した開発単位 • 独⽴した開発プロセス • 独⽴したリソース単位 • 組織論な⾯も⼤きい
  14. © 2023, Amazon Web Services, Inc. or its affiliates. All

    rights reserved. © 2023, Amazon Web Services, Inc. or its affiliates. All rights reserved. マイクロサービスにおける データレイヤーの設計 デプロイの独⽴性 結果整合性 トランザクションの 分割 俊敏性 スケーラビ リティ 弾⼒性 データレイヤーのプラクティス マイクロサービス アーキテクチャ
  15. © 2023, Amazon Web Services, Inc. or its affiliates. All

    rights reserved. デプロイの独⽴性とサービス分割の単位 • アーキテクチャ的な分離の観点は︖ • データレイヤーの分離は︖ サービス B サービス A サービス D サービス C ︖ RDB
  16. © 2023, Amazon Web Services, Inc. or its affiliates. All

    rights reserved. データレイヤーも分離すること サービス A サービス B A ⽤ B ⽤ ⾃分のエリアを跨いだや りとり 同⼀トランザクション
  17. © 2023, Amazon Web Services, Inc. or its affiliates. All

    rights reserved. サービス A サービス B A ⽤ B ⽤ 変更の影響が 伝播 データレイヤーも分離すること スケールの しにくさ 結合度が⾼まる 危険性
  18. © 2023, Amazon Web Services, Inc. or its affiliates. All

    rights reserved. A ⽤ B ⽤ サービス A サービス B サービス API データレイヤーも分離すること 変更の影響を 受けにくい 単独でスケー ル可能 データの結合度 の低下
  19. © 2023, Amazon Web Services, Inc. or its affiliates. All

    rights reserved. A ⽤ B ⽤ サービス A サービス B サービス API データレイヤーも分離すること トランザクション の分割が発⽣
  20. © 2023, Amazon Web Services, Inc. or its affiliates. All

    rights reserved. (参考) トランザクションによる安全の保証 • ACID特性 • Atomicity (原⼦性) • Consistency • Isolation (分離性) • Durability 時間 Commit 変更 B 変更 A Read 変更 A は⾒えない Write
  21. © 2023, Amazon Web Services, Inc. or its affiliates. All

    rights reserved. 分散アプリケーションでは整合性の考え⽅が変わる Atomic かつ Isolated Atomic でも Isolated でもない
  22. © 2023, Amazon Web Services, Inc. or its affiliates. All

    rights reserved. © 2023, Amazon Web Services, Inc. or its affiliates. All rights reserved. トランザクション分割の⽅針 デプロイの独⽴性 結果整合性 トランザクションの 分割 俊敏性 スケーラビ リティ 弾⼒性 データレイヤーのプラクティス マイクロサービス アーキテクチャ
  23. © 2023, Amazon Web Services, Inc. or its affiliates. All

    rights reserved. A ⽤ B ⽤ サービス A サービス B サービス API マイクロサービスの分割の考え⽅ ビジネスドメインの 境界と同⼀にしたい
  24. © 2023, Amazon Web Services, Inc. or its affiliates. All

    rights reserved. マイクロサービスの分割の考え⽅ • ビジネスドメインとサービスの分割境界を同⼀に • ⾼凝集・疎結合 直販 ⼩売 SCM 製造 発注
  25. © 2023, Amazon Web Services, Inc. or its affiliates. All

    rights reserved. ドメイン駆動設計(DDD) • 設計における意思決定とドメイン 設計の議論における技術的な⽤語の広義 のフレームワークを提供 • ユビキタス⾔語 ー ビジネスドメイン エキスパートと開発者の間の意思疎通と して利⽤される⽤語によってモデリング と設計を⾏う • 戦略的な設計のためのガイドライン ー 境界づけられたコンテキスト、 蒸留、⼤規模な構造の考察 「エリック・エヴァンスのドメイン駆動設計」︓エリック・エバンス著 2011/4/9 翔泳社 「実践ドメイン駆動設計」︓ヴォーン・ヴァーノン著 2015/3/16 翔泳社
  26. © 2023, Amazon Web Services, Inc. or its affiliates. All

    rights reserved. ドメイン駆動設計で定義される境界 集約(Aggregate) • ⼀貫性(トランザクション)を保持する必要のある境界 • ⾃⼰完結した状態を管理する 境界づけられたコンテキスト(Bounded Context) • 関連する集約のコレクション • より広い世界への明確なインターフェースを備える
  27. © 2023, Amazon Web Services, Inc. or its affiliates. All

    rights reserved. ドメイン 集約を跨ぐのであれば トランザクションは 切れているべき 境界づけられたコンテキスト A 境界づけられたコンテキスト B 境界づけられたコンテキスト C 集約 A1 集約 A2 集約 B3 集約 C4 = マイクロサービスの単位 〜
  28. © 2023, Amazon Web Services, Inc. or its affiliates. All

    rights reserved. A ⽤ B ⽤ サービス A サービス B サービス API トランザクション境界を意識した設計 異なる集約同⼠は トランザクション を分割 集約 1 集約 2
  29. © 2023, Amazon Web Services, Inc. or its affiliates. All

    rights reserved. 開発者たちは、アトミックな変更が必須であると 洗脳されているのが⼀般的だ。 (しかし)ドメインエキスパートたちは整合性を 保つのに時間がかかったとしても、それが合理的な 遅延であれば受け⼊れてくれるものだ。 実践ドメイン駆動設計 / Vaughn Vernon 第⼗章 集約 10.5.「ルール︓境界の外部では結果整合性を⽤いる」
  30. © 2023, Amazon Web Services, Inc. or its affiliates. All

    rights reserved. (参考) Consistency Model ⼀貫性モデル・整合性モデル • 強い整合性 • 例︓線形化可能性(Linearizability) • 変更が遅延なく伝わる • 評価順や読み取り順が仕組みによって制御される • 弱い整合性 • 例︓結果整合性(Eventual Consistency) • A であった変更が B にいつか伝わる • 過去の状態が⾒える • 全体として⼀致しない状態が制御できない形で表出する Consistencyって 200種類あんねん Linearizability External Consistency Quiescent Consistency Sequential Consistency Release Consistency Casual Consistency Processor Consistency PRAM Consistency Read Your Write Consistency Eventual Consistency Cache Consistency Slow Consistency Monotonic read Consistency Monotonic Write Consistency ……
  31. © 2023, Amazon Web Services, Inc. or its affiliates. All

    rights reserved. まず結果整合性を検討する • DDDやマイクロサービスでなくても同じ⽂脈 • まずビジネスとシステムの関係を考えること • 分割におけるドメインエキスパートとの上流⼯程の重要性
  32. © 2023, Amazon Web Services, Inc. or its affiliates. All

    rights reserved. 結果整合性が受け⼊れられない場合 • サービス境界の⾒直し • トレードオフを受け⼊れて結合度の⾼い⽅法を選択
  33. © 2023, Amazon Web Services, Inc. or its affiliates. All

    rights reserved. ビジネスにおける価値 原則 プラクティス デプロイ独⽴性 結果整合性 トランザクション の分割 俊敏性 スケーラ ビリティ 弾⼒性 ここまでのまとめ データレイヤーのプラクティス マイクロサービス アーキテクチャ
  34. © 2023, Amazon Web Services, Inc. or its affiliates. All

    rights reserved. © 2023, Amazon Web Services, Inc. or its affiliates. All rights reserved. トランザクション分割実装パターン
  35. © 2023, Amazon Web Services, Inc. or its affiliates. All

    rights reserved. トランザクションを分割するパターン コレオグラフィ オーケストレーション 同⼀ DB の トランザクション処理 データの関係性 補償要件・エラー要件 ⼀貫性 Saga パターン 疎 密 緩い 厳しい 強い 弱い 分散合意系 結果整合 強整合 Event データ伝播
  36. © 2023, Amazon Web Services, Inc. or its affiliates. All

    rights reserved. トランザクションを分割するパターン コレオグラフィ オーケストレーション 同⼀ DB の トランザクション処理 データの関係性 補償要件・エラー要件 ⼀貫性 Saga パターン 疎 密 緩い 厳しい 強い 弱い 分散合意系 結果整合 強整合 Event データ伝播
  37. © 2023, Amazon Web Services, Inc. or its affiliates. All

    rights reserved. Saga パターン • もともと⻑命トランザクション(LLT)を扱うための⼿法 • 分散アプリケーションにおけるトランザクション管理の本命 • 失敗した時の補償トランザクションを含む • 考え⽅ • 分割されたトランザクションをそのまま順番に実⾏ • 失敗したらロールバックするまでを考える • サービスごとにトランザクションが決定するのが⾒える 1 2 -2 -1 3 OK OK OK NG NG rollback Commit
  38. © 2023, Amazon Web Services, Inc. or its affiliates. All

    rights reserved. Saga パターン 1 2 -2 -1 3 OK OK OK NG NG rollback Commit オーケストレーション オーケストレーター 状態 ストア 実⾏ 実⾏ 実⾏ サービスA サービスB サービスC サービス A サービス B サービス D コレオグラフィ Done ⾮同期的なトリガー サービス C
  39. © 2023, Amazon Web Services, Inc. or its affiliates. All

    rights reserved. Saga の上⼿な使い⽅ • トランザクションの境界に注意 • サービス間では Dirty Read が発⽣する • サービス内は ACID 特性を守ることができる • その他のテクニック • セマンティックロック(ロジック内のロック) • 冪等性 サービス同⼠の トランザクション境界 冪等性 A ⽤ B ⽤ 集約 1 集約 2 何回呼んでも同じ結果
  40. © 2023, Amazon Web Services, Inc. or its affiliates. All

    rights reserved. Saga の利⽤例 • ビジネストランザクションを中⼼に分割と補償を考える • 予約の仕組み • 引き落としと振込 • 注⽂と在庫確保 予約 ⽀払 払戻 予約解放 確定 OK OK 実施完了 キャンセル ⽀払失敗 オーケストレーター 予約 ⽀払 予約サービス ⽀払サービス 予約管理 サービス 予約状況確認 予約解放 払戻
  41. © 2023, Amazon Web Services, Inc. or its affiliates. All

    rights reserved. AWS Step Functions 1 2 -2 -1 3 OK OK OK NG NG rollback Commit オーケストレーションのフルマネージドサービス オーケストレーター 状態 ストア 実⾏ 実⾏ 実⾏ サービス A サービス B サービス C
  42. © 2023, Amazon Web Services, Inc. or its affiliates. All

    rights reserved. イベント駆動 トランザクションを分割するパターン コレオグラフィ オーケストレーション 同⼀ DB の トランザクション処理 データの関係性 補償要件・エラー要件 ⼀貫性 Saga パターン 疎 密 緩い 厳しい 強い 弱い 分散合意系 結果整合 強整合 Event データ伝播
  43. © 2023, Amazon Web Services, Inc. or its affiliates. All

    rights reserved. 参照的にデータを共有する⽅法 • バッチ処理でのレプリケーション • APIやVIEWを使ったデータ公開 • イベント駆動アーキテクチャ
  44. © 2023, Amazon Web Services, Inc. or its affiliates. All

    rights reserved. event [i-’vent] 名詞 状態が変更されたことを⽰す シグナル
  45. © 2023, Amazon Web Services, Inc. or its affiliates. All

    rights reserved. イベントは観察可能であり、指⽰ではない コマンドの指⽰ 請求書を 発⾏して ください。 承知 しました イベントの観察 Xさんが今 ⼩物を注⽂ しました 請求書を 送ります セールスレポート に追加します。
  46. © 2023, Amazon Web Services, Inc. or its affiliates. All

    rights reserved. イベント駆動アーキテクチャ 注⽂ サービス 請求 サービス ⼊⾦ サービス 販売予測 サービス プロデューサ コンシューマ イベント ルーター ルーティング/ フィルタ/ ルール キュー キュー キュー
  47. © 2023, Amazon Web Services, Inc. or its affiliates. All

    rights reserved. Saga パターンにおけるコレオグラフィ サービス A サービス B サービス D Done サービス C イベント イベントルーティング
  48. © 2023, Amazon Web Services, Inc. or its affiliates. All

    rights reserved. イベントソーシング ドメイン(アプリケーション)で発⽣したイベントを他のドメイン に供給するためのデザインパターン イベント1 イベント2 イベント3 イベント4 プロセス プロセス プロセス
  49. © 2023, Amazon Web Services, Inc. or its affiliates. All

    rights reserved. トランザクションを分割するパターン コレオグラフィ オーケストレーション 同⼀ DB の トランザクション処理 データの関係性 補償要件・エラー要件 ⼀貫性 Saga パターン 疎 密 緩い 厳しい 強い 弱い 分散合意系 結果整合 強整合 Event データ伝播
  50. © 2023, Amazon Web Services, Inc. or its affiliates. All

    rights reserved. サービスA サービスB A⽤ B⽤ 変更の影響が 伝播 同⼀DBのトランザクション処理 スケールの しにくさ 結合度が⾼まる 危険性 • サービスがDBを共有する場合かなり注意が必要
  51. © 2023, Amazon Web Services, Inc. or its affiliates. All

    rights reserved. (参考)モジュラーモノリス A B C D E A, B C D E
  52. © 2023, Amazon Web Services, Inc. or its affiliates. All

    rights reserved. トランザクションを分割するパターン コレオグラフィ オーケストレーション 同⼀ DB の トランザクション処理 データの関係性 補償要件・エラー要件 ⼀貫性 Saga パターン 疎 密 緩い 厳しい 強い 弱い 分散合意系 結果整合 強整合 Event データ伝播
  53. © 2023, Amazon Web Services, Inc. or its affiliates. All

    rights reserved. 分散合意とは ネットワーク的に分離している 参加者間で全体として⼀つの 結論を選択できるのか︖ 故障しない 故障がすぐわかる 故障がそのうちわかる 故障するが復旧する 邪悪な⼈が邪魔をする 故障モデル 難しい
  54. © 2023, Amazon Web Services, Inc. or its affiliates. All

    rights reserved. 分散合意の使⽤例 in AWS Amazon DynamoDB の Multi-Paxos https://www.usenix.org/system/files/atc22-elhemali.pdf より Amazon Aurora のクォーラムシステム AWS Summit Tokyo 2018 セッションより
  55. © 2023, Amazon Web Services, Inc. or its affiliates. All

    rights reserved. アプリケーションへの応⽤(分散コミット) • ⼆相コミット(2PC)のリスク ロックの待ち時間によるレイテンシーの増加 通信障害などによるロック状態の固定 調停者 調停者 第1フェーズ 第2フェーズ commit OK? OK! commit! 更新 更新 commit OK? OK! commit! ロック ロック 故障しない 故障がすぐわかる 故障がそのうちわかる 故障するが復旧する 邪悪な⼈が邪魔をする 故障モデル 難しい ここの障害 に弱い
  56. © 2023, Amazon Web Services, Inc. or its affiliates. All

    rights reserved. アプリケーションへの応⽤(分散コミット) • ⼆相コミットはアンチパターンになってしまった • クラウドの⽂脈で親和性が低い • ハードとの関わりの強い冗⻑構成になる • そもそも故障しないことが前提で⼿動オペレーションによる復旧を想定
  57. © 2023, Amazon Web Services, Inc. or its affiliates. All

    rights reserved. 新たな分散コミットの例 故障しない 故障がすぐわかる 故障がそのうちわかる 故障するが復旧する 邪悪な⼈が邪魔をする 故障モデル 難しい サービス 調停者群 Paxos Commit DB1 DB2
  58. © 2023, Amazon Web Services, Inc. or its affiliates. All

    rights reserved. 分散コミットのアプリケーション利⽤への是⾮ • トレードオフを考える • アルゴリズムが複雑で厳密性が必要であること • 典型的には過半数のノードが⽣きていることが前提 • 調停ノード群の⾦銭的なコスト • パフォーマンスへの影響
  59. © 2023, Amazon Web Services, Inc. or its affiliates. All

    rights reserved. トランザクションを分割するパターン コレオグラフィ オーケストレーション 同⼀ DB の トランザクション処理 データの関係性 補償要件・エラー要件 ⼀貫性 Saga パターン 疎 密 緩い 厳しい 強い 弱い 分散合意系 結果整合 強整合 Event データ伝播
  60. © 2023, Amazon Web Services, Inc. or its affiliates. All

    rights reserved. ビジネスにおける価値 原則 プラクティス デプロイの独⽴性 結果整合性 トランザクション の分割 俊敏性 スケーラ ビリティ 弾⼒性 まとめ データレイヤーのプラクティス マイクロサービス アーキテクチャ
  61. © 2023, Amazon Web Services, Inc. or its affiliates. All

    rights reserved. Thank you! © 2023, Amazon Web Services, Inc. or its affiliates. All rights reserved. Yuji Nomura Twitter : @ugnomura [email protected]
  62. © 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.
  63. © 2023, Amazon Web Services, Inc. or its affiliates. All

    rights reserved. © 2023, Amazon Web Services, Inc. or its affiliates. All rights reserved. Appendix
  64. © 2023, Amazon Web Services, Inc. or its affiliates. All

    rights reserved. 参考⽂献 デプロイの独⽴性・マイクロサービスアーキテクチャ • マイクロサービスアーキテクチャ / Sam Newman ドメイン駆動設計 • エリック・エヴァンスのドメイン駆動設計 / Eric Evans • 実践ドメイン駆動設計 / Vaughn Vernon ⼀貫性モデル • データ指向アプリケーションデザイン / Martin Kleppman • 詳説データベース / Alex Petrov • The Art of Multiprocessor Programming / Maurice Herlihy Saga パターン • ソフトウェアアーキテクチャ・ハードパーツ / Neal Ford 他