Lost in transaction? · If your transaction involves 2 to 4 steps, choreography might be a very...

76
@berndruecker Lost in transaction? Strategies to deal with (in)consistency in distributed systems

Transcript of Lost in transaction? · If your transaction involves 2 to 4 steps, choreography might be a very...

Page 1: Lost in transaction? · 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

@berndruecker

Lost in transaction?

Strategies to deal with(in)consistency in distributed systems

Page 2: Lost in transaction? · 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

Do A

Do B

All ornothing

+

try {tx.begin();doA();doB();tx.commit();} catch (Exception e) {tx.rollback();}

@Transactionalpublic void createCustomer(Customer cust) {// ...

}

Or simply:

Once upon a time:

Page 3: Lost in transaction? · 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

ACID

AtomicityConsistencyIsolationDurability

Page 4: Lost in transaction? · 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

Distributed systems

Page 5: Lost in transaction? · 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

Distributed systems

Page 6: Lost in transaction? · 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

Distributed systems

Page 7: Lost in transaction? · 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

But there is two-phase commit (XA)!!

TXCoordinator

ResourceManagers

PreparePhase

CommitPhase

Page 8: Lost in transaction? · 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

Pat Helland

Distributed Systems Guru Worked at Amazon, Microsoft & Salesforce

Page 9: Lost in transaction? · 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

Pat Helland

Grown-Ups Don’t Use Distributed Transactions

Distributed Systems Guru Worked at Amazon, Microsoft & Salesforce

Page 10: Lost in transaction? · 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

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

Photo by John Ingle

Page 11: Lost in transaction? · 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

Eric BrewerAtomicityConsistencyIsolationDurability

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

Page 12: Lost in transaction? · 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

That means

Do A

Do B

Temporarilyinconsistent

Eventuallyconsistentagain

t

Consistent

LocalACID

LocalACID

1 (micro-)service1 aggregate1 program1 resource

Violates „I“ of ACID

Page 13: Lost in transaction? · 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

You

mig

htkn

owth

isfr

om:

Do A

Do B

Temporarilyinconsistent

Eventuallyconsistentagain

t

Consistent

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

Page 14: Lost in transaction? · 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

„Building on Quicksand“ Paper

ACID2.0Pat Helland

Page 15: Lost in transaction? · 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

AssociativeCommutativeIdempotentDistributed2.0

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

a + b = b + a

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

„Building on Quicksand“ Paper

Pat Helland

Page 17: Lost in transaction? · 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

Requirement: Idempotency of services!

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

Page 18: Lost in transaction? · 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

Requirement: Idempotency of services!

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

Page 19: Lost in transaction? · 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

Example

CreditCard

Paymentcharge

Page 20: Lost in transaction? · 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

Strategy: retry

CreditCard

Payment

Charge Credit CardcardNumber

amount

Charge Credit CardcardNumber

amounttransactionId

Not idempotent

Idempotent

has to be idempotent

charge

Page 21: Lost in transaction? · 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

Distributed

Page 22: Lost in transaction? · 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

It is impossible todifferentiate certain

failure scenarios:

Independant ofcommunication style!

Service Provider

Client

Page 23: Lost in transaction? · 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

Strategy: Cleanup

CreditCard

Paymentcharge

Make sure it isnot charged!

Cancel chargecardNumber

amounttransactionId

Raisepayment failed

Page 24: Lost in transaction? · 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

Some communication challengesrequire state.

Page 25: Lost in transaction? · 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

Strategy: Stateful retry

CreditCard

Paymentcharge

Page 26: Lost in transaction? · 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

Strategy: Stateful retry

CreditCard

Paymentcharge

Make sure it isnot charged!

Page 27: Lost in transaction? · 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

Warning:Contains Opinion

Page 28: Lost in transaction? · 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

Berlin, Germany

[email protected]@berndruecker

Bernd RueckerCo-founder and Chief Technologist ofCamunda

Page 29: Lost in transaction? · 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

Let‘s use a lightweight OSS workflow engine for this:

Page 30: Lost in transaction? · 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

Payment

Stateful retry

CreditCardREST

Page 31: Lost in transaction? · 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

Stateful retry & cleanup

CreditCard

PaymentREST

Cancel

charge

Page 32: Lost in transaction? · 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

Live hacking

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

Page 33: Lost in transaction? · 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

Embedded Engine Example (Java)

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

Page 34: Lost in transaction? · 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

Remote Engine Example (Polyglot)

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

Page 35: Lost in transaction? · 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

A relatively common pattern

Service(e.g. Go)

Kafka / Rabbit

RDMS

1. Receive

4. Send additional events

2. Business Logic

3. Send response? ACK

Page 36: Lost in transaction? · 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

„Can this handle 15k requests per second?“

Page 37: Lost in transaction? · 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

„Yes.“

Page 38: Lost in transaction? · 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
Page 39: Lost in transaction? · 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
Page 40: Lost in transaction? · 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

https://blogs.msdn.microsoft.com/pathelland/2007/05/15/memories-guesses-and-apologies/

Page 41: Lost in transaction? · 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

Compensation – the classical example

Sagabookhotel

bookcar

bookflight

cancelhotel

cancelcar

1. 2. 3.

5.6.

In case of failuretrigger compensations

booktrip

Page 42: Lost in transaction? · 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

2 alterntive approaches: choreography & orchestration

Page 43: Lost in transaction? · 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

Event-driven choreography

Hotel

Flight

Car

Trip

Tripbooked

Flightbooked

Triprequested

Hotelbooked

Carbooked

Requesttrip

Page 44: Lost in transaction? · 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

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)

Page 45: Lost in transaction? · 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

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

Page 46: Lost in transaction? · 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

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

Page 47: Lost in transaction? · 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

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

Page 48: Lost in transaction? · 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

Classical example

Sagabookhotel

bookcar

bookflight

cancelhotel

cancelcar

1. 2. 3.

5.6.

In case of failuretrigger compensations

booktrip

Page 49: Lost in transaction? · 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

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/

Page 50: Lost in transaction? · 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

Microservice pioneers have become aware

Page 51: Lost in transaction? · 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

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!

Page 52: Lost in transaction? · 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

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!

Page 53: Lost in transaction? · 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

Photo by born1945, available under Creative Commons BY 2.0 license.

Page 55: Lost in transaction? · 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

Orchestration

Hotel

Flight

Car

Trip

Tripbooked

Requesttrip

Bookhotel

Hotelbooked

Carbooked

Flightbooked

Book car

Bookflight

Page 56: Lost in transaction? · 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

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!

Page 57: Lost in transaction? · 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

Describe orchestration with BPMN

Trip

Tripbooked

Requesttrip

Page 58: Lost in transaction? · 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

The workflow is part of the service

Trip

Page 59: Lost in transaction? · 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

The workflow is part of the service

Trip

Payment

Page 60: Lost in transaction? · 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

Caitie McCaffrey | @caitie

Page 61: Lost in transaction? · 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

Graphical models?

Page 62: Lost in transaction? · 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

Clemens VastersArchitect at Microsoft

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

Page 63: Lost in transaction? · 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

Clemens VastersArchitect at Microsoft

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

Page 64: Lost in transaction? · 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

Clemens VastersArchitect at Microsoft

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

Page 65: Lost in transaction? · 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

BPMNBusiness Process

Model and Notation

ISO Standard

Page 66: Lost in transaction? · 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

Living documentation for long-running behaviour

Page 67: Lost in transaction? · 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

Visual HTML reports for test cases

Page 68: Lost in transaction? · 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

BizDevOps

Page 69: Lost in transaction? · 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

Saga with AWS Step Functions

https://theburningmonk.com/2017/07/applying-the-saga-pattern-with-aws-lambda-and-step-functions/

Page 70: Lost in transaction? · 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

Thoughts on the state machine | workflow engine market

Page 71: Lost in transaction? · 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

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

Page 72: Lost in transaction? · 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

Does it support stateful operations?

Does it support the necessary flow logic?

Does it support BizDevOps?

Does it scale?

Page 73: Lost in transaction? · 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

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

Page 74: Lost in transaction? · 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

Recap

• Grown ups don‘t use distributed transactionsbut eventual consistency

• Idempotency is super important in distributed systems• Some consistency challenges require state• Know some strategies• Stateful retry & cleanup• Saga / Compensation• Apologies

Page 75: Lost in transaction? · 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

Thank you!

Page 76: Lost in transaction? · 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

[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/