Distributed Sagas: A Protocol for Coordinating Microservices

Distributed Sagas: A Protocol for Coordinating Microservices

Microservices have become the defacto architecture pattern for building services. However separating business logic into small services that operate with a single logical data set has introduced consistency challenges. Previous attempts to solve this problem like two phase commit have not been widely adopted due to availability and liveness issues.

Instead developers implement feral concurrency control mechanism. This technique can be error prone, and often results in “Death Star” architectures which rely on chained calls to enforce application invariants. These architectures become more complicated over time, and are difficult to modify and extend, and often don't correctly handle all failure scenarios.

In this talk I propose a new solution for this problem, Distributed Sagas, a protocol for coordinating requests among multiple micro services, while ensuring application invariants.

9128d500301ae51524e887bb680f471d?s=128

Caitie McCaffrey

May 19, 2017
Tweet

Transcript

  1. 5.
  2. 20.
  3. 22.
  4. 29.

    2PC: Commit Front End Trips Done Done Done Doesn’t Scale

    •O (N^2) Messages in the worst case •Coordinator is a Single Point of Failure •Reduced Throughput
  5. 31.
  6. 33.

    1987 “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.”
  7. 35.
  8. 36.

    A Distributed Saga is a Collection of Requests Book Hotel

    Book Car Book Flight Charge Money and Compensating Requests Cancel Hotel Cancel Car Cancel Flight Refund Money
  9. 37.

    A Distributed Saga is a Collection of Requests Book Hotel

    Book Car Book Flight Charge Money and Compensating Requests Cancel Hotel Cancel Car Cancel Flight Refund Money that represent a single business level action
  10. 61.

    Distributed Saga Guarantee Book Hotel Book Car Book Flight Charge

    Money All requests were completed successfully
  11. 62.

    Distributed Saga Guarantee Book Hotel Book Car Book Flight Charge

    Money All requests were completed successfully Or a subset of requests and the corresponding compensating requests were executed Book Hotel Book Car Cancel Hotel Cancel Car
  12. 63.

    Distributed Saga Guarantee Book Hotel Book Car Book Flight Charge

    Money All requests were completed successfully Or a subset of requests and the corresponding compensating requests were executed Book Hotel Book Car Cancel Hotel Cancel Car No Atomicity No Isolation
  13. 64.

    Distributed Saga Guarantee Book Hotel Book Car Book Flight Charge

    Money All requests were completed successfully Or a subset of requests and the corresponding compensating requests were executed Book Hotel Book Car Cancel Hotel Cancel Car No Atomicity No Isolation Book Hotel Book Car Visible before Saga Completes
  14. 65.

    Distributed Saga Guarantee Book Hotel Book Car Book Flight Charge

    Money All requests were completed successfully Or a subset of requests and the corresponding compensating requests were executed Book Hotel Book Car Cancel Hotel Cancel Car
  15. 69.

    Start & End Vertices Distributed Saga Start Saga End Saga

    Car Flight Hotel Payment Start Saga End Saga
  16. 74.
  17. 75.

    Car Flight Hotel Payment Start Saga End Saga SEC Saga

    Log Done Start Saga Book Trip Request
  18. 77.
  19. 78.

    Car Flight Hotel Payment Start Saga End Saga Saga Log

    SEC Book Hotel Request Start Saga Start Hotel
  20. 79.

    Car Flight Hotel Payment Start Saga End Saga Saga Log

    SEC Book Hotel Response Start Saga Start Hotel
  21. 80.

    Car Flight Hotel Payment Start Saga End Saga Saga Log

    SEC End Hotel Start Saga Start Hotel
  22. 81.

    Car Flight Hotel Payment Start Saga End Saga Saga Log

    SEC Done Start Saga Start Hotel End Hotel
  23. 82.

    Car Flight Hotel Payment Start Saga End Saga Saga Log

    SEC Start Car Start Saga Start Hotel End Hotel
  24. 83.

    Car Flight Hotel Payment Start Saga End Saga Saga Log

    SEC Done Book Car Request Start Saga Start Hotel End Hotel Start Car
  25. 84.

    Car Flight Hotel Payment Start Saga End Saga Saga Log

    Start Saga SEC Start Hotel End Hotel Start Car Book Car Response End Car
  26. 85.

    Car Flight Hotel Payment Start Saga End Saga Saga Log

    Start Saga SEC Start Hotel End Hotel Start Car Done End Car
  27. 86.

    Car Flight Hotel Payment Start Saga End Saga Saga Log

    Start Saga SEC Start Hotel End Hotel Start Car Start Flight End Car
  28. 87.

    Car Flight Hotel Payment Start Saga End Saga Saga Log

    Start Saga SEC Start Hotel End Hotel Start Car Done End Car Start Flight Book Flight Request
  29. 88.

    Car Flight Hotel Payment Start Saga End Saga Saga Log

    Start Saga SEC Start Hotel End Hotel Start Car End Car Start Flight Book Flight Response End Flight
  30. 89.

    Car Flight Hotel Payment Start Saga End Saga Saga Log

    Start Saga SEC Start Hotel End Hotel Start Car End Car Start Flight Done End Flight
  31. 90.

    Car Flight Hotel Payment Start Saga End Saga Saga Log

    Start Saga SEC Start Hotel End Hotel Start Car End Car Start Flight Start Payment End Flight
  32. 91.

    Car Flight Hotel Payment Start Saga End Saga Saga Log

    Start Saga SEC Start Hotel End Hotel Start Car End Car Start Flight Done End Flight Start Payment Payment Request
  33. 92.

    Car Flight Hotel Payment Start Saga End Saga Saga Log

    Start Saga SEC Start Hotel End Hotel Start Car End Car Start Flight End Payment End Flight Start Payment Payment Response
  34. 93.

    Car Flight Hotel Payment Start Saga End Saga Saga Log

    Start Saga SEC Start Hotel End Hotel Start Car End Car Start Flight End Flight Start Payment End Payment Done
  35. 94.

    Car Flight Hotel Payment Start Saga End Saga Saga Log

    Start Saga SEC Start Hotel End Hotel Start Car End Car Start Flight End Flight Start Payment End Payment End Saga
  36. 95.

    Car Flight Hotel Payment Start Saga End Saga Saga Log

    Start Saga SEC Start Hotel End Hotel Start Car End Car Start Flight End Flight Start Payment End Payment Done End Saga
  37. 96.

    Car Flight Hotel Payment Start Saga End Saga Saga Log

    Start Saga SEC Start Hotel End Hotel Start Car End Car Start Flight End Flight Start Payment End Payment End Saga
  38. 98.

    Car Flight Hotel Payment Start Saga End Saga Saga Log

    SEC Book Car Request Start Saga Start Hotel End Hotel Start Car Start Flight
  39. 99.

    Car Flight Hotel Payment Start Saga End Saga Saga Log

    Start Saga SEC Start Hotel End Hotel Book Car Response Abort Car Start Car Start Flight
  40. 100.

    Car Flight Hotel Payment Start Saga End Saga Saga Log

    Start Saga SEC Start Hotel End Hotel Done Start Car Start Flight Abort Car
  41. 101.

    Car Flight Hotel Payment Start Saga End Saga Saga Log

    Start Saga SEC Start Hotel End Hotel Done Start Car Start Flight Abort Car Rollback Recovery
  42. 102.

    Car Flight Hotel Payment End Comp Saga Start Comp Saga

    Saga Log Start Saga SEC Start Hotel End Hotel Start Car Start Flight Abort Car
  43. 103.

    Car Flight Hotel Payment Saga Log Start Saga SEC Start

    Hotel End Hotel Start Car Start Flight Abort Car Start Comp Saga End Comp Saga
  44. 104.

    Car Flight Hotel Payment Saga Log Start Saga SEC Start

    Hotel End Hotel Payment Log Entries? Start Car Start Flight Abort Car Start Comp Saga End Comp Saga
  45. 105.

    Car Flight Hotel Payment Saga Log Start Saga SEC Start

    Hotel End Hotel { } Start Car Start Flight Abort Car Start Comp Saga End Comp Saga
  46. 106.

    Car Flight Hotel Payment Saga Log Start Saga SEC Start

    Hotel End Hotel Start Car Start Flight Abort Car Car Log Entries? Start Comp Saga End Comp Saga
  47. 107.

    Car Flight Hotel Payment Saga Log Start Saga SEC Start

    Hotel End Hotel Start Car Start Flight Abort Car { Start, Abort } Start Comp Saga End Comp Saga
  48. 108.

    Car Flight Hotel Payment Saga Log Start Saga SEC Start

    Hotel End Hotel Start Car Start Flight Abort Car Hotel Log Entries? Start Comp Saga End Comp Saga
  49. 109.

    Car Flight Hotel Payment Saga Log Start Saga SEC Start

    Hotel End Hotel Start Car Start Flight Abort Car { Start, End } Start Comp Saga End Comp Saga
  50. 110.

    Car Flight Hotel Payment Saga Log Start Saga SEC Start

    Hotel End Hotel Start Car Start Flight Abort Car { Start, End } Cancel Hotel Request Start Comp Saga End Comp Saga
  51. 111.

    Car Flight Hotel Payment Saga Log Start Saga SEC Start

    Hotel End Hotel Start Car Start Flight Abort Car Cancel Hotel Response Comp Hotel Start Comp Saga End Comp Saga
  52. 112.

    Car Flight Hotel Payment Saga Log Start Saga SEC Start

    Hotel End Hotel Start Car Start Flight Abort Car Done Comp Hotel Start Comp Saga End Comp Saga
  53. 113.

    Car Flight Hotel Payment Saga Log Start Saga SEC Start

    Hotel End Hotel Start Car Start Flight Abort Car Comp Hotel Flight Log Entries? Start Comp Saga End Comp Saga
  54. 114.

    Car Flight Hotel Payment Saga Log Start Saga SEC Start

    Hotel End Hotel Start Car Start Flight Abort Car Comp Hotel { Start } Start Comp Saga End Comp Saga
  55. 115.

    Car Flight Hotel Payment Saga Log Start Saga SEC Start

    Hotel End Hotel Start Car Start Flight Abort Car Comp Hotel { Start } Book Flight Request Start Comp Saga End Comp Saga
  56. 116.

    Car Flight Hotel Payment Saga Log Start Saga SEC Start

    Hotel End Hotel Start Car Start Flight Abort Car Comp Hotel End Flight Book Flight Response Start Comp Saga End Comp Saga
  57. 117.

    Car Flight Hotel Payment Saga Log Start Saga SEC Start

    Hotel End Hotel Start Car Start Flight Abort Car Comp Hotel Done Cancel Flight Request End Flight Start Comp Saga End Comp Saga
  58. 118.

    Car Flight Hotel Payment Saga Log Start Saga SEC Start

    Hotel End Hotel Start Car Start Flight Abort Car Comp Hotel Comp Flight Cancel Flight Response Start Comp Saga End Comp Saga End Flight
  59. 119.

    Car Flight Hotel Payment Saga Log Start Saga SEC Start

    Hotel End Hotel Start Car Start Flight Abort Car Comp Hotel Done Comp Flight Start Comp Saga End Comp Saga End Flight Comp Flight
  60. 120.

    Car Flight Hotel Payment Saga Log Start Saga SEC Start

    Hotel End Hotel Start Car Start Flight Abort Car Comp Hotel End Saga Start Comp Saga End Comp Saga Comp Flight End Flight Comp Flight
  61. 121.

    Car Flight Hotel Payment Saga Log Start Saga SEC Start

    Hotel End Hotel Start Car Start Flight Abort Car Comp Hotel Done End Saga Start Comp Saga End Comp Saga Comp Flight End Flight Comp Flight
  62. 122.

    Distributed Saga Guarantee Book Hotel Book Car Book Flight Charge

    Money All requests were completed successfully Or a subset of requests and the corresponding compensating requests were executed Book Hotel Book Car Cancel Hotel Cancel Car
  63. 128.

    with Distributed Sagas Isolation of Complex Code Fron Tr Complex

    Code Lives Everywhere Front SEC Saga Log Complex Code Lives Here Complex Code Lives Here
  64. 129.

    Fron Tr with Distributed Sagas Modular Services Front SEC Saga

    Log Only Flight Booking Flight Booking & Feral Concurrency Control Mechanisms
  65. 130.

    Fron Tr with Distributed Sagas Service Composition Front SEC Saga

    Log Just Add A New Saga! New Service & Feral Concurrency Control Mechanisms