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

sagas.pdf

 sagas.pdf

Sagas pattern and how to use it to run distributed transactions across domain contexts, microservices, databases and third-party API's with Elixir.

Andrew Dryga

May 31, 2018
Tweet

Other Decks in Technology

Transcript

  1. ABOUT ME ▸ 14 years in IT, Elixir dev for

    2+ years ▸ Worked on NHS of Ukraine and few other large projects ▸ Maintain 20+ public Elixir repos used in production ▸ Contribute back as much as I can
  2. WE ARE GOING TO TALK ABOUT ▸ Why do we

    need distributed transactions? ▸ Sagas pattern ▸ Sage Elixir library ▸ Structuring your domain contexts (bonus)
  3. IMAGINE WE ARE BUILDING A SIMPLE BOOKING WEBSITE Invariants: -

    We should not charge card if request was not fulfilled - We should not hold booking if charge is failed
  4. WHAT IS WRONG? ▸ Bad for latency - can't book

    concurrently ▸ The syntax to track step on which failure occurred is ugly ▸ To test it you need a complex failure injection for API client stubs ▸ Easy to miss some errors - monster else clause
  5. DISTRIBUTED TRANSACTIONS ARE EVERYWHERE ▸ CRM and Campaign Automation (Salesforce,

    Hubspot, Intercom, Autopilot) ▸ Payment Processors (Stripe, Braintree, PayPal) ▸ Analytics (GA, Mixpanel) ▸ Cloud Providers (Google Cloud, AWS) ▸ Any other SaaS integration ▸ Microservices
  6. SAGAS PATTERN Failure management pattern that originates from 1987’s paper

    on long running transactions for databases. A long lived transaction is a Saga if it can be written as a sequence of transactions that can be interleaved with other transactions. The database management system guarantees that either all the transactions in a Saga are successfully completed or compensating transactions are run to amend a partial execution.
  7. HOW DOES IT WORK? ▸ Transaction is split into a

    multiple atomic steps ▸ Each step consist from transaction and compensation ▸ When transaction fails compensation are run to semantically amend it's effects ▸ Compensation and transactions should be idempotent
  8. SAGE Sage is a dependency-free pure Elixir library inspired by

    Sagas pattern. Provides set of additional features on top of steps, transactions and compensations defined by the original paper.
  9. It's like Ecto.Multi but across business logic and third-party APIs.

    — @jayjun from #elixir-lang Slack channel
  10. RECAP Use Sage to: ▸ Synchronize state across microservices ▸

    Synchronize state across databases ▸ To run distributed transactions Sage can help to elegantly organize logic in your domain contexts. Still young but well-documented and covered with tests.