Event Sourcing with Microservices

94
Event Sourcing and Microservices Why & How? Ralph Winzinger Taipei, April 2016

Transcript of Event Sourcing with Microservices

Page 1: Event Sourcing with Microservices

Event Sourcing and Microservices

Why & How?

Ralph Winzinger Taipei, April 2016

Page 2: Event Sourcing with Microservices

Senior Technical Leader

MicroservicesAgile

Architecture

Digital

RalphWinzinger

Cloud

Page 3: Event Sourcing with Microservices

Microservices• small - not only in terms of „lines of code“ but regarding

its functionality and the effort it takes to maintain

• self-contained, autonomous

• independent deployment unit

• fault-tolerant / resilient

• lightweight, having a small footprint

• exclusive access to its datastore

Page 4: Event Sourcing with Microservices

Microservices

• self contained

• fault-tolerant / resilient

• independent components

Page 5: Event Sourcing with Microservices

Microservices

• self contained

• fault-tolerant / resilient

• independent components

TransactionService

CustomerService

AccountService

Page 6: Event Sourcing with Microservices

Microservices

• self contained

• fault-tolerant / resilient

• independent components

TransactionService

CustomerService

AccountService

Page 7: Event Sourcing with Microservices

Microservices

• self contained ???

• fault-tolerant / resilient ???

• independent components ???

TransactionService

CustomerService

AccountService

Page 8: Event Sourcing with Microservices

Microservices

• self contained ???

• fault-tolerant / resilient ???

• independent components ???

TransactionService

CustomerService

AccountService

highly distributed systems!

Page 9: Event Sourcing with Microservices

Microservices

• self contained ???

• fault-tolerant / resilient ???

• independent components ???

TransactionService

CustomerService

AccountService

highly distributed systems!

Availability?

Page 10: Event Sourcing with Microservices

Microservices

• self contained ???

• fault-tolerant / resilient ???

• independent components ???

TransactionService

CustomerService

AccountService

highly distributed systems!

Availability?

Latency?

Page 11: Event Sourcing with Microservices

Availabilityfor eCommerce 24/7 systems

AV = Uptime / (Uptime + Downtime)

Page 12: Event Sourcing with Microservices

Availabilityfor eCommerce 24/7 systems

AV = Uptime / (Uptime + Downtime)

1 year = 8760 hours = 525600 minutes

Page 13: Event Sourcing with Microservices

Availabilityfor eCommerce 24/7 systems

AV = Uptime / (Uptime + Downtime)

1 year = 8760 hours = 525600 minutes

99% 88,5 hours downtime

Page 14: Event Sourcing with Microservices

Availabilityfor eCommerce 24/7 systems

AV = Uptime / (Uptime + Downtime)

1 year = 8760 hours = 525600 minutes

99,9% 8,8 hours downtime

Page 15: Event Sourcing with Microservices

Availabilityfor eCommerce 24/7 systems

AV = Uptime / (Uptime + Downtime)

1 year = 8760 hours = 525600 minutes

99,99% 53 minutes downtime

Page 16: Event Sourcing with Microservices

Availabilityfor eCommerce 24/7 systems

AV = Uptime / (Uptime + Downtime)

1 year = 8760 hours = 525600 minutes

99,999% 5 minutes downtime

Page 17: Event Sourcing with Microservices

Availabilityfor eCommerce 24/7 systems

AV = Uptime / (Uptime + Downtime)

1 year = 8760 hours = 525600 minutes

99,999% 5 minutes downtime

„five nines“high availability

Page 18: Event Sourcing with Microservices

Availabilityfor eCommerce 24/7 systems

AV = Uptime / (Uptime + Downtime) AS = AC(n-1) + ((1-AC(n-1))*ACn)

Page 19: Event Sourcing with Microservices

Availabilityfor eCommerce 24/7 systems

AV = Uptime / (Uptime + Downtime) AS = AC(n-1) + ((1-AC(n-1))*ACn)

S1

S2

S3

DB

FW

LB

Page 20: Event Sourcing with Microservices

Availabilityfor eCommerce 24/7 systems

AV = Uptime / (Uptime + Downtime) AS = AC(n-1) + ((1-AC(n-1))*ACn)

S1

S2

S3

DB

FW

LB

Page 21: Event Sourcing with Microservices

Availabilityfor eCommerce 24/7 systems

AV = Uptime / (Uptime + Downtime) AS = AC(n-1) + ((1-AC(n-1))*ACn)

S1

S2

S3

DB

99,5%

FW

LB 99,99%

99,99%

99,9%

99,9%

99,99%

Page 22: Event Sourcing with Microservices

Availabilityfor eCommerce 24/7 systems

AV = Uptime / (Uptime + Downtime) AS = AC(n-1) + ((1-AC(n-1))*ACn)

S1

S2

S3

DB

99,5%

FW

LB 99,99%

99,99%

99,9%

99,9%

99,99%

99,27% 64h downtime

Page 23: Event Sourcing with Microservices

Availabilityfor eCommerce 24/7 systems

AV = Uptime / (Uptime + Downtime) AS = AC(n-1) + ((1-AC(n-1))*ACn)

S1

S2

S3

DB

99,5%

FW

LB 99,99%

99,99%

99,9%

99,9%

99,99%

99,27% 64h downtimewe don’t want dependencies

& communication

Page 24: Event Sourcing with Microservices

LatencyThe more dependencies we have, the longer

our requests will take

Page 25: Event Sourcing with Microservices

LatencyThe more dependencies we have, the longer

our requests will take

S1

S2

S3

DB

FW

LB

Page 26: Event Sourcing with Microservices

LatencyThe more dependencies we have, the longer

our requests will take

S1

S2

S3

DB

FW

LB

Page 27: Event Sourcing with Microservices

LatencyThe more dependencies we have, the longer

our requests will take

S1

S2

S3

DB

100 msec

FW

LB 5 msec

5 msec

100 msec

100 msec

5 msec

Page 28: Event Sourcing with Microservices

LatencyThe more dependencies we have, the longer

our requests will take

S1

S2

S3

DB

100 msec

FW

LB 5 msec

5 msec

100 msec

100 msec

5 msec

315 ms

Page 29: Event Sourcing with Microservices

LatencyThe more dependencies we have, the longer

our requests will take

S1

S2

S3

DB

100 msec

FW

LB 5 msec

5 msec

100 msec

100 msec

5 msec

315 mswe don’t want dependencies

& communication

Page 30: Event Sourcing with Microservices

only data, not logicMicroservices contain all necessary

logic to process a request

Page 31: Event Sourcing with Microservices

only data, not logicMicroservices contain all necessary

logic to process a request

But maybe we need some additional data from other services, some „facts“

Page 32: Event Sourcing with Microservices

only data, not logic

• access the other service’ database

• keep data redundant

• low level database sync

• event sourcing

Microservices contain all necessary logic to process a request

But maybe we need some additional data from other services, some „facts“

Page 33: Event Sourcing with Microservices

only data, not logic

• access the other service’ database

• keep data redundant

• low level database sync

• event sourcing

Microservices contain all necessary logic to process a request

But maybe we need some additional data from other services, some „facts“

NO !

Page 34: Event Sourcing with Microservices

only data, not logic

• access the other service’ database

• keep data redundant

• low level database sync - also introduces coupling

• event sourcing

Microservices contain all necessary logic to process a request

But maybe we need some additional data from other services, some „facts“

NO !

Page 35: Event Sourcing with Microservices

only data, not logic

• access the other service’ database

• keep data redundant

• low level database sync - also introduces coupling

• event sourcing

Microservices contain all necessary logic to process a request

But maybe we need some additional data from other services, some „facts“

NO !

Page 36: Event Sourcing with Microservices

Domain Events• An important change in the state of our system.

Important with regard to business functionality.

• It happened in the past and it is immutable

• It is persistent

• The „sum“ of all events is the current system state

• It’s public for all services

Page 37: Event Sourcing with Microservices

Domain Events• An important change in the state of our system.

Important with regard to business functionality.

• It happened in the past and it is immutable

• It is persistent

• The „sum“ of all events is the current system state

• It’s public for all services

CustomerCreatedEvent

AccountOpenedEvent

TransactionProcessedEvent

CustomerAddressChangedEvent

Page 38: Event Sourcing with Microservices

Account BalanceMicroservices have exactly one thing to do.

They have one business functionality.

Page 39: Event Sourcing with Microservices

Account BalanceMicroservices have exactly one thing to do.

They have one business functionality.

Transactionsamount

Customer[accounts]

Accountbalance

Page 40: Event Sourcing with Microservices

Account BalanceThe balance depends on transactions.

Where can I find the current account balance?

Transactionsamount

Customer[accounts]

Accountbalance

Page 41: Event Sourcing with Microservices

Account BalanceThe balance depends on transactions.

Where can I find the current account balance?

• select balance from acc where accno=18773

Page 42: Event Sourcing with Microservices

Account BalanceLet’s assume we have a TransactionProcessedEvent

that is created each time, the TransactionService processed a transaction

Page 43: Event Sourcing with Microservices

Account BalanceLet’s assume we have a TransactionProcessedEvent

that is created each time, the TransactionService processed a transaction

TransactionService

DB

Online Banking

Mobile Banking

Page 44: Event Sourcing with Microservices

Account BalanceLet’s assume we have a TransactionProcessedEvent

that is created each time, the TransactionService processed a transaction

TransactionService

DB

Online Banking

Mobile Banking TransactionProcessedEvent

source = 18773 dest = 20770 amount = - 8.000 NTD

Page 45: Event Sourcing with Microservices

Account BalanceLet’s assume we have a TransactionProcessedEvent

that is created each time, the TransactionService processed a transaction

TransactionService

DB

Online Banking

Mobile Banking TransactionProcessedEvent

source = 18773 dest = 20770 amount = -8.000 NTDTransactionProcessedEvent

source = 170805 dest = 18773 amount = +1.500 NTD

Page 46: Event Sourcing with Microservices

Account BalanceThe balance depends on transactions.

Where can I find the current account balance?

• select balance from acc where accno=18773

• replay all events • use „empty“ / initial account • sum up all TransactionProcessedEvents

Page 47: Event Sourcing with Microservices

Account Balance

Accountbalance = 0

Page 48: Event Sourcing with Microservices

Account Balance

TPE (+120.000 NTD)

Accountbalance = 120.000

Page 49: Event Sourcing with Microservices

Account Balance

TPE (+120.000 NTD)

Accountbalance = 118.500

TPE (- 1.500 NTD)

Page 50: Event Sourcing with Microservices

Account Balance

TPE (+120.000 NTD)

Accountbalance = 110.500

TPE (- 1.500 NTD)

TPE (- 8.000 NTD)

Page 51: Event Sourcing with Microservices

Account Balance

TPE (+120.000 NTD)

Accountbalance = 127.500

TPE (- 1.500 NTD)

TPE (- 8.000 NTD)

TPE (+ 17.000 NTD)

Page 52: Event Sourcing with Microservices

Account Balance

TPE (+120.000 NTD)

Accountbalance = 677.500

TPE (- 1.500 NTD)

TPE (- 8.000 NTD)

TPE (+ 17.000 NTD)

TPE (+ 550.000 NTD)

Page 53: Event Sourcing with Microservices

Account Balance

TPE (+120.000 NTD)

Accountbalance = 652.500

TPE (- 1.500 NTD)

TPE (- 8.000 NTD)

TPE (+ 17.000 NTD)

TPE (+ 550.000 NTD)

TPE (- 25.000 NTD)

Page 54: Event Sourcing with Microservices

Account Balance

TransactionServiceAccountService

getTransactions()

Page 55: Event Sourcing with Microservices

Account Balance

TransactionServiceAccountService

getTransactions()

TPE

Page 56: Event Sourcing with Microservices

Account Balance

TransactionServiceAccountService

getTransactions()

TPE DB

ReadModel

Page 57: Event Sourcing with Microservices

Account Balance

TransactionServiceAccountService

getTransactions()

TPE DB

ReadModel

no more synchronous dependency!

Page 58: Event Sourcing with Microservices

!! Eventual Consistency !!

Page 59: Event Sourcing with Microservices

!! Eventual Consistency !!

TransactionServiceAccountService

TPE DB

ReadModel

Page 60: Event Sourcing with Microservices

!! Eventual Consistency !!

TransactionServiceAccountService

TPE DB

ReadModel t = 100 msec

Page 61: Event Sourcing with Microservices

!! Eventual Consistency !!

TransactionServiceAccountService

TPE DB

ReadModel t = 100 msec

The transaction was already processed but the AccountService still uses the old balance

Page 62: Event Sourcing with Microservices

!! Eventual Consistency !!

TransactionServiceAccountService

TPE DB

ReadModel t = 100 msec

The transaction was already processed but the AccountService still uses the old balance

Inconsistency! (but that’s how distribution works)

Page 63: Event Sourcing with Microservices

Technical Realization• Domain Events are very similar to messages

• publish / subscribe & fire forget

Page 64: Event Sourcing with Microservices

Technical Realization• Domain Events are very similar to messages

• publish / subscribe & fire forget

• Domain Events need a special semantic

• as a producer I want „fire & forget“

• as a consumer I want „exactly once“

Page 65: Event Sourcing with Microservices

Technical Realization• Domain Events are very similar to messages

• publish / subscribe & fire forget

• Domain Events need a special semantic

• as a producer I want „fire & forget“

• as a consumer I want „exactly once“

• even if a consumer is down for some time

• even if a consumer is late to the game

Page 66: Event Sourcing with Microservices

Technical Realization• Domain Events are very similar to messages

• publish / subscribe & fire forget

• Domain Events need a special semantic

• as a producer I want „fire & forget“

• as a consumer I want „exactly once“

• even if a consumer is down for some time

• even if a consumer is late to the game

multiple implementations available, e.g.:

Redis Persistent Queues

EventStore

Akka Persistence + MongoDB

Page 67: Event Sourcing with Microservices

Time Traveling

Page 68: Event Sourcing with Microservices

Time Traveling

AML Service

check limit

Page 69: Event Sourcing with Microservices

Time Traveling

AML Service

TPE

check limit

Page 70: Event Sourcing with Microservices

Time Traveling

AML Service

TPE

check limit suspicious

transactions

Page 71: Event Sourcing with Microservices

Time Traveling

AML Service

TPE

check limit suspicious

transactions

Analytics Alerting

Page 72: Event Sourcing with Microservices

Time TravelingAssume we will finish this service in two years.

How do we start it?

Page 73: Event Sourcing with Microservices

Time TravelingAssume we will finish this service in two years.

How do we start it?

A - start up & wait for new events

Page 74: Event Sourcing with Microservices

Time TravelingAssume we will finish this service in two years.

How do we start it?

A - start up & wait for new events

B - start up & process old events & wait for new events

Page 75: Event Sourcing with Microservices

Time TravelingAssume we will finish this service in two years.

How do we start it?

A - start up & wait for new events

B - start up & process old events & wait for new events

Now, we can apply new software (or changes) to the past!

bug fixes, new features, new analytics, … agile!

Page 76: Event Sourcing with Microservices

Time Traveling

AML Service

check limit suspicious

transactions

Analytics Alerting

TPE (+120.000 NTD)

TPE (- 1.500 NTD)

TPE (- 8.000 NTD)

TPE (+ 17.000 NTD)

TPE (+ 550.000 NTD)

TPE (- 25.000 NTD)

Page 77: Event Sourcing with Microservices

Time Traveling

AML Service

check limit suspicious

transactions

Analytics Alerting

TPE (+120.000 NTD)

TPE (- 1.500 NTD)

TPE (- 8.000 NTD)

TPE (+ 17.000 NTD)

TPE (+ 550.000 NTD)

TPE (- 25.000 NTD)

TPE (+ 550.000 NTD)

Page 78: Event Sourcing with Microservices

!! Race-Conditions !!We need „exactly once“!

Page 79: Event Sourcing with Microservices

!! Race-Conditions !!We need „exactly once“!

AccountService

TPE

Page 80: Event Sourcing with Microservices

!! Race-Conditions !!We need „exactly once“!

AccountService

TPE

Page 81: Event Sourcing with Microservices

!! Race-Conditions !!We need „exactly once“!

AccountService

AccountServiceAccountService

Account Service Cluster

TPE

Page 82: Event Sourcing with Microservices

!! Race-Conditions !!We need „exactly once“!

AccountService

AccountServiceAccountService

Account Service Cluster

TPE

Page 83: Event Sourcing with Microservices

!! Race-Conditions !!We need „exactly once“!

AccountService

AccountServiceAccountService

Account Service Cluster

TPE

coor

dina

tor

Page 84: Event Sourcing with Microservices

!! Race-Conditions !!We need „exactly once“!

AccountService

AccountServiceAccountService

Account Service Cluster

TPE

coor

dina

tor

multiple implementations available, e.g.:

Akka, …

Page 85: Event Sourcing with Microservices

What is the result?

Page 86: Event Sourcing with Microservices

What is the result?

Transactions

Customer

Account

Bank

AML

XYZ

Page 87: Event Sourcing with Microservices

What is the result?

Transactions

Customer

Account

Bank

AML

XYZ

lots of dependencies high latency & low availability

Page 88: Event Sourcing with Microservices

What is the result?

Transactions

Customer

Account

Bank

AML

XYZ

ReadModel

ReadModel

ReadModel

ReadModel

Page 89: Event Sourcing with Microservices

What is the result?

Transactions

Customer

Account

Bank

AML

XYZ

ReadModel

ReadModel

ReadModel

ReadModel

lots of independent services low latency & high availability

Page 90: Event Sourcing with Microservices

Sample Code

Page 91: Event Sourcing with Microservices

Sample Code

http://bit.ly/1RYRY6p

https://github.com/senacor/CUB-codesamples-1/tree/es-akka-eventstore

Page 92: Event Sourcing with Microservices

Event Sourcing …

… is not easy

But if you start thinking about this pattern, you will like how powerful it is

Page 93: Event Sourcing with Microservices

Any Questions …… on Event Sourcing?

… on my project at Cathay United Bank?

Right now or later - just as you want.

Page 94: Event Sourcing with Microservices

Any Questions …… on Event Sourcing?

… on my project at Cathay United Bank?

Right now or later - just as you want.