86
@crichardson Managing data consistency in a microservice architecture using Sagas Chris Richardson Founder of eventuate.io Author of Microservices Patterns Founder of the original CloudFoundry.com Author of POJOs in Action @crichardson [email protected] http://eventuate.io http://learn.microservices.io

Managing Data Consistency in a Microservice Architecture ... · High risk => use 2PC/distributed transaction. @crichardson Agenda ACID is not an option Overview of sagas Coordinating

  • Upload
    others

  • View
    5

  • Download
    0

Embed Size (px)

Citation preview

Page 1: Managing Data Consistency in a Microservice Architecture ... · High risk => use 2PC/distributed transaction. @crichardson Agenda ACID is not an option Overview of sagas Coordinating

@crichardson

Managing data consistency in a microservice architecture using

SagasChris Richardson

Founder of eventuate.io Author of Microservices Patterns Founder of the original CloudFoundry.com Author of POJOs in Action

@crichardson [email protected] http://eventuate.io http://learn.microservices.io

Page 2: Managing Data Consistency in a Microservice Architecture ... · High risk => use 2PC/distributed transaction. @crichardson Agenda ACID is not an option Overview of sagas Coordinating

@crichardson

Presentation goal

Distributed data management challenges in a microservice architecture

Sagas as the transaction model

Page 3: Managing Data Consistency in a Microservice Architecture ... · High risk => use 2PC/distributed transaction. @crichardson Agenda ACID is not an option Overview of sagas Coordinating

@crichardson

About Chris

Page 4: Managing Data Consistency in a Microservice Architecture ... · High risk => use 2PC/distributed transaction. @crichardson Agenda ACID is not an option Overview of sagas Coordinating

@crichardson

About Chris

Consultant and trainer focusing on modern

application architectures including microservices

(http://www.chrisrichardson.net/)

Page 5: Managing Data Consistency in a Microservice Architecture ... · High risk => use 2PC/distributed transaction. @crichardson Agenda ACID is not an option Overview of sagas Coordinating

@crichardson

About Chris

Founder of a startup that is creating an open-source/SaaS platform

that simplifies the development of transactional microservices

(http://eventuate.io)

Page 6: Managing Data Consistency in a Microservice Architecture ... · High risk => use 2PC/distributed transaction. @crichardson Agenda ACID is not an option Overview of sagas Coordinating

@crichardson

For more information

http://learn.microservices.io

40% discount with code

ctwsaturn18

Page 7: Managing Data Consistency in a Microservice Architecture ... · High risk => use 2PC/distributed transaction. @crichardson Agenda ACID is not an option Overview of sagas Coordinating

@crichardson

Agenda

ACID is not an option

Overview of sagas

Coordinating sagas

Countermeasures for data anomalies

Using reliable and transactional messaging

Page 8: Managing Data Consistency in a Microservice Architecture ... · High risk => use 2PC/distributed transaction. @crichardson Agenda ACID is not an option Overview of sagas Coordinating

The microservice architecture structures

an application as a set of loosely coupled

services

Page 9: Managing Data Consistency in a Microservice Architecture ... · High risk => use 2PC/distributed transaction. @crichardson Agenda ACID is not an option Overview of sagas Coordinating

@crichardson

Microservices enable continuous delivery/deployment

Process: Continuous delivery/deployment

Organization:Small, agile, autonomous,

cross functional teams

Architecture: Microservice architecture

Enables

Enables Enables

SuccessfulSoftware

Development

Services =

testability and

deployability

Teams own services

Page 10: Managing Data Consistency in a Microservice Architecture ... · High risk => use 2PC/distributed transaction. @crichardson Agenda ACID is not an option Overview of sagas Coordinating

@crichardson

Each team owns one or more services

Catalog Service

Review Service

Order Service

… Service

Catalog Team

Review Team

Order Team

… Team

Responsible for

Page 11: Managing Data Consistency in a Microservice Architecture ... · High risk => use 2PC/distributed transaction. @crichardson Agenda ACID is not an option Overview of sagas Coordinating

@crichardson

Microservice architecture

Browser

Mobile Device

Store Front UI

API Gateway

Customer Service

Order Service

… Service

Customer Database

Order Database

… Database

HTML

REST

REST

Database per service

Page 12: Managing Data Consistency in a Microservice Architecture ... · High risk => use 2PC/distributed transaction. @crichardson Agenda ACID is not an option Overview of sagas Coordinating

@crichardson

Private database !=

private database server

Page 13: Managing Data Consistency in a Microservice Architecture ... · High risk => use 2PC/distributed transaction. @crichardson Agenda ACID is not an option Overview of sagas Coordinating

@crichardson

Loose coupling = encapsulated data

Order Service Customer Service

Order Database Customer Database

Order table Customer table

orderTotal creditLimit availableCredit

Change schema without coordinating with other teams

Page 14: Managing Data Consistency in a Microservice Architecture ... · High risk => use 2PC/distributed transaction. @crichardson Agenda ACID is not an option Overview of sagas Coordinating

@crichardson

But…

How to maintain data consistency?

How to implement queries?

Page 15: Managing Data Consistency in a Microservice Architecture ... · High risk => use 2PC/distributed transaction. @crichardson Agenda ACID is not an option Overview of sagas Coordinating

@crichardson

How to maintain data consistency?createOrder(customerId, orderTotal)

Pre-conditions: • customerId is valid Post-conditions • Order was created • Customer.availableCredit -= orderTotal

Customer class

Invariant:

availableCredit >= 0 availableCredit <= creditLimit

Spans services

Page 16: Managing Data Consistency in a Microservice Architecture ... · High risk => use 2PC/distributed transaction. @crichardson Agenda ACID is not an option Overview of sagas Coordinating

@crichardson

Cannot use ACID transactions that span services

BEGIN TRANSACTION … SELECT ORDER_TOTAL FROM ORDERS WHERE CUSTOMER_ID = ? … SELECT CREDIT_LIMIT FROM CUSTOMERS WHERE CUSTOMER_ID = ? … INSERT INTO ORDERS … … COMMIT TRANSACTION

Private to the Order Service

Private to the Customer Service

Distributed transactions

Page 17: Managing Data Consistency in a Microservice Architecture ... · High risk => use 2PC/distributed transaction. @crichardson Agenda ACID is not an option Overview of sagas Coordinating

@crichardson

2PC is not an optionGuarantees consistency

BUT

2PC coordinator is a single point of failure

Chatty: at least O(4n) messages, with retries O(n^2)

Reduced throughput due to locks

Not supported by many NoSQL databases (or message brokers)

CAP theorem ⇒ 2PC impacts availability

….

Page 18: Managing Data Consistency in a Microservice Architecture ... · High risk => use 2PC/distributed transaction. @crichardson Agenda ACID is not an option Overview of sagas Coordinating

@crichardson

Basically Available Soft state Eventually consistent

http://queue.acm.org/detail.cfm?id=1394128

ACID ⇒

Page 19: Managing Data Consistency in a Microservice Architecture ... · High risk => use 2PC/distributed transaction. @crichardson Agenda ACID is not an option Overview of sagas Coordinating

@crichardson

Agenda

ACID is not an option

Overview of sagas

Coordinating sagas

Countermeasures for data anomalies

Using reliable and transactional messaging

Page 20: Managing Data Consistency in a Microservice Architecture ... · High risk => use 2PC/distributed transaction. @crichardson Agenda ACID is not an option Overview of sagas Coordinating

@crichardson

From a 1987 paper

Page 21: Managing Data Consistency in a Microservice Architecture ... · High risk => use 2PC/distributed transaction. @crichardson Agenda ACID is not an option Overview of sagas Coordinating

@crichardson

Saga

Use Sagas instead of 2PCDistributed transaction

Service A Service B

Service A

Local transaction

Service B

Local transaction

Service C

Local transaction

X Service C

Page 22: Managing Data Consistency in a Microservice Architecture ... · High risk => use 2PC/distributed transaction. @crichardson Agenda ACID is not an option Overview of sagas Coordinating

@crichardson

Order Service

Create Order Saga

Local transaction

Order

state=PENDING

createOrder()

Customer Service

Local transaction

Customer

reserveCredit()

Order Service

Local transaction

Order

state=APPROVED

approve order()

createOrder() Initiates saga

Page 23: Managing Data Consistency in a Microservice Architecture ... · High risk => use 2PC/distributed transaction. @crichardson Agenda ACID is not an option Overview of sagas Coordinating

@crichardson

But what about rollback?

BEGIN TRANSACTION … UPDATE … … INSERT …. … …. BUSINESS RULE VIOLATED!!!! … ROLLBACK TRANSACTION

Really simple!

Page 24: Managing Data Consistency in a Microservice Architecture ... · High risk => use 2PC/distributed transaction. @crichardson Agenda ACID is not an option Overview of sagas Coordinating

@crichardson

Rolling back sagas

Use compensating transactions

Developer must write application logic to “rollback” eventually consistent transactions

Careful design required!

Page 25: Managing Data Consistency in a Microservice Architecture ... · High risk => use 2PC/distributed transaction. @crichardson Agenda ACID is not an option Overview of sagas Coordinating

@crichardson

Saga: Every Ti has a Ci

T1 T2 …

C1 C2

Compensating transactions

T1 ⇒ T2 ⇒ C1

FAILS

Page 26: Managing Data Consistency in a Microservice Architecture ... · High risk => use 2PC/distributed transaction. @crichardson Agenda ACID is not an option Overview of sagas Coordinating

@crichardson

Order Service

Create Order Saga - rollback

Local transaction

Order

createOrder()

Customer Service

Local transaction

Customer

reserveCredit()

Order ServiceLocal transaction

Order

reject order()

createOrder()

FAIL

Insufficient credit

Page 27: Managing Data Consistency in a Microservice Architecture ... · High risk => use 2PC/distributed transaction. @crichardson Agenda ACID is not an option Overview of sagas Coordinating

@crichardson

Writing compensating transactions isn’t always easy

Write them so they will always succeed

If a compensating transaction fails => no clear way to recover

Challenge: Undoing changes when data has already been changed by a different transaction/saga

More on this later

Page 28: Managing Data Consistency in a Microservice Architecture ... · High risk => use 2PC/distributed transaction. @crichardson Agenda ACID is not an option Overview of sagas Coordinating

@crichardson

Non-compensatable actions

For example, sending an email can’t be unsent.

Move actions that can’t be undone to the end of the saga

More on this later.

Page 29: Managing Data Consistency in a Microservice Architecture ... · High risk => use 2PC/distributed transaction. @crichardson Agenda ACID is not an option Overview of sagas Coordinating

@crichardson

Sagas complicate API designSynchronous API vs Asynchronous Saga

Request initiates the saga. When to send back the response?

Option #1: Send response when saga completes:

+ Response specifies the outcome

- Reduced availability

Option #2: Send response immediately after creating the saga (recommended):

+ Improved availability

- Response does not specify the outcome. Client must poll or be notified

Page 30: Managing Data Consistency in a Microservice Architecture ... · High risk => use 2PC/distributed transaction. @crichardson Agenda ACID is not an option Overview of sagas Coordinating

@crichardson

Revised Create Order API

createOrder()

returns id of newly created order

NOT fully validated

getOrder(id)

Called periodically by client to get outcome of validation

Page 31: Managing Data Consistency in a Microservice Architecture ... · High risk => use 2PC/distributed transaction. @crichardson Agenda ACID is not an option Overview of sagas Coordinating

@crichardson

Minimal impact on UI

UI hides asynchronous API from the user

Saga will usually appear instantaneous (<= 100ms)

If it takes longer ⇒ UI displays “processing” popup

Server can push notification to UI

Page 32: Managing Data Consistency in a Microservice Architecture ... · High risk => use 2PC/distributed transaction. @crichardson Agenda ACID is not an option Overview of sagas Coordinating

@crichardson

Agenda

ACID is not an option

Overview of sagas

Coordinating sagas

Countermeasures for data anomalies

Using reliable and transactional messaging

Page 33: Managing Data Consistency in a Microservice Architecture ... · High risk => use 2PC/distributed transaction. @crichardson Agenda ACID is not an option Overview of sagas Coordinating

@crichardson

How to sequence the saga transactions?

After the completion of transaction Ti “something” must decide what step to execute next

Success: which T(i+1) - branching

Failure: C(i - 1)

Page 34: Managing Data Consistency in a Microservice Architecture ... · High risk => use 2PC/distributed transaction. @crichardson Agenda ACID is not an option Overview of sagas Coordinating

@crichardson

Use asynchronous, broker-based messaging

Order Service Customer Service ….

Message broker

Guaranteed delivery ensures a saga complete when its participants are temporarily unavailable

Page 35: Managing Data Consistency in a Microservice Architecture ... · High risk => use 2PC/distributed transaction. @crichardson Agenda ACID is not an option Overview of sagas Coordinating

@crichardson

Saga step = a transaction local to a service

Service

Database Message Broker

update publish message/event

Transactional DB: BEGIN … COMMITDDD: AggregateNoSQL: single ‘record’

Page 36: Managing Data Consistency in a Microservice Architecture ... · High risk => use 2PC/distributed transaction. @crichardson Agenda ACID is not an option Overview of sagas Coordinating

@crichardson

Choreography: distributed decision making

vs.

Orchestration: centralized decision making

Page 37: Managing Data Consistency in a Microservice Architecture ... · High risk => use 2PC/distributed transaction. @crichardson Agenda ACID is not an option Overview of sagas Coordinating

@crichardson

Option #1: Choreography-based coordination using events

Order Service

Customer Service

Order created

Credit Reserved

Credit Limit Exceeded

Create Order

OR

Customer

creditLimit creditReservations

Order

state total

create()

reserveCredit()approve()/ reject()

Order events channel

Customer events channel

Page 38: Managing Data Consistency in a Microservice Architecture ... · High risk => use 2PC/distributed transaction. @crichardson Agenda ACID is not an option Overview of sagas Coordinating

@crichardson

Order Service: publishing domain events

Publish event

Create order

Page 39: Managing Data Consistency in a Microservice Architecture ... · High risk => use 2PC/distributed transaction. @crichardson Agenda ACID is not an option Overview of sagas Coordinating

@crichardson

Customer Service: consuming domain events…

https://github.com/eventuate-tram/eventuate-tram-examples-customers-and-orders

Subscribe to event

Page 40: Managing Data Consistency in a Microservice Architecture ... · High risk => use 2PC/distributed transaction. @crichardson Agenda ACID is not an option Overview of sagas Coordinating

@crichardson

Customer Service: consuming domain events

Attempt to Reserve credit

Publish event on success

Publish event on failure

Page 41: Managing Data Consistency in a Microservice Architecture ... · High risk => use 2PC/distributed transaction. @crichardson Agenda ACID is not an option Overview of sagas Coordinating

@crichardson

More complex choreography example

Order Service

Consumer Service

Order created

Consumer Validated

Create Order

Order events channel

Consumer events channel

Inventory Service

Accounting Service

Inventory events channel

Inventory Reserved

Accounting events channel

Card Authorized

Page 42: Managing Data Consistency in a Microservice Architecture ... · High risk => use 2PC/distributed transaction. @crichardson Agenda ACID is not an option Overview of sagas Coordinating

Benefits and drawbacks of choreographyBenefits

Simple, especially when using event sourcing

Participants are loosely coupled

Drawbacks

Cyclic dependencies - services listen to each other’s events

Overloads domain objects, e.g. Order and Customer know too much

Events = indirect way to make something happen

https://github.com/eventuate-examples/eventuate-examples-java-customers-and-orders

Page 43: Managing Data Consistency in a Microservice Architecture ... · High risk => use 2PC/distributed transaction. @crichardson Agenda ACID is not an option Overview of sagas Coordinating

@crichardson

Order Service

Option #2: Orchestration-based saga coordination

Local transaction

Order

state=PENDING

createOrder()

Customer Service

Local transaction

Customer

reserveCredit()

Order Service

Local transaction

Order

state=APPROVED

approve order()

createOrder()CreateOrderSaga

InvokesInvokesInvokes

Page 44: Managing Data Consistency in a Microservice Architecture ... · High risk => use 2PC/distributed transaction. @crichardson Agenda ACID is not an option Overview of sagas Coordinating

@crichardson

A saga (orchestrator) is a persistent object

that tracks the state of the saga

and invokes the participants

Page 45: Managing Data Consistency in a Microservice Architecture ... · High risk => use 2PC/distributed transaction. @crichardson Agenda ACID is not an option Overview of sagas Coordinating

Saga orchestrator behaviorOn create:

Invokes a saga participant

Persists state in database

Wait for a reply

On reply:

Load state from database

Determine which saga participant to invoke next

Invokes saga participant

Updates its state

Persists updated state

Wait for a reply

Page 46: Managing Data Consistency in a Microservice Architecture ... · High risk => use 2PC/distributed transaction. @crichardson Agenda ACID is not an option Overview of sagas Coordinating

@crichardson

Order Service

CreateOrderSaga orchestrator

Customer Service

Create Order

Customer

creditLimit creditReservations ...

Order state total…

reserveCredit()

CreateOrderSaga

OrderService

create()

create()

approve()

creditReserved()

Customer command channel

Saga reply channel

Page 47: Managing Data Consistency in a Microservice Architecture ... · High risk => use 2PC/distributed transaction. @crichardson Agenda ACID is not an option Overview of sagas Coordinating

@crichardson

CreateOrderSaga definition

Sequence of steps

step = (Ti, Ci)

Build command to send

Saga’s Data

Page 48: Managing Data Consistency in a Microservice Architecture ... · High risk => use 2PC/distributed transaction. @crichardson Agenda ACID is not an option Overview of sagas Coordinating

@crichardson

Customer Service command handler

Route command to handler

Reserve credit

Make reply message

Page 49: Managing Data Consistency in a Microservice Architecture ... · High risk => use 2PC/distributed transaction. @crichardson Agenda ACID is not an option Overview of sagas Coordinating

@crichardson

Eventuate Tram Sagas

Open-source Saga orchestration framework

Currently for Java

https://github.com/eventuate-tram/eventuate-tram-sagas

https://github.com/eventuate-tram/eventuate-tram-sagas-examples-customers-and-orders

Page 50: Managing Data Consistency in a Microservice Architecture ... · High risk => use 2PC/distributed transaction. @crichardson Agenda ACID is not an option Overview of sagas Coordinating

Benefits and drawbacks of orchestrationBenefits

Centralized coordination logic is easier to understand

Reduced coupling, e.g. Customer knows less. Simply has API

Reduces cyclic dependencies

Drawbacks

Risk of smart sagas directing dumb services

Page 51: Managing Data Consistency in a Microservice Architecture ... · High risk => use 2PC/distributed transaction. @crichardson Agenda ACID is not an option Overview of sagas Coordinating

@crichardson

Agenda

ACID is not an option

Overview of sagas

Coordinating sagas

Countermeasures for data anomalies

Using reliable and transactional messaging

Page 52: Managing Data Consistency in a Microservice Architecture ... · High risk => use 2PC/distributed transaction. @crichardson Agenda ACID is not an option Overview of sagas Coordinating

@crichardson

Lack of isolation ⇒ complicates business logic

Order Service

Local transaction

Order

state=PENDING

createOrder()

Customer Service

Local transaction

Customer

reserveCredit()

Order Service

Local transactioncancelOrder()

?Time

Page 53: Managing Data Consistency in a Microservice Architecture ... · High risk => use 2PC/distributed transaction. @crichardson Agenda ACID is not an option Overview of sagas Coordinating

@crichardson

How to cancel a PENDING Order?

Don’t ⇒ throw an OrderNotCancellableException

Questionable user experience

“Interrupt” the Create Order saga?

Cancel Order Saga: set order.state = CANCELLED

Causes Create Order Saga to rollback

But is that enough to cancel the order?

Cancel Order saga waits for the Create Order saga to complete?

Suspiciously like a distributed lock

But perhaps that is ok

Page 54: Managing Data Consistency in a Microservice Architecture ... · High risk => use 2PC/distributed transaction. @crichardson Agenda ACID is not an option Overview of sagas Coordinating

@crichardson

Countermeasure Transaction Model

http://bit.ly/semantic-acid-ctm - paywall 😥

Page 55: Managing Data Consistency in a Microservice Architecture ... · High risk => use 2PC/distributed transaction. @crichardson Agenda ACID is not an option Overview of sagas Coordinating

@crichardson

Sagas are ACDAtomicity

Saga implementation ensures that all transactions are executed OR all are compensated

Consistency

Referential integrity within a service handled by local databases

Referential integrity across services handled by application

Durability

Durability handled by local databases

Page 56: Managing Data Consistency in a Microservice Architecture ... · High risk => use 2PC/distributed transaction. @crichardson Agenda ACID is not an option Overview of sagas Coordinating

@crichardson

Lack of I ⇒ anomalies

Page 57: Managing Data Consistency in a Microservice Architecture ... · High risk => use 2PC/distributed transaction. @crichardson Agenda ACID is not an option Overview of sagas Coordinating

@crichardson

Outcome of concurrent execution

!= a sequential execution

Page 58: Managing Data Consistency in a Microservice Architecture ... · High risk => use 2PC/distributed transaction. @crichardson Agenda ACID is not an option Overview of sagas Coordinating

@crichardson

Sounds scary BUT

It’s common to relax isolation to improve performance

Page 59: Managing Data Consistency in a Microservice Architecture ... · High risk => use 2PC/distributed transaction. @crichardson Agenda ACID is not an option Overview of sagas Coordinating

@crichardson

Anomaly: Lost update

Ti: Create Order

Tj: Approve Order

Ti: Cancel Order

Create Order Saga

Cancel Order Saga

Time

Overwrites cancelled order

Page 60: Managing Data Consistency in a Microservice Architecture ... · High risk => use 2PC/distributed transaction. @crichardson Agenda ACID is not an option Overview of sagas Coordinating

@crichardson

Anomaly: Dirty reads …

Ti: Reserve Credit

Ci: Unreserve credit

Ti: Reserve Credit

Reads uncommitted

changes

Saga 1

Saga 2

Order rejected unnecessarily

Time

Page 61: Managing Data Consistency in a Microservice Architecture ... · High risk => use 2PC/distributed transaction. @crichardson Agenda ACID is not an option Overview of sagas Coordinating

@crichardson

… Anomaly: Dirty reads

Ti: Unreserve Credit

Ci: Reserve credit

Ti: Reserve Credit

Reads uncommitted

changes

Saga 1

Saga 2

Credit limit exceeded!

Time

Page 62: Managing Data Consistency in a Microservice Architecture ... · High risk => use 2PC/distributed transaction. @crichardson Agenda ACID is not an option Overview of sagas Coordinating

@crichardson

Anomaly: non-repeatable/fuzzy read

Ti: Begin Revise Order

Tj: Finalize Revise Order

Ti: Update Order

Time

Order has changed since Ti

Saga 1

Saga 2

Page 63: Managing Data Consistency in a Microservice Architecture ... · High risk => use 2PC/distributed transaction. @crichardson Agenda ACID is not an option Overview of sagas Coordinating

@crichardson

Countermeasures for reducing impact

of isolation anomalies…*

i.e. good enough, eventually consistent application

Page 64: Managing Data Consistency in a Microservice Architecture ... · High risk => use 2PC/distributed transaction. @crichardson Agenda ACID is not an option Overview of sagas Coordinating

Saga structure

T1 C1

T2 C2

Tn+1

Tn+2

….

Compensatable transactions

Pivot transaction = GO/NO GO

Retriable transactions that can’t fail

Page 65: Managing Data Consistency in a Microservice Architecture ... · High risk => use 2PC/distributed transaction. @crichardson Agenda ACID is not an option Overview of sagas Coordinating

Countermeasure: Semantic lock

Compensatable transaction sets flag, retriable transaction releases it

Indicates a possible dirty read

Flag = lock:

prevents other transactions from accessing it

Require deadlock detection, e.g. timeout

Flag = warning - treat the data differently, e.g.

Order.state = PENDING

a pending deposit

… …

Approve Order

Create Pending Order Reject Order

Order.state = PENDING

Order.state = REJECTED

Order.state = APPROVED

Page 66: Managing Data Consistency in a Microservice Architecture ... · High risk => use 2PC/distributed transaction. @crichardson Agenda ACID is not an option Overview of sagas Coordinating

@crichardson

Countermeasure: Commutative updates

Commutative: g(f(x)) = f(g(x))

For example:

Account.debit() compensates for Account.credit()

Account.credit() compensates for Account.debit()

Avoids lost updates

Page 67: Managing Data Consistency in a Microservice Architecture ... · High risk => use 2PC/distributed transaction. @crichardson Agenda ACID is not an option Overview of sagas Coordinating

@crichardson

Countermeasure: Pessimistic view

Increase avail. credit

Reduce avail. credit

Cancel Order Delivery

Cancel Order

Reorder saga to

reduce riskCancel Order Delivery

Cancel Order

Increase avail. credit

… …… …

Won’t be compensated, so no dirty read

Page 68: Managing Data Consistency in a Microservice Architecture ... · High risk => use 2PC/distributed transaction. @crichardson Agenda ACID is not an option Overview of sagas Coordinating

@crichardson

Countermeasure: Re-read value

Ti: Read Order

Tj: Re-read, then update Order

Ti: Update Order

TimeSaga 1

Saga 2

Verify unchanged, possibly restart

Prevents lost update~Offline optimistic lock pattern

Page 69: Managing Data Consistency in a Microservice Architecture ... · High risk => use 2PC/distributed transaction. @crichardson Agenda ACID is not an option Overview of sagas Coordinating

@crichardson

Countermeasure: version file

Ti: Create Order

Tj: Authorize Credit Card

Ti: Cancel Order

TimeSaga 1

Saga 2

Tj: Reserve Credit

Tj: Reverse Credit Card

Payment

1. reverse() 2. authorize()

“log” of changes: Enables operations to commute

Page 70: Managing Data Consistency in a Microservice Architecture ... · High risk => use 2PC/distributed transaction. @crichardson Agenda ACID is not an option Overview of sagas Coordinating

@crichardson

Countermeasure: By value

Business risk determines strategy

Low risk => semantic ACID

High risk => use 2PC/distributed transaction

Page 71: Managing Data Consistency in a Microservice Architecture ... · High risk => use 2PC/distributed transaction. @crichardson Agenda ACID is not an option Overview of sagas Coordinating

@crichardson

Agenda

ACID is not an option

Overview of sagas

Coordinating sagas

Countermeasures for data anomalies

Using reliable and transactional messaging

Page 72: Managing Data Consistency in a Microservice Architecture ... · High risk => use 2PC/distributed transaction. @crichardson Agenda ACID is not an option Overview of sagas Coordinating

@crichardson

Use asynchronous, broker-based messaging

Order Service Customer Service ….

Message broker

Guaranteed delivery ensures a saga complete when its participants are temporarily unavailable

Page 73: Managing Data Consistency in a Microservice Architecture ... · High risk => use 2PC/distributed transaction. @crichardson Agenda ACID is not an option Overview of sagas Coordinating

@crichardson

Messaging must be transactional

Service

Database Message Broker

update publish

How to make atomic without 2PC?

Page 74: Managing Data Consistency in a Microservice Architecture ... · High risk => use 2PC/distributed transaction. @crichardson Agenda ACID is not an option Overview of sagas Coordinating

@crichardson

Option #1: Use database table as a message queue

Page 75: Managing Data Consistency in a Microservice Architecture ... · High risk => use 2PC/distributed transaction. @crichardson Agenda ACID is not an option Overview of sagas Coordinating

@crichardson

Option #1: Use database table as a message queue

ACID transaction

See BASE: An Acid Alternative, http://bit.ly/ebaybase

DELETE

?Customer Service

ORDER_ID CUSTOMER_ID TOTAL

99

CUSTOMER_CREDIT_RESERVATIONS table

101 1234

ID TYPE DATA DESTINATION

MESSAGE table

84784 CreditReserved {…} …

INSERT INSERT

Message Publisher

QUERY

Message Broker

Publish

Local transaction

reserveCredit()

Page 76: Managing Data Consistency in a Microservice Architecture ... · High risk => use 2PC/distributed transaction. @crichardson Agenda ACID is not an option Overview of sagas Coordinating

@crichardson

Publishing messages by polling the MESSAGE table

Message table

Message PublisherSELECT *

FROM MESSAGES WHERE ….

Message table

Publish message

DELETE …

Page 77: Managing Data Consistency in a Microservice Architecture ... · High risk => use 2PC/distributed transaction. @crichardson Agenda ACID is not an option Overview of sagas Coordinating

@crichardson

Transaction log tailingOrder

Service

Datastore

ORDER table

Transaction log

Update

Transaction log miner Message

Broker

PublishChanges

MESSAGE table

Page 78: Managing Data Consistency in a Microservice Architecture ... · High risk => use 2PC/distributed transaction. @crichardson Agenda ACID is not an option Overview of sagas Coordinating

@crichardson

Transaction log tailing

Eventuate Local - reads MySQL binlog

Oracle Golden Gate

AWS DynamoDB streams

MongoDB - Read the oplog

Page 79: Managing Data Consistency in a Microservice Architecture ... · High risk => use 2PC/distributed transaction. @crichardson Agenda ACID is not an option Overview of sagas Coordinating

Transaction log tailing: benefits and drawbacks

Benefits

No 2PC

No application changes required

Guaranteed to be accurate

Drawbacks

Obscure

Database specific solutions

Tricky to avoid duplicate publishing

Page 80: Managing Data Consistency in a Microservice Architecture ... · High risk => use 2PC/distributed transaction. @crichardson Agenda ACID is not an option Overview of sagas Coordinating

@crichardson

Option #2: Event sourcing: event-centric persistence

Page 81: Managing Data Consistency in a Microservice Architecture ... · High risk => use 2PC/distributed transaction. @crichardson Agenda ACID is not an option Overview of sagas Coordinating

@crichardson

Event sourcing: persists an object as a sequence of events

Service

Event Store

save events and

publish

Event table

Entity type Event id

Entity id

Event data

Order 902101 …OrderApproved

Order 903101 …OrderShipped

Event type

Order 901101 …OrderCreated

Every state change ⇒ event

Page 82: Managing Data Consistency in a Microservice Architecture ... · High risk => use 2PC/distributed transaction. @crichardson Agenda ACID is not an option Overview of sagas Coordinating

@crichardson

Guarantees:

state change ->

event is published

Page 83: Managing Data Consistency in a Microservice Architecture ... · High risk => use 2PC/distributed transaction. @crichardson Agenda ACID is not an option Overview of sagas Coordinating

@crichardson

Implementing choreography-based sagas is straightforward

Page 84: Managing Data Consistency in a Microservice Architecture ... · High risk => use 2PC/distributed transaction. @crichardson Agenda ACID is not an option Overview of sagas Coordinating

@crichardson

Preserves history of domain objects

Supports temporal queries

Built-in auditing

Page 85: Managing Data Consistency in a Microservice Architecture ... · High risk => use 2PC/distributed transaction. @crichardson Agenda ACID is not an option Overview of sagas Coordinating

@crichardson

Summary

Microservices tackle complexity and accelerate development

Database per service is essential for loose coupling

Use ACID transactions within services

Use orchestration-based or choreography-based sagas to maintain data consistency across services

Use countermeasures to reduce impact of anomalies caused by lack of isolation

Page 86: Managing Data Consistency in a Microservice Architecture ... · High risk => use 2PC/distributed transaction. @crichardson Agenda ACID is not an option Overview of sagas Coordinating

@crichardson

@crichardson [email protected]

http://learn.microservices.io

Questions?

40% discount with code

ctwsaturn18