132
Distributed Sagas A Protocol for Coordinating Microservices

Distributed sagas a protocol for coordinating microservices

Embed Size (px)

Citation preview

Page 1: Distributed sagas a protocol for coordinating microservices

Distributed SagasA Protocol for Coordinating Microservices

Page 2: Distributed sagas a protocol for coordinating microservices

Caitie McCaffreyDistributed Systems Engineer

caitiem.com

@caitie

Page 3: Distributed sagas a protocol for coordinating microservices

Service ServiceServiceMonoliths

Page 4: Distributed sagas a protocol for coordinating microservices

Microservices & NoSQL

Page 5: Distributed sagas a protocol for coordinating microservices

2015

Page 6: Distributed sagas a protocol for coordinating microservices

2015

“Application-level Mechanisms for maintaining

database integrity”

Page 7: Distributed sagas a protocol for coordinating microservices

Reserve a Hotel Front End

Page 8: Distributed sagas a protocol for coordinating microservices

Reserve a Hotel Front End

Feral Concurrency Control Mechanisms

Page 9: Distributed sagas a protocol for coordinating microservices

Reserve a Hotel Front End

Feral Concurrency Control Mechanisms

Page 10: Distributed sagas a protocol for coordinating microservices

Reserve a Car Front End

Page 11: Distributed sagas a protocol for coordinating microservices

Reserve a Flight Front End

Page 12: Distributed sagas a protocol for coordinating microservices

Trips Front End

Page 13: Distributed sagas a protocol for coordinating microservices

Trips Front End

Trips

Page 14: Distributed sagas a protocol for coordinating microservices

Trips Front End

Trips

Feral Concurrency Control Mechanisms

Page 15: Distributed sagas a protocol for coordinating microservices

Trips Front End

Trips

Feral Concurrency Control Mechanisms

Page 16: Distributed sagas a protocol for coordinating microservices

Trips Front End

Trips

Feral Concurrency Control Mechanisms

Page 17: Distributed sagas a protocol for coordinating microservices

Front End

Trips

Architecture

Page 18: Distributed sagas a protocol for coordinating microservices

Death Star Architectures

Page 19: Distributed sagas a protocol for coordinating microservices

Can We Do Betterthan Feral Concurrency Control?

Page 20: Distributed sagas a protocol for coordinating microservices

2012

Page 21: Distributed sagas a protocol for coordinating microservices

2012

“Spanner is Google’s scalable, multi-version, globally distributed, and

synchronously-replicated database”

Page 22: Distributed sagas a protocol for coordinating microservices

2015

Page 23: Distributed sagas a protocol for coordinating microservices

2015

“The biggest barrier…is that consistency mechanisms must

integrate across many stateful services”

Page 24: Distributed sagas a protocol for coordinating microservices

Two Phase Commit

Page 25: Distributed sagas a protocol for coordinating microservices

2PC: Prepare Front End

Trips

Propose Book Hotel

Propose Book Car

Propose Book Flight

Page 26: Distributed sagas a protocol for coordinating microservices

2PC: Prepare Front End

Trips

Vote Book Hotel

Vote Book Car

Vote Book Flight

Page 27: Distributed sagas a protocol for coordinating microservices

2PC: Commit Front End

Trips

Commit/Abort Book Hotel

Commit/Abort Book Car

Commit/Abort Book Flight

Page 28: Distributed sagas a protocol for coordinating microservices

2PC: Commit Front End

Trips

Done

Done

Done

Page 29: Distributed sagas a protocol for coordinating microservices

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

Page 30: Distributed sagas a protocol for coordinating microservices

Distributed SagasA Protocol for Coordinating Microservices

Page 31: Distributed sagas a protocol for coordinating microservices

1987

Page 32: Distributed sagas a protocol for coordinating microservices

1987

“Sagas are Long Lived Transactions [in a single relational Database]”

Page 33: Distributed sagas a protocol for coordinating microservices

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.”

Page 34: Distributed sagas a protocol for coordinating microservices

Distributed SagasA Protocol for Coordinating Microservices

Page 35: Distributed sagas a protocol for coordinating microservices

A Distributed Saga is a Collection of Requests

Book Hotel Book Car Book Flight Charge Money

Page 36: Distributed sagas a protocol for coordinating microservices

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

Page 37: Distributed sagas a protocol for coordinating microservices

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

Page 38: Distributed sagas a protocol for coordinating microservices

Distributed SagaRequests

Page 39: Distributed sagas a protocol for coordinating microservices

RequestsCan Abort

Book Car

Page 40: Distributed sagas a protocol for coordinating microservices

RequestsCan Abort

Book Car

Page 41: Distributed sagas a protocol for coordinating microservices

RequestsMust Be Idempotent

Book Car

Page 42: Distributed sagas a protocol for coordinating microservices

RequestsMust Be Idempotent

Book Car

Page 43: Distributed sagas a protocol for coordinating microservices

RequestsMust Be Idempotent

Book Car

Page 44: Distributed sagas a protocol for coordinating microservices

RequestsMust Be Idempotent

Book Car

Page 45: Distributed sagas a protocol for coordinating microservices

RequestsMust Be Idempotent

Book Car

Page 46: Distributed sagas a protocol for coordinating microservices

RequestsMust Be Idempotent

Book CarTimeout

Page 47: Distributed sagas a protocol for coordinating microservices

RequestsMust Be Idempotent

Book Car

Page 48: Distributed sagas a protocol for coordinating microservices

RequestsMust Be Idempotent

Book Car

Page 49: Distributed sagas a protocol for coordinating microservices

Distributed SagaCompensating Requests

Page 50: Distributed sagas a protocol for coordinating microservices

Compensating RequestsSemantically undoes the effect of a request

Page 51: Distributed sagas a protocol for coordinating microservices

Cancel CarCompensating

RequestsCannot Abort

Page 52: Distributed sagas a protocol for coordinating microservices

Cancel CarCompensating

RequestsCan Not Abort

Page 53: Distributed sagas a protocol for coordinating microservices

Cancel CarCompensating

RequestsMust Be Idempotent

Page 54: Distributed sagas a protocol for coordinating microservices

Compensating Requests

Must Be Commutative with Requests

Book CarCancel Car

Cancel CarBook Car

is the same as

Page 55: Distributed sagas a protocol for coordinating microservices

Compensating Requests

Book Car

Must Be Commutative with Requests

Page 56: Distributed sagas a protocol for coordinating microservices

Compensating Requests

Book CarTimeout

Must Be Commutative with Requests

Page 57: Distributed sagas a protocol for coordinating microservices

Compensating Requests

Book Car

Book Car

Must Be Commutative with Requests

Page 58: Distributed sagas a protocol for coordinating microservices

Compensating Requests

Book Car

Book Car

Cancel CarMust Be Commutative with Requests

Page 59: Distributed sagas a protocol for coordinating microservices

Compensating Requests

Book Car

Book Car

Cancel CarMust Be Commutative with Requests

Page 60: Distributed sagas a protocol for coordinating microservices

Compensating RequestsRequests

Idempotent Idempotent

Commutative

Can Not Abort

Can Abort

Page 61: Distributed sagas a protocol for coordinating microservices

Distributed Saga Guarantee

Book Hotel Book Car Book Flight Charge Money

All requests were completed successfully

Page 62: Distributed sagas a protocol for coordinating microservices

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

Page 63: Distributed sagas a protocol for coordinating microservices

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

Page 64: Distributed sagas a protocol for coordinating microservices

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 IsolationBook Hotel

Book Car

Visible before Saga Completes

Page 65: Distributed sagas a protocol for coordinating microservices

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

Page 66: Distributed sagas a protocol for coordinating microservices

Distributed Sagadefining a

Page 67: Distributed sagas a protocol for coordinating microservices

Directed Acyclic GraphDistributed Saga Car FlightHotel

Payment

Start Saga

End Saga

Page 68: Distributed sagas a protocol for coordinating microservices

Name: Hotel

Request: Book Hotel

Compensating Request: Cancel Hotel

Status: Not Completed

VertexDistributed Saga

Page 69: Distributed sagas a protocol for coordinating microservices

Start & End Vertices Distributed Saga

Start Saga

End Saga

Car FlightHotel

Payment

Start Saga

End Saga

Page 70: Distributed sagas a protocol for coordinating microservices

Distributed Saga Logfault-tolerant & highly available

Page 71: Distributed sagas a protocol for coordinating microservices

Saga Execution Coordinator

SECCar FlightHotel

Payment

Start Saga

End Saga

Saga

Log

Page 72: Distributed sagas a protocol for coordinating microservices

Distributed Saga Executing a

Page 73: Distributed sagas a protocol for coordinating microservices

Car FlightHotel

Payment

Start Saga

End Saga

Saga

Log

Book Trip Request

SEC

Page 74: Distributed sagas a protocol for coordinating microservices

Car FlightHotel

Payment

Start Saga

End Saga

SECSaga

Log

Start Saga

Book Trip Request

Page 75: Distributed sagas a protocol for coordinating microservices

Car FlightHotel

Payment

Start Saga

End Saga

SECSaga

Log

Done

Start Saga

Book Trip Request

Page 76: Distributed sagas a protocol for coordinating microservices

Car FlightHotel

Payment

Start Saga

End Saga

Saga

Log

SECStart Hotel

Start Saga

Page 77: Distributed sagas a protocol for coordinating microservices

Car FlightHotel

Payment

Start Saga

End Saga

Saga

Log

SECDone

Start Saga

Start Hotel

Page 78: Distributed sagas a protocol for coordinating microservices

Car FlightHotel

Payment

Start Saga

End Saga

Saga

Log

SECBook Hotel

Request

Start Saga

Start Hotel

Page 79: Distributed sagas a protocol for coordinating microservices

Car FlightHotel

Payment

Start Saga

End Saga

Saga

Log

SEC

Book Hotel Response

Start Saga

Start Hotel

Page 80: Distributed sagas a protocol for coordinating microservices

Car FlightHotel

Payment

Start Saga

End Saga

Saga

Log

SECEnd Hotel

Start Saga

Start Hotel

Page 81: Distributed sagas a protocol for coordinating microservices

Car FlightHotel

Payment

Start Saga

End Saga

Saga

Log

SECDone

Start Saga

Start Hotel

End Hotel

Page 82: Distributed sagas a protocol for coordinating microservices

Car FlightHotel

Payment

Start Saga

End Saga

Saga

Log

SECStart Car

Start Saga

Start Hotel

End Hotel

Page 83: Distributed sagas a protocol for coordinating microservices

Car FlightHotel

Payment

Start Saga

End Saga

Saga

Log

SECDone

Book Car Request

Start Saga

Start Hotel

End Hotel

Start Car

Page 84: Distributed sagas a protocol for coordinating microservices

Car FlightHotel

Payment

Start Saga

End Saga

Saga

Log

Start Saga

SEC

Start Hotel

End Hotel

Start Car

Book Car Response

End Car

Page 85: Distributed sagas a protocol for coordinating microservices

Car FlightHotel

Payment

Start Saga

End Saga

Saga

Log

Start Saga

SEC

Start Hotel

End Hotel

Start CarDone

End Car

Page 86: Distributed sagas a protocol for coordinating microservices

Car FlightHotel

Payment

Start Saga

End Saga

Saga

Log

Start Saga

SEC

Start Hotel

End Hotel

Start CarStart Flight

End Car

Page 87: Distributed sagas a protocol for coordinating microservices

Car FlightHotel

Payment

Start Saga

End Saga

Saga

Log

Start Saga

SEC

Start Hotel

End Hotel

Start Car

DoneEnd Car

Start Flight Book Flight Request

Page 88: Distributed sagas a protocol for coordinating microservices

Car FlightHotel

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

Page 89: Distributed sagas a protocol for coordinating microservices

Car FlightHotel

Payment

Start Saga

End Saga

Saga

Log

Start Saga

SEC

Start Hotel

End Hotel

Start Car

End Car

Start Flight

Done

End Flight

Page 90: Distributed sagas a protocol for coordinating microservices

Car FlightHotel

Payment

Start Saga

End Saga

Saga

Log

Start Saga

SEC

Start Hotel

End Hotel

Start Car

End Car

Start Flight

Start Payment

End Flight

Page 91: Distributed sagas a protocol for coordinating microservices

Car FlightHotel

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

Page 92: Distributed sagas a protocol for coordinating microservices

Car FlightHotel

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

Page 93: Distributed sagas a protocol for coordinating microservices

Car FlightHotel

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

Page 94: Distributed sagas a protocol for coordinating microservices

Car FlightHotel

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

Page 95: Distributed sagas a protocol for coordinating microservices

Car FlightHotel

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

Page 96: Distributed sagas a protocol for coordinating microservices

Car FlightHotel

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

Page 97: Distributed sagas a protocol for coordinating microservices

Distributed Saga Failure of a

Page 98: Distributed sagas a protocol for coordinating microservices

Car FlightHotel

Payment

Start Saga

End Saga

Saga

Log

SECBook Car Request

Start Saga

Start Hotel

End Hotel

Start Car

Start Flight

Page 99: Distributed sagas a protocol for coordinating microservices

Car FlightHotel

Payment

Start Saga

End Saga

Saga

Log

Start Saga

SEC

Start Hotel

End Hotel

Book Car Response

Abort CarStart Car

Start Flight

Page 100: Distributed sagas a protocol for coordinating microservices

Car FlightHotel

Payment

Start Saga

End Saga

Saga

Log

Start Saga

SEC

Start Hotel

End Hotel

DoneStart Car

Start Flight

Abort Car

Page 101: Distributed sagas a protocol for coordinating microservices

Car FlightHotel

Payment

Start Saga

End Saga

Saga

Log

Start Saga

SEC

Start Hotel

End Hotel

DoneStart Car

Start Flight

Abort Car

Rollback Recovery

Page 102: Distributed sagas a protocol for coordinating microservices

Car FlightHotel

Payment

End Comp Saga

Start Comp Saga

Saga

Log

Start Saga

SEC

Start Hotel

End Hotel

Start Car

Start Flight

Abort Car

Page 103: Distributed sagas a protocol for coordinating microservices

Car FlightHotel

Payment

Saga

Log

Start Saga

SEC

Start Hotel

End Hotel

Start Car

Start Flight

Abort Car

Start Comp Saga

End Comp Saga

Page 104: Distributed sagas a protocol for coordinating microservices

Car FlightHotel

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

Page 105: Distributed sagas a protocol for coordinating microservices

Car FlightHotel

Payment

Saga

Log

Start Saga

SEC

Start Hotel

End Hotel

{ }Start Car

Start Flight

Abort Car

Start Comp Saga

End Comp Saga

Page 106: Distributed sagas a protocol for coordinating microservices

Car FlightHotel

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

Page 107: Distributed sagas a protocol for coordinating microservices

Car FlightHotel

Payment

Saga

Log

Start Saga

SEC

Start Hotel

End Hotel

Start Car

Start Flight

Abort Car

{ Start, Abort }

Start Comp Saga

End Comp Saga

Page 108: Distributed sagas a protocol for coordinating microservices

Car FlightHotel

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

Page 109: Distributed sagas a protocol for coordinating microservices

Car FlightHotel

Payment

Saga

Log

Start Saga

SEC

Start Hotel

End Hotel

Start Car

Start Flight

Abort Car

{ Start, End }

Start Comp Saga

End Comp Saga

Page 110: Distributed sagas a protocol for coordinating microservices

Car FlightHotel

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

Page 111: Distributed sagas a protocol for coordinating microservices

Car FlightHotel

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

Page 112: Distributed sagas a protocol for coordinating microservices

Car FlightHotel

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

Page 113: Distributed sagas a protocol for coordinating microservices

Car FlightHotel

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

Page 114: Distributed sagas a protocol for coordinating microservices

Car FlightHotel

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

Page 115: Distributed sagas a protocol for coordinating microservices

Car FlightHotel

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

Page 116: Distributed sagas a protocol for coordinating microservices

Car FlightHotel

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

Page 117: Distributed sagas a protocol for coordinating microservices

Car FlightHotel

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

Page 118: Distributed sagas a protocol for coordinating microservices

Car FlightHotel

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

Page 119: Distributed sagas a protocol for coordinating microservices

Car FlightHotel

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

Page 120: Distributed sagas a protocol for coordinating microservices

Car FlightHotel

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

Page 121: Distributed sagas a protocol for coordinating microservices

Car FlightHotel

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

Page 122: Distributed sagas a protocol for coordinating microservices

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

Page 123: Distributed sagas a protocol for coordinating microservices

Saga Execution CoordinatorRecovering from

Failure

Page 124: Distributed sagas a protocol for coordinating microservices

Front End

SEC

Start Saga

End Saga

Saga

Log

Page 125: Distributed sagas a protocol for coordinating microservices

Front End

SEC

Start Saga

End Saga

Saga

Log

Page 126: Distributed sagas a protocol for coordinating microservices

Front End

SEC

Start Saga

End Saga

Saga

Log

Page 127: Distributed sagas a protocol for coordinating microservices

Front End

SEC

Saga

Log

Start Saga

End Saga

Page 128: Distributed sagas a protocol for coordinating microservices

with Distributed SagasIsolation of Complex Code

Fron

Tr

Complex Code Lives Everywhere

Front

SECSa

ga L

og

Complex Code Lives Here

Complex Code Lives Here

Page 129: Distributed sagas a protocol for coordinating microservices

Fron

Tr

with Distributed SagasModular Services

Front

SECSa

ga L

og

Only Flight Booking

Flight Booking & Feral Concurrency

Control Mechanisms

Page 130: Distributed sagas a protocol for coordinating microservices

Fron

Tr

with Distributed SagasService Composition

Front

SECSa

ga L

og

Just Add A New Saga! New Service &

Feral Concurrency Control Mechanisms

Page 131: Distributed sagas a protocol for coordinating microservices

Distributed Sagas Makes Building & Modifying Microservices Easier

Page 132: Distributed sagas a protocol for coordinating microservices

Thank you@caitie