Successfully reported this slideshow.
We use your LinkedIn profile and activity data to personalize ads and to show you more relevant ads. You can change your ad preferences anytime.

E-VAN - Sagas


Published on

My e-van presentation on sagas and how they are supported in NServiceBus

Published in: Technology, Design
  • Be the first to comment

E-VAN - Sagas

  1. 1. What’s this “Saga” thing that everyone is talking about? <ul><li>@andreasohlund </li></ul><ul><li> </li></ul>
  2. 2. What is a long running process? <ul><li>“A long running process is a process whose execution lifetime exceeds the time to process a single external event or message” </li></ul><ul><li>- Udi Dahan </li></ul>
  3. 3. Long running process Step Step Step Step Step Start End Start
  4. 4. The problem <ul><li>Holding locks for the duration of a long running transaction are more likely </li></ul><ul><ul><li>To be deadlocked </li></ul></ul><ul><ul><li>To experience infrastructure failures </li></ul></ul>
  5. 5. Two levels of transactions <ul><li>First level is a business transaction </li></ul><ul><ul><li>Does not comply to ACID </li></ul></ul><ul><ul><li>Requires compensating actions </li></ul></ul><ul><li>Second level is acid compliant </li></ul><ul><ul><li>Analyze the business to find the boundaries </li></ul></ul>
  6. 6. Long Lived Transactions Step Step Step Step Step Start End Business Transaction ACID ACID ACID ACID ACID
  7. 7. The anatomy of a step Step Get current state Store
  8. 8. Sagas vs Aggregate roots Saga AR Event Command Event Command
  9. 9. Sagas and bounded contexts Saga AR Bounded context Event Command External Event
  10. 10. The birth of a saga Bus DB Store state Saga message Saga instance Create Dispatch Send Saga Persister Save
  11. 11. Existing sagas Bus Message Dispatch Send Save DB Load/Store Saga Persister Get Saga instance State
  12. 12. State <ul><li>Remember what steps have been executed </li></ul><ul><li>Gets persisted between each step </li></ul><ul><li>Temporary storage of data </li></ul><ul><li>Serialize access to the same saga instance </li></ul><ul><ul><li>Unique index </li></ul></ul><ul><ul><li>Locking </li></ul></ul>
  13. 13. Time <ul><li>Long running transactions </li></ul><ul><li>Time is a business requirement </li></ul><ul><li>Needs to be durable and managed externally </li></ul>
  14. 14. Sagas help throughput <ul><li>Can execute “steps” in parallel </li></ul><ul><li>Saga instances are not run in parallel </li></ul><ul><ul><li>Use locks in DB to ensure that </li></ul></ul>
  15. 15. Versioning <ul><li>Long running == high probability that new versions must be compatible with older state </li></ul><ul><ul><li>Either patch DB </li></ul></ul><ul><ul><li>or write back compat code </li></ul></ul>
  16. 16. How do sagas wake up? <ul><li>Messages need to contain id:s for correlation </li></ul><ul><ul><li>Non technical </li></ul></ul>
  17. 17. What happens when they are dead? <ul><li>The big void </li></ul><ul><ul><li>state is no longer relevant </li></ul></ul><ul><li>But their children lives to tell the story </li></ul><ul><ul><li>Other entities gets updated to reflect the outcome of a saga </li></ul></ul><ul><li>todo: image black hole/gravestone </li></ul>
  18. 18. Shit happens <ul><li>Compensating actions </li></ul><ul><ul><li>You decide </li></ul></ul><ul><li>Retry of individual messages </li></ul><ul><li>Strive for forward recovery only </li></ul>