Lost in transaction? - JAX London...If your transaction involves 2 to 4 steps, choreography might be...

79
@berndruecker Lost in transaction? Strategies to manage consistency in distributed systems

Transcript of Lost in transaction? - JAX London...If your transaction involves 2 to 4 steps, choreography might be...

Page 1: Lost in transaction? - JAX London...If your transaction involves 2 to 4 steps, choreography might be a very good fit. However, this approach can rapidly become confusing if you keep

@berndruecker

Lost in transaction?

Strategies to manage consistencyin distributed systems

Page 2: Lost in transaction? - JAX London...If your transaction involves 2 to 4 steps, choreography might be a very good fit. However, this approach can rapidly become confusing if you keep

@berndruecker

Page 3: Lost in transaction? - JAX London...If your transaction involves 2 to 4 steps, choreography might be a very good fit. However, this approach can rapidly become confusing if you keep

Order

OrderLine Item

Customer

Customer

Address

orderIdcustomerIdchargeId…

customerId…

productIdprice…

Charge

Charge

Order

chargeIdamountpaymentProvidertransactionId…

Aggregate

Entity

Value Object

Reference by identity@berndruecker

Page 4: Lost in transaction? - JAX London...If your transaction involves 2 to 4 steps, choreography might be a very good fit. However, this approach can rapidly become confusing if you keep

Order

OrderLine Item

orderIdcustomerIdchargeId…

productIdprice… Order

Charge

Charge

chargeIdamountpaymentProvidertransactionId… Create

Charge

Mark order as

paid

All ornothing+

@berndruecker

Page 5: Lost in transaction? - JAX London...If your transaction involves 2 to 4 steps, choreography might be a very good fit. However, this approach can rapidly become confusing if you keep

ACID

AtomicityConsistencyIsolationDurability

@berndruecker

Page 6: Lost in transaction? - JAX London...If your transaction involves 2 to 4 steps, choreography might be a very good fit. However, this approach can rapidly become confusing if you keep

Aggregates = Consistency Boundaries

Order

OrderLine Item

orderIdcustomerIdchargeId…

productIdprice… Order

Charge

Charge

chargeIdamountpaymentProvidertransactionId…

You can do ACID within here

Or here

But not a joinedACID transaction!

@berndruecker

Page 7: Lost in transaction? - JAX London...If your transaction involves 2 to 4 steps, choreography might be a very good fit. However, this approach can rapidly become confusing if you keep

Aggregates = Consistency Boundaries

Order

OrderLine Item

orderIdcustomerIdchargeId…

productIdprice… Order

Charge

Charge

chargeIdamountpaymentProvidertransactionId…

Own DB / autonomous technology decisionsAPI-drivenScalability…

@berndruecker

Page 8: Lost in transaction? - JAX London...If your transaction involves 2 to 4 steps, choreography might be a very good fit. However, this approach can rapidly become confusing if you keep

Aggregates = Consistency Boundaries

Order

OrderLine Item

orderIdcustomerIdchargeId…

productIdprice… Order

Charge

Charge

chargeIdamountpaymentProvidertransactionId…

Own DB / autonomous technology decisionsAPI-drivenScalability…

@berndruecker

Page 9: Lost in transaction? - JAX London...If your transaction involves 2 to 4 steps, choreography might be a very good fit. However, this approach can rapidly become confusing if you keep

Boundaries need to be designed carefully

Charge

chargeIdamountpaymentProvidertransactionId…

Order

OrderLine Item

orderIdcustomerIdchargeId…

productIdprice…Order

@berndruecker

Page 10: Lost in transaction? - JAX London...If your transaction involves 2 to 4 steps, choreography might be a very good fit. However, this approach can rapidly become confusing if you keep

But no implicit constraints!

Order

OrderLine Item

orderIdcustomerIdchargeId…

productIdprice… Order

Charge

Charge

chargeIdamountpaymentProvidertransactionId…

Joined DB transaciton

@berndruecker

Page 11: Lost in transaction? - JAX London...If your transaction involves 2 to 4 steps, choreography might be a very good fit. However, this approach can rapidly become confusing if you keep

There is two-phase commit (XA)!!

TXCoordinator

ResourceManagers

PreparePhase

CommitPhase

@berndruecker

Page 12: Lost in transaction? - JAX London...If your transaction involves 2 to 4 steps, choreography might be a very good fit. However, this approach can rapidly become confusing if you keep

Pat Helland

Distributed Systems Guru Worked at Amazon, Microsoft & Salesforce

@berndruecker

Page 13: Lost in transaction? - JAX London...If your transaction involves 2 to 4 steps, choreography might be a very good fit. However, this approach can rapidly become confusing if you keep

Pat Helland

Grown-Ups Don’t Use Distributed Transactions

Distributed Systems Guru Worked at Amazon, Microsoft & Salesforce

@berndruecker

Page 14: Lost in transaction? - JAX London...If your transaction involves 2 to 4 steps, choreography might be a very good fit. However, this approach can rapidly become confusing if you keep

Starbucks does not use two phase commithttps://www.enterpriseintegrationpatterns.com/ramblings/18_starbucks.html

Photo by John Ingle

@berndruecker

Page 15: Lost in transaction? - JAX London...If your transaction involves 2 to 4 steps, choreography might be a very good fit. However, this approach can rapidly become confusing if you keep

Eric BrewerAtomicityConsistencyIsolationDurability

http://pld.cs.luc.edu/courses/353/spr11/notes/brewer_keynote.pdf @berndruecker

Page 16: Lost in transaction? - JAX London...If your transaction involves 2 to 4 steps, choreography might be a very good fit. However, this approach can rapidly become confusing if you keep

That means

Do A

Do B

Temporarilyinconsistent

Eventuallyconsistentagain

t

Consistent

LocalACID

LocalACID

1 aggregate1 (micro-)service1 program1 resource

Violates „I“ of ACID

@berndruecker

Page 17: Lost in transaction? - JAX London...If your transaction involves 2 to 4 steps, choreography might be a very good fit. However, this approach can rapidly become confusing if you keep

„Building on Quicksand“ Paper

ACID2.0Pat Helland

@berndruecker

Page 18: Lost in transaction? - JAX London...If your transaction involves 2 to 4 steps, choreography might be a very good fit. However, this approach can rapidly become confusing if you keep

AssociativeCommutativeIdempotentDistributed2.0

(a + b) + c = a + (b + c)

a + b = b + a

f(x) = f( f(x) )

„Building on Quicksand“ Paper

Pat Helland

@berndruecker

Page 19: Lost in transaction? - JAX London...If your transaction involves 2 to 4 steps, choreography might be a very good fit. However, this approach can rapidly become confusing if you keep

Distributed@berndruecker

Page 20: Lost in transaction? - JAX London...If your transaction involves 2 to 4 steps, choreography might be a very good fit. However, this approach can rapidly become confusing if you keep

Distributed systems @berndruecker

Page 21: Lost in transaction? - JAX London...If your transaction involves 2 to 4 steps, choreography might be a very good fit. However, this approach can rapidly become confusing if you keep

Network problems

CreditCard

Paymentcharge

@berndruecker

Page 22: Lost in transaction? - JAX London...If your transaction involves 2 to 4 steps, choreography might be a very good fit. However, this approach can rapidly become confusing if you keep

Strategy: retry

CreditCard

Payment

Charge Credit CardcardNumber

amount

Charge Credit CardcardNumber

amounttransactionId

Not idempotent

Idempotent

has to be idempotent

charge

@berndruecker

Page 23: Lost in transaction? - JAX London...If your transaction involves 2 to 4 steps, choreography might be a very good fit. However, this approach can rapidly become confusing if you keep

Photo by pixabay, available under Creative Commons CC0 1.0 license.

@berndruecker

Page 24: Lost in transaction? - JAX London...If your transaction involves 2 to 4 steps, choreography might be a very good fit. However, this approach can rapidly become confusing if you keep

Requirement: Idempotency of services!

Photo by pixabay, available under Creative Commons CC0 1.0 license.

@berndruecker

Page 25: Lost in transaction? - JAX London...If your transaction involves 2 to 4 steps, choreography might be a very good fit. However, this approach can rapidly become confusing if you keep

Requirement: Idempotency of services!

Photo by Chr.Späth, available under Public Domain.

@berndruecker

Page 26: Lost in transaction? - JAX London...If your transaction involves 2 to 4 steps, choreography might be a very good fit. However, this approach can rapidly become confusing if you keep

Distributed systems @berndruecker

Page 27: Lost in transaction? - JAX London...If your transaction involves 2 to 4 steps, choreography might be a very good fit. However, this approach can rapidly become confusing if you keep

It is impossible todifferentiate certain

failure scenarios:

Independant ofcommunication style!

Service Provider

Client

@berndruecker

Page 28: Lost in transaction? - JAX London...If your transaction involves 2 to 4 steps, choreography might be a very good fit. However, this approach can rapidly become confusing if you keep

Strategy: Cleanup

CreditCard

Paymentcharge

Make sure it isnot charged!

Cancel chargecardNumber

amounttransactionId

Raisepayment failed

@berndruecker

Page 29: Lost in transaction? - JAX London...If your transaction involves 2 to 4 steps, choreography might be a very good fit. However, this approach can rapidly become confusing if you keep

Distributed systems @berndruecker

Page 30: Lost in transaction? - JAX London...If your transaction involves 2 to 4 steps, choreography might be a very good fit. However, this approach can rapidly become confusing if you keep

Some communication challengesrequire state.

@berndruecker

Page 31: Lost in transaction? - JAX London...If your transaction involves 2 to 4 steps, choreography might be a very good fit. However, this approach can rapidly become confusing if you keep

Strategy: Stateful retry

CreditCard

Paymentcharge

@berndruecker

Page 32: Lost in transaction? - JAX London...If your transaction involves 2 to 4 steps, choreography might be a very good fit. However, this approach can rapidly become confusing if you keep

Strategy: Stateful retry

CreditCard

Paymentcharge

Make sure it isnot charged!

@berndruecker

Page 33: Lost in transaction? - JAX London...If your transaction involves 2 to 4 steps, choreography might be a very good fit. However, this approach can rapidly become confusing if you keep

Warning:Contains Opinion

Page 34: Lost in transaction? - JAX London...If your transaction involves 2 to 4 steps, choreography might be a very good fit. However, this approach can rapidly become confusing if you keep

Berlin, Germany

[email protected]@berndruecker

Bernd RueckerCo-founder and Technologist ofCamunda

Page 35: Lost in transaction? - JAX London...If your transaction involves 2 to 4 steps, choreography might be a very good fit. However, this approach can rapidly become confusing if you keep

Payment

Stateful retry

CreditCardREST

@berndruecker

Page 36: Lost in transaction? - JAX London...If your transaction involves 2 to 4 steps, choreography might be a very good fit. However, this approach can rapidly become confusing if you keep

Stateful retry & cleanup

CreditCard

PaymentREST

Cancelcharge

@berndruecker

Page 37: Lost in transaction? - JAX London...If your transaction involves 2 to 4 steps, choreography might be a very good fit. However, this approach can rapidly become confusing if you keep

Architecture

Infrastructure

Application

Domain

WorkflowEngine

@berndruecker

Page 38: Lost in transaction? - JAX London...If your transaction involves 2 to 4 steps, choreography might be a very good fit. However, this approach can rapidly become confusing if you keep

Architecture

Infrastructure

Application

Domain

WorkflowEngine

@berndruecker

Lightweight Open SourceBPMN

Page 39: Lost in transaction? - JAX London...If your transaction involves 2 to 4 steps, choreography might be a very good fit. However, this approach can rapidly become confusing if you keep

Live hacking

https://github.com/flowing/flowing-retail/tree/master/rest

@berndruecker

Page 40: Lost in transaction? - JAX London...If your transaction involves 2 to 4 steps, choreography might be a very good fit. However, this approach can rapidly become confusing if you keep

Embedded Engine Example (Java)

https://blog.bernd-ruecker.com/architecture-options-to-run-a-workflow-engine-6c2419902d91

@berndruecker

Page 41: Lost in transaction? - JAX London...If your transaction involves 2 to 4 steps, choreography might be a very good fit. However, this approach can rapidly become confusing if you keep

A relatively common pattern

Service(e.g. Go)

Rabbit

RDMS

1. Receive

4. Send additional events

2. Business Logic

3. Send response? ACK

@berndruecker

Page 42: Lost in transaction? - JAX London...If your transaction involves 2 to 4 steps, choreography might be a very good fit. However, this approach can rapidly become confusing if you keep

„Can this handle 15k requests per second?“

@berndruecker

Page 43: Lost in transaction? - JAX London...If your transaction involves 2 to 4 steps, choreography might be a very good fit. However, this approach can rapidly become confusing if you keep
Page 44: Lost in transaction? - JAX London...If your transaction involves 2 to 4 steps, choreography might be a very good fit. However, this approach can rapidly become confusing if you keep

and

ACID?

Page 45: Lost in transaction? - JAX London...If your transaction involves 2 to 4 steps, choreography might be a very good fit. However, this approach can rapidly become confusing if you keep

@berndruecker

Page 46: Lost in transaction? - JAX London...If your transaction involves 2 to 4 steps, choreography might be a very good fit. However, this approach can rapidly become confusing if you keep

@berndruecker

Page 47: Lost in transaction? - JAX London...If your transaction involves 2 to 4 steps, choreography might be a very good fit. However, this approach can rapidly become confusing if you keep

Compensation – the classical example

Sagabookhotel

bookcar

bookflight

cancelhotel

cancelcar

1. 2. 3.

5.6.

In case of failuretrigger compensations

booktrip

Page 48: Lost in transaction? - JAX London...If your transaction involves 2 to 4 steps, choreography might be a very good fit. However, this approach can rapidly become confusing if you keep

2 alterntive approaches: choreography & orchestration

@berndruecker

Page 49: Lost in transaction? - JAX London...If your transaction involves 2 to 4 steps, choreography might be a very good fit. However, this approach can rapidly become confusing if you keep

Event-driven choreography

Hotel

Flight

Car

Trip

Tripbooked

Flightbooked

Triprequested

Hotelbooked

Carbooked

Requesttrip

@berndruecker

Page 50: Lost in transaction? - JAX London...If your transaction involves 2 to 4 steps, choreography might be a very good fit. However, this approach can rapidly become confusing if you keep

Event-driven choreography

Hotel

Flight

Car

Trip

Tripfailed

Triprequested

Hotelbooked

Carbooked

Requesttrip

Flightfailed

Car canceled

Hotel canceled

Perform undo(cancel car booking)

Perform undo(cancel hotel)

@berndruecker

Page 51: Lost in transaction? - JAX London...If your transaction involves 2 to 4 steps, choreography might be a very good fit. However, this approach can rapidly become confusing if you keep

The danger is that it's very easy to make nicely decoupled systems with event notification, without realizing that you're losing sight of that larger-scale flow, and thus set yourself up for trouble in future years.

https://martinfowler.com/articles/201701-event-driven.html

@berndruecker

Page 52: Lost in transaction? - JAX London...If your transaction involves 2 to 4 steps, choreography might be a very good fit. However, this approach can rapidly become confusing if you keep

The danger is that it's very easy to make nicely decoupled systems with event notification, without realizing that you're losing sight of that larger-scale flow, and thus set yourself up for trouble in future years.

https://martinfowler.com/articles/201701-event-driven.html

@berndruecker

Page 53: Lost in transaction? - JAX London...If your transaction involves 2 to 4 steps, choreography might be a very good fit. However, this approach can rapidly become confusing if you keep

The danger is that it's very easy to make nicely decoupled systems with event notification, without realizing that you're losing sight of that larger-scale flow, and thus set yourself up for trouble in future years.

https://martinfowler.com/articles/201701-event-driven.html

@berndruecker

Page 54: Lost in transaction? - JAX London...If your transaction involves 2 to 4 steps, choreography might be a very good fit. However, this approach can rapidly become confusing if you keep

If your transaction involves 2 to 4 steps, choreography might be a very good fit.

However, this approach can rapidly become confusing if you keep adding extra steps in your transaction as it is difficult to track which services listen to which events. Moreover, it also might add a cyclic dependency between services as they have to subscribe to one another’s events.

Denis RosaCouchbase

https://blog.couchbase.com/saga-pattern-implement-business-transactions-using-microservices-part/

@berndruecker

Page 55: Lost in transaction? - JAX London...If your transaction involves 2 to 4 steps, choreography might be a very good fit. However, this approach can rapidly become confusing if you keep

Implementing changes in the process

Hotel

Flight

Car

Trip

Tripfailed

Triprequested

Hotelbooked

Carbooked

Requesttrip

Flightfailed

Car canceled

Hotel canceled

We have a new basic agreementwith the car rental agency and can cancel for free within 1 hour

– do that first!

@berndruecker

Page 56: Lost in transaction? - JAX London...If your transaction involves 2 to 4 steps, choreography might be a very good fit. However, this approach can rapidly become confusing if you keep

Implementing changes in the process

Hotel

Flight

Car

Trip

Tripfailed

Triprequested

Hotelbooked

Carbooked

Requesttrip

Flightfailed

Car canceled

Hotel canceled

You have to adjust all services and redeploy at the same time!

We have a new basic agreementwith the car rental agency and can cancel for free within 1 hour

– do that first!

@berndruecker

Page 58: Lost in transaction? - JAX London...If your transaction involves 2 to 4 steps, choreography might be a very good fit. However, this approach can rapidly become confusing if you keep

Orchestration

Hotel

Flight

Car

Trip

Tripbooked

Requesttrip

Bookhotel

Hotelbooked

Carbooked

Flightbooked

Book car

Bookflight

@berndruecker

Page 59: Lost in transaction? - JAX London...If your transaction involves 2 to 4 steps, choreography might be a very good fit. However, this approach can rapidly become confusing if you keep

Orchestration

Hotel

Flight

Car

Trip

Tripbooked

Requesttrip

Bookhotel

Hotelbooked

Carbooked

Flightbooked

Book car

Bookflight

We have a new basic agreementwith the car rental agency and can cancel for free within 1 hour

– do that first!

You have to adjust one service and redeploy only this one!

@berndruecker

Page 60: Lost in transaction? - JAX London...If your transaction involves 2 to 4 steps, choreography might be a very good fit. However, this approach can rapidly become confusing if you keep

Describe orchestration with BPMN

Trip

Tripbooked

Requesttrip

@berndruecker

Page 61: Lost in transaction? - JAX London...If your transaction involves 2 to 4 steps, choreography might be a very good fit. However, this approach can rapidly become confusing if you keep

The workflow is domain logic as part of the service

Trip

@berndruecker

Page 62: Lost in transaction? - JAX London...If your transaction involves 2 to 4 steps, choreography might be a very good fit. However, this approach can rapidly become confusing if you keep

The workflow is domain logic as part of the service

Trip

Payment

Payment could be onestep in the Trip Saga

@berndruecker

Page 63: Lost in transaction? - JAX London...If your transaction involves 2 to 4 steps, choreography might be a very good fit. However, this approach can rapidly become confusing if you keep

Caitie McCaffrey | @caitie

@berndruecker

Page 64: Lost in transaction? - JAX London...If your transaction involves 2 to 4 steps, choreography might be a very good fit. However, this approach can rapidly become confusing if you keep

Graphical models?

@berndruecker

Page 65: Lost in transaction? - JAX London...If your transaction involves 2 to 4 steps, choreography might be a very good fit. However, this approach can rapidly become confusing if you keep

Clemens VastersArchitect at Microsoft

http://vasters.com/archive/Sagas.html

@berndruecker

Page 66: Lost in transaction? - JAX London...If your transaction involves 2 to 4 steps, choreography might be a very good fit. However, this approach can rapidly become confusing if you keep

Clemens VastersArchitect at Microsoft

http://vasters.com/archive/Sagas.html

@berndruecker

Page 67: Lost in transaction? - JAX London...If your transaction involves 2 to 4 steps, choreography might be a very good fit. However, this approach can rapidly become confusing if you keep

Clemens VastersArchitect at Microsoft

http://vasters.com/archive/Sagas.html

@berndruecker

Page 68: Lost in transaction? - JAX London...If your transaction involves 2 to 4 steps, choreography might be a very good fit. However, this approach can rapidly become confusing if you keep

BPMNBusiness Process

Model and Notation

ISO Standard

@berndruecker

Page 69: Lost in transaction? - JAX London...If your transaction involves 2 to 4 steps, choreography might be a very good fit. However, this approach can rapidly become confusing if you keep

Living documentation for long-running behaviour

@berndruecker

Page 70: Lost in transaction? - JAX London...If your transaction involves 2 to 4 steps, choreography might be a very good fit. However, this approach can rapidly become confusing if you keep

Visual HTML reports for test cases

@berndruecker

Page 71: Lost in transaction? - JAX London...If your transaction involves 2 to 4 steps, choreography might be a very good fit. However, this approach can rapidly become confusing if you keep

BizDevOps

@berndruecker

Page 72: Lost in transaction? - JAX London...If your transaction involves 2 to 4 steps, choreography might be a very good fit. However, this approach can rapidly become confusing if you keep

Fancy a DSL? Just do it!

https://github.com/berndruecker/flowing-trip-booking-saga

The visual getauto-generated…

@berndruecker

Page 73: Lost in transaction? - JAX London...If your transaction involves 2 to 4 steps, choreography might be a very good fit. However, this approach can rapidly become confusing if you keep

Thoughts on the state machine | workflow engine market

@berndruecker

Page 74: Lost in transaction? - JAX London...If your transaction involves 2 to 4 steps, choreography might be a very good fit. However, this approach can rapidly become confusing if you keep

Thoughts on the state machine | workflow engine market

OSS Workflow orOrchestration Engines

Stack Vendors,Pure Play BPMS

Low Code Platforms

Homegrown frameworksto scratch an itch

Integration Frameworks

Cloud Offerings

Uber, Netflix, AirBnb, ING, … AWS Step Functions, Azure Durable Functions, …

Camunda, Zeebe, jBPM, Activiti, Mistral, …

PEGA, IBM, SAG, …

Apache Airflow, Spring Data Flow, …

Apache Camel, Balerina, …

Data Pipelines

@berndruecker

Page 75: Lost in transaction? - JAX London...If your transaction involves 2 to 4 steps, choreography might be a very good fit. However, this approach can rapidly become confusing if you keep

Does it support stateful operations?

Does it support the necessary flow logic?

Does it support BizDevOps?

Does it scale?

@berndruecker

Page 76: Lost in transaction? - JAX London...If your transaction involves 2 to 4 steps, choreography might be a very good fit. However, this approach can rapidly become confusing if you keep

My personal pro-tip for a shortlist ;-)

OSS Workflow orOrchestration Engines

Stack Vendors,Pure Play BPMS

Low Code Platforms

Homegrown frameworksto scratch an itch

Integration Frameworks

Cloud Offerings

Data Pipelines

Camunda & Zeebe

@berndruecker

Page 77: Lost in transaction? - JAX London...If your transaction involves 2 to 4 steps, choreography might be a very good fit. However, this approach can rapidly become confusing if you keep

Recap

• Aggregates = Consistency boundaries• Grown ups don‘t use distributed transactions

but eventual consistency• Idempotency is super important• Some consistency challenges require state• Stateful retry & cleanup• Saga / Compensation

@berndruecker

Page 78: Lost in transaction? - JAX London...If your transaction involves 2 to 4 steps, choreography might be a very good fit. However, this approach can rapidly become confusing if you keep

Thank you!

@berndruecker

Page 79: Lost in transaction? - JAX London...If your transaction involves 2 to 4 steps, choreography might be a very good fit. However, this approach can rapidly become confusing if you keep

[email protected]@berndruecker

https://berndruecker.io

https://medium.com/berndruecker

https://github.com/berndruecker

https://www.infoq.com/articles/events-workflow-automation

Contact:

Slides:

Blog:

Code:

https://www.infoworld.com/article/3254777/application-development/3-common-pitfalls-of-microservices-integrationand-how-to-avoid-them.html

https://thenewstack.io/5-workflow-automation-use-cases-you-might-not-have-considered/