applying the saga pattern
play

Applying the Saga Pattern Caitie McCafffrey Caitie McCaffrey - PowerPoint PPT Presentation

Applying the Saga Pattern Caitie McCafffrey Caitie McCaffrey Distributed Systems Engineer @Caitie CaitieM.com Why Sagas? Sagas Paper Distributed Sagas Sagas in Halo 4 Systems Used to Be Simple Serializability & ACID


  1. Applying the Saga Pattern Caitie McCafffrey

  2. Caitie McCaffrey � Distributed Systems Engineer @Caitie CaitieM.com

  3. • Why Sagas? • Sagas Paper • Distributed Sagas • Sagas in Halo 4

  4. Systems Used to Be Simple

  5. Serializability & ACID

  6. SOA/Microservices

  7. Two Phase Commit

  8. 2PC: Prepare Phase

  9. 2PC: Commit Phase

  10. 2PC Doesn’t Scale • O(n^2) Messages • Coordinator: Single Point of Failure • Reduced Throughput

  11. Spanner Google’s Globally-Distributed Database Corbett et. al.

  12. “Spanner is Google’s scalable, multi-version, globally distributed , and synchronously- replicated database. It is the first system to distribute data at global scale and support externally-consistent distributed transactions .” –Corbett et al.

  13. “The key enabler of these properties is a new TrueTime API and its implementation…using multiple modern clock references ( GPS and atomic clocks ).” –Corbett et al.

  14. Spanner is Expensive & Proprietary • Custom Hardware & Data-Centers • Synchronization Not Solved

  15. Distributed Transactions are Hard & Expensive

  16. Can We Do Better?

  17. Can We Do Better?

  18. Sagas Hector Garcia-Molina, Kenneth Salem Princeton University 1987

  19. Sagas are Long Lived Transactions

  20. “A Saga is a Long Lived Transaction that can be written as a sequence of transactions that can be interleaved. All transactions in the sequence complete successfully or compensating transactions are ran to amend a partial execution.”

  21. A Saga is a Collection of Sub-Transactions T 1 , T 2 … T n

  22. Each Sub-Transaction has a Compensating Transaction C 1 , C 2 … C n

  23. C n Semantically Undoes T n

  24. Saga Guarantee Either • T 1 , T 2 … T n or • T 1 , T 2 … T j, C j , … C 2 , C 1

  25. Trade-Off: Atomicity for Availability

  26. Sagas are a Failure Management Pattern

  27. Large Single Transaction

  28. Sagas • Book Hotel (T 1 ) • Cancel Hotel (C 1 ) • Book Car (T 2 ) • Cancel Car (C 2 ) • Book Flight (T 3 ) • Cancel Flight (C 3 )

  29. Saga Execution Coordinator (SEC)

  30. Saga Log • Begin T i • Begin Saga • End T i • End Saga • Begin C i • Abort Saga • End C i

  31. Successful Saga Begin Saga � Start Book Hotel (T 1 ) End Book Hotel (T 1 ) Start Book Car Rental (T 2 ) End Book Car Rental (T 2 ) Start Book Flight (T 3 ) End Book Flight (T 3 ) End Saga

  32. Successful Saga Begin Saga Start Book Hotel (T 1 ) � End Book Hotel (T 1 ) Start Book Car Rental (T 2 ) End Book Car Rental (T 2 ) Start Book Flight (T 3 ) End Book Flight (T 3 ) End Saga

  33. Successful Saga Begin Saga Start Book Hotel (T 1 ) End Book Hotel (T 1 ) � Start Book Car Rental (T 2 ) End Book Car Rental (T 2 ) Start Book Flight (T 3 ) End Book Flight (T 3 ) End Saga

  34. Successful Saga Begin Saga Start Book Hotel (T 1 ) End Book Hotel (T 1 ) Start Book Car Rental (T 2 ) � End Book Car Rental (T 2 ) Start Book Flight (T 3 ) End Book Flight (T 3 ) End Saga

  35. Successful Saga Begin Saga Start Book Hotel (T 1 ) End Book Hotel (T 1 ) Start Book Car Rental (T 2 ) End Book Car Rental (T 2 ) � Start Book Flight (T 3 ) End Book Flight (T 3 ) End Saga

  36. Successful Saga Begin Saga Start Book Hotel (T 1 ) End Book Hotel (T 1 ) Start Book Car Rental (T 2 ) End Book Car Rental (T 2 ) Start Book Flight (T 3 ) � End Book Flight (T 3 ) End Saga

  37. Successful Saga Begin Saga Start Book Hotel (T 1 ) End Book Hotel (T 1 ) Start Book Car Rental (T 2 ) End Book Car Rental (T 2 ) Start Book Flight (T 3 ) End Book Flight (T 3 ) � End Saga

  38. Successful Saga Begin Saga Start Book Hotel (T 1 ) End Book Hotel (T 1 ) Start Book Car Rental (T 2 ) End Book Car Rental (T 2 ) Start Book Flight (T 3 ) End Book Flight (T 3 ) End Saga

  39. Unsuccessful Saga Backwards Recovery

  40. Unsuccessful Saga Begin Saga � Start Book Hotel (T 1 ) End Book Hotel (T 1 ) Start Book Car Rental (T 2 ) Abort Saga Start Compensate Car Rental (C 2 ) End Compensate Car Rental (C 2 ) Start Compensate Book Hotel (C 1 ) End Compensate Book Hotel (C 1 ) End Saga

  41. Unsuccessful Saga Begin Saga Start Book Hotel (T 1 ) � End Book Hotel (T 1 ) Start Book Car Rental (T 2 ) Abort Saga Start Compensate Car Rental (C 2 ) End Compensate Car Rental (C 2 ) Start Compensate Book Hotel (C 1 ) End Compensate Book Hotel (C 1 ) End Saga

  42. Unsuccessful Saga Begin Saga Start Book Hotel (T 1 ) End Book Hotel (T 1 ) � Start Book Car Rental (T 2 ) Abort Saga Start Compensate Car Rental (C 2 ) End Compensate Car Rental (C 2 ) Start Compensate Book Hotel (C 1 ) End Compensate Book Hotel (C 1 ) End Saga

  43. Unsuccessful Saga Begin Saga Start Book Hotel (T 1 ) End Book Hotel (T 1 ) Start Book Car Rental (T 2 ) � Abort Saga Start Compensate Car Rental (C 2 ) End Compensate Car Rental (C 2 ) Start Compensate Book Hotel (C 1 ) End Compensate Book Hotel (C 1 ) End Saga

  44. Unsuccessful Saga Begin Saga Start Book Hotel (T 1 ) End Book Hotel (T 1 ) Start Book Car Rental (T 2 ) Abort Saga � Start Compensate Car Rental (C 2 ) End Compensate Car Rental (C 2 ) Start Compensate Book Hotel (C 1 ) End Compensate Book Hotel (C 1 ) End Saga

  45. Unsuccessful Saga Begin Saga Start Book Hotel (T 1 ) End Book Hotel (T 1 ) Start Book Car Rental (T 2 ) Abort Saga Start Compensate Car Rental (C 2 ) � End Compensate Car Rental (C 2 ) Start Compensate Book Hotel (C 1 ) End Compensate Book Hotel (C 1 ) End Saga

  46. Unsuccessful Saga Begin Saga Start Book Hotel (T 1 ) End Book Hotel (T 1 ) Start Book Car Rental (T 2 ) Abort Saga Start Compensate Car Rental (C 2 ) End Compensate Car Rental (C 2 ) � Start Compensate Book Hotel (C 1 ) End Compensate Book Hotel (C 1 ) End Saga

  47. Unsuccessful Saga Begin Saga Start Book Hotel (T 1 ) End Book Hotel (T 1 ) Start Book Car Rental (T 2 ) Abort Saga Start Compensate Car Rental (C 2 ) End Compensate Car Rental (C 2 ) Start Compensate Book Hotel (C 1 ) � End Compensate Book Hotel (C 1 ) End Saga

  48. Unsuccessful Saga Begin Saga Start Book Hotel (T 1 ) End Book Hotel (T 1 ) Start Book Car Rental (T 2 ) Abort Saga Start Compensate Car Rental (C 2 ) End Compensate Car Rental (C 2 ) Start Compensate Book Hotel (C 1 ) End Compensate Book Hotel (C 1 ) � End Saga

  49. Unsuccessful Saga Begin Saga Start Book Hotel (T 1 ) End Book Hotel (T 1 ) Start Book Car Rental (T 2 ) Abort Saga Start Compensate Car Rental (C 2 ) End Compensate Car Rental (C 2 ) Start Compensate Book Hotel (C 1 ) End Compensate Book Hotel (C 1 ) End Saga

  50. Sagas in Distributed Systems “Due to space limitations, we only discuss Sagas in a centralized System, although clearly they can be implemented in a distributed database system.” –Molina et. al

  51. SOA/Microservices

  52. Requests instead of Transactions \ • Book Hotel (T 1 ) • Cancel Hotel (C 1 ) • Book Car (T 2 ) • Cancel Car (C 2 ) • Book Flight (T 3 ) • Cancel Flight (C 3 )

  53. A Distributed Saga is a Collection of Sub-Requests T 1 , T 2 … T n Each Sub-Request has a Compensating Request C 1 , C 2 … C n

  54. Successful Distributed Saga Begin Saga Start Book Hotel Request (T 1 ) End Book Hotel Request (T 1 ) Start Book Car Rental Request (T 2 ) End Book Car Rental Request (T 2 ) Start Book Flight Request (T 3 ) End Book Flight Request (T 3 ) End Saga

  55. Saga Log Durable & Distributed

  56. Saga Execution Coordinator (SEC) • Interprets & Writes to Saga Log • Applies Saga Sub-Requests • Applies Saga Compensating Requests when Necessary

  57. Apply Compensating Requests • Aborted Saga Response • Start Request Fails • SEC Crashes (non-safe state)

  58. What Happens when Compensating Requests Fail?

  59. Compensating Requests Must Be Idempotent

  60. What Happens when SEC Fails?

  61. Safe States • All Executed Sub-Requests are Complete (Start T i & End T i both logged) • Saga has been Aborted, Proceed with Compensating Transactions

  62. Un-Safe State • Start T i logged, no End T i logged Abort Saga Start Compensating Requests

  63. Request Messaging Semantics • Sub-Requests (T i ): At Most Once • Compensating Requests (C i ): At Least Once

  64. Distributed Saga Guarantee Either • T 1 , T 2 … T n or • T 1 , T 2 … T j, C j , … C 2 , C 1

  65. Distributed Sagas • Distributed/Durable Saga Log • SEC Process • Compensating Requests: Idempotent

  66. Halo Statistics

  67. Halo Statistics Service

  68. Halo Statistics Service with Sagas

  69. Forward-Recovery

  70. Game Grain / SEC Store Stats Store Stats Store Stats Store Stats Player One Player Two Player Three Player Four

  71. Forward Recovery Sub-Requests Must Also Be Idempotent

  72. Game Grain / SEC Store Stats Store Stats Store Stats Store Stats Player One Player Two Player Three Player Four

  73. Game Grain / SEC Store Stats Store Stats Store Stats Store Stats Player One Player Two Player Three Player Four

Recommend


More recommend