Opportunities & Pitfalls of Event-Driven Utopia @berndruecker - - PowerPoint PPT Presentation

opportunities pitfalls
SMART_READER_LITE
LIVE PREVIEW

Opportunities & Pitfalls of Event-Driven Utopia @berndruecker - - PowerPoint PPT Presentation

Opportunities & Pitfalls of Event-Driven Utopia @berndruecker Why this talk Why this talk Agenda Events on the outside 3 Events on the inside 1 Service 1 Service 2 Agenda Events on the outside 3 Events on the inside 1 Service 1


slide-1
SLIDE 1

Opportunities & Pitfalls

  • f

Event-Driven Utopia

@berndruecker

slide-2
SLIDE 2

Why this talk

slide-3
SLIDE 3

Why this talk

slide-4
SLIDE 4

Service 1

Agenda

Service 2

Events on the inside Events on the outside

1 3

slide-5
SLIDE 5

Service 1

Agenda

Service 2

Events on the inside Events on the outside

1 3

Events inside out

2

slide-6
SLIDE 6

Service 1

Agenda

Service 2

Events on the inside

1

Events on the outside

3

Events inside out

2

slide-7
SLIDE 7

Once upon a time…

RDMS Application Client

BBC architecture

(box - arrow – box – arrow - cylinder)

Every architecture diagram you'll ever need

slide-8
SLIDE 8

The great thing about this architecture

RDMS Application

DB gurantees (e.g. ACID)

slide-9
SLIDE 9

The problem

not webscale resiliency is expensive

RDMS Application RDMS

slide-10
SLIDE 10

Pat Helland

https://vimeo.com/52831373

slide-11
SLIDE 11

Append-only Log

bank account created +2,500 $ transfered

  • 14.99$

paid by credit card

Persistent change

RDMS

Account # Balance 12345 2,500$

Persistent state

Current Balance = 2,485.01 $

slide-12
SLIDE 12

Append-only Log

bank account created +2,500 $ transfered

  • 14.99$

paid by credit card

Persistent change

Event

Bank Account Created 2019/04/16 11:00 # 12345

Event

Money T ransfer Received 2,500$ 2019/04/16 11:00 # 12345

slide-13
SLIDE 13

Event Sourcing in a nutshell

Customer

Business Logic Save event

  • 2. Read events

Replay

Internal State

  • 3. Build

internal state

  • 5. Add Customer Created Event
  • 1. Create Customer
  • 4. Validation and

Invariant Checks e.g. Customer Created, Customer Credit Limit Approved, …

Customer Event Store

  • 6. Async publish

Customer Created Domain Event

slide-14
SLIDE 14

Working without distributed transactions

Customer

Business Logic Save event

  • 2. Read events

Replay

Internal State

  • 3. Build

internal state

  • 5. Add Customer Created Event
  • 1. Create Customer
  • 4. Validation and

Invariant Checks e.g. Customer Created, Customer Credit Limit Approved, …

Customer Event Store

  • 6. Async publish

Customer Created Domain Event

This is the only atomic operation required

slide-15
SLIDE 15

T raditional Architecture

Customer

  • 1. Create Customer

Account RDMS

Business Logic Open Account

  • 2. Persist state

changes

  • 3. Remote

Communication

slide-16
SLIDE 16

Pat Helland

Distributed Systems Guru Worked at Amazon, Microsoft & Salesforce @berndruecker

slide-17
SLIDE 17

Pat Helland

Grown-Ups Don’t Use Distributed T ransactions

Distributed Systems Guru Worked at Amazon, Microsoft & Salesforce

slide-18
SLIDE 18

Open Account

Outbox pattern in traditional architectures

Customer

  • 1. Create Customer

Account RDMS

Business Logic Job: „Open Account“

  • 2. Persist state

changes

  • 3. Remote

Communication

Execute: „Open Account“

TX 1 TX 2 Async after first transaction!

slide-19
SLIDE 19

Outbox pattern – Implementation Approaches

Scheduler Database T ransaction Log Workflow Automation

slide-20
SLIDE 20

Idempotency

Customer

  • 1. Create

Customer

Account RDMS

Business Logic Job: „Open Account“

  • 2. Persist state

changes

  • 3. Remote

Communication Async after transaction!

Execute: „Open Account“ Capture Request

TX 1 TX 2

slide-21
SLIDE 21

Idempotency

Customer

  • 1. Create

Customer

Account RDMS

Business Logic Job: „Open Account“

  • 2. Persist state

changes

  • 3. Remote

Communication Async after transaction!

Execute: „Open Account“ Capture Request

TX 1 TX 3 TX 2

slide-22
SLIDE 22

Working without distributed transactions

Customer

Business Logic Save event

  • 2. Read events

Replay

Internal State

  • 3. Build

internal state

  • 5. Add Customer Created Event
  • 1. Create Customer
  • 4. Validation and

Invariant Checks e.g. Customer Created, Customer Credit Limit Approved, …

Customer Event Store

  • 6. Async publish

Customer Created Domain Event

This is the only atomic operation required

slide-23
SLIDE 23

Events on the inside. An example from my world

slide-24
SLIDE 24

mail@berndruecker.io @berndruecker

Bernd Ruecker

Co-founder and Chief T echnologist of Camunda

slide-25
SLIDE 25

We offer two different workflow engines. Why? Camunda Zeebe

Persistent State Persistent change

slide-26
SLIDE 26

Workflow Instance Id Current Activity State 2 RetrievePayment running Workflow Instance Id Current Activity State 2 ShipGoods running Workflow Instance Id Current Activity State 2 OrderDelivered ended

2.

  • 3. UPDAT

E

  • 2. UPDAT

E

  • 1. INSERT

3. 1.

RDMS Workflow Engine

slide-27
SLIDE 27

Append-only Log

1.

create workflow instance workflow instance created start event

  • ccured

sequence flow taken activity activated task created lock created task locked complete task task completed activity completed sequence flow taken …

2. 2. 1.

Workflow Engine

slide-28
SLIDE 28

Event Handling, Replication & Single Writer

Follower Follower

complete task command task completed event 1

send

2

append command

Broker (Leader)

Stream Processor 4

process

7

store & replicate event

6

append event

3

store & replicate command

5

respond Single Writer (single thread)

slide-29
SLIDE 29

What we do different

Follower Follower

complete task command task completed event 1

send

2

append command

Leader

Stream Processor 4

process

7

store & replicate event

6

append event

3

store & replicate command

5

respond Single Writer (single thread)

Store and replay commands Delete records that are fully processed Persist & replicate internal state

slide-30
SLIDE 30

Consistency Availability Partition

slide-31
SLIDE 31

Zeebe is CP

Follower Follower

complete task command task completed event 1

send

2

append command

Leader

Stream Processor 4

process

7

store & replicate event

6

append event

3

store & replicate command

5

respond Single Writer (single thread)

slide-32
SLIDE 32

Horizontal scalability by partitioning

Partition 1 Partition 2 Partition 3 Partition 4

Every workflow instance is exactly handled by one partition

instance id: 2-42 instance id: 3-66

Stream Processor Single Writer (single thread)

slide-33
SLIDE 33

Queries and read models

Zeebe Broker Zeebe Broker

Streaming Exporter ask ask

slide-34
SLIDE 34

Recap 1 – Events on the inside # Natural mechanism to build scalable services in distributed systems (with Outbox & co included) But # You have to think about reads, queries & eventual consistency # Much less industry experience available

@berndruecker

slide-35
SLIDE 35

Service 1

Agenda

Service 2

Events on the inside Events on the outside

1 3

Events inside out

2

slide-36
SLIDE 36

Event Store and Messaging

Customer

  • 1. Create Customer

Customer Event Store

slide-37
SLIDE 37

Merge Messaging and Event Store

Customer

  • 1. Create Customer

Customer Event Store

slide-38
SLIDE 38

Merge messaging and event store

Customer

  • 1. Create Customer

Shared Event Store

slide-39
SLIDE 39

Enter the world of Kafka…

slide-40
SLIDE 40

Merge messaging and event store

Customer

  • 1. Create Customer

Shared Event Store

slide-41
SLIDE 41

Kafka as transport

Customer

  • 1. Create Customer

Used as queue (but persistent!)

slide-42
SLIDE 42

Service 1

Agenda

Service 2

Events on the inside Events on the outside

1 3

Events inside out

2

slide-43
SLIDE 43

Once upon a time

Billing Customer

Change Address

slide-44
SLIDE 44

Event Notification

Address changed

Billing Customer

slide-45
SLIDE 45

Event Notification

Address changed

Billing Customer Billing Customer

Reverse direction

  • f dependency

What‘s direction

  • f dependency

Change Address

slide-46
SLIDE 46

Event Notification

Address changed

Billing Customer

AdressChanged { customerId: 42 } Ask for details

slide-47
SLIDE 47

Event-carried State T ransfer

Address changed

Billing Customer

AddressChanged { customerId: 42, address: ... } CustomerChanged { customerId: 42, status: A, address: ..., } AddressChanged { customerId: 42,

  • ldAddress: ...

newAddress: ... } CustomerMoved { ..., }

slide-48
SLIDE 48

This decision is complex

Address changed

Billing Customer Billing Customer

Reverse direction

  • f dependency

What‘s direction

  • f dependency

Change Address

slide-49
SLIDE 49

Example

Change Address Address Submit

From bla@company.com Date 2019-04-23 09.05 To confirm your address change please click on this link: http://company.com/confirm?id=82e97d49-166c-4862- 9973-4db348e6225d

Incoming Email

slide-50
SLIDE 50

Example

Customer Notification

Address change confirmed Change Adress Address change requested

http://company.com/confirm?id=82e97 d49-166c-4862-9973-4db348e6225d

direction of dependency

slide-51
SLIDE 51

Example

Customer Notification

‚Confirmation‘ approved Change Adress

http://company.com/confirm?id=82e97 d49-166c-4862-9973-4db348e6225d

Send mail ‚Confirmation‘ Address changed

direction of dependency

slide-52
SLIDE 52

Challenge: Command vs. Event

Event Command

vs

slide-53
SLIDE 53

It is NOT about communication protocols

Address changed

Billing Customer Billing Customer

Change Address

It can be messaging, REST, whatever, ….

slide-54
SLIDE 54

Manifold ways of transport

slide-55
SLIDE 55

Manifold ways of transport

slide-56
SLIDE 56

?

Event Command Query Message Record Event

Fact, happened in the past, immutable Intend, Want s.th. to happen, The intention itself is a fact

slide-57
SLIDE 57

?

Event Command Query Message Record Event

slide-58
SLIDE 58

Commands in disguise

The Customer Needs T

  • Be

Sent A Message T

  • Confirm

Address Change Event Send Message

Wording of Sender Wording of recipient

slide-59
SLIDE 59

Examples

Payment Order Subscription

Retrieve Payment More general, does not need to know who is retrieving payments

Customer Order

Address Changed

Billing

More general, does not need to care about who is interessted in address changes

Notification

Send Mail Global service

Order Notification

Order Placed Payment Received Goods Shipped Service that can handle notifications for orders autonomously

slide-60
SLIDE 60

Distributed Monoliths

Authorization Service Document Context Page Context

Document attached Page created Document moved Page moved

slide-61
SLIDE 61

Define stable contract/API instead

Authorization Service Document Context Page Context

Add auth

slide-62
SLIDE 62

Next challenge: Event chains

slide-63
SLIDE 63

Event Chains

Adress Check Credit Check Registration @berndruecker Customer

Event Bus

Registration requested Credit checked Address checked Customer registered

slide-64
SLIDE 64

Event Chains

Adress Check Credit Check Registration @berndruecker Customer

Event Bus

Registration requested Credit checked Address checked Customer registered

How does customer registration work?

slide-65
SLIDE 65

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

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

slide-66
SLIDE 66

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

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

slide-67
SLIDE 67

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

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

slide-68
SLIDE 68

Monitoring Workflows Across Microservices

https://www.infoq.com/articles/monitor-workflow-collaborating-microservices @berndruecker

slide-69
SLIDE 69

T ypical approaches

Distributed T racing Data Lake / Event Monitoring Process Mining Process T racking @berndruecker

slide-70
SLIDE 70

What we currently build with customers…

Camunda Optimize

Elastic Registration requested Credit checked Address checked Customer registered @berndruecker

slide-71
SLIDE 71

All great – until you have to move…

slide-72
SLIDE 72

Changes required for an additional check

Adress Check Credit Check Registration Criminal Check @berndruecker Customer

Event Bus

Registration requested Credit checked Customer registered Address checked

slide-73
SLIDE 73

Changes required for an additional check

Adress Check Credit Check Registration Criminal Check @berndruecker Customer

Event Bus

Registration requested Credit checked Customer registered Address checked Criminal checked

slide-74
SLIDE 74

Alternative flow

Adress Check Credit Check Registration Criminal Check @berndruecker Customer

Kafka

Customer registered Registration requested Address checked Credit checked Criminal checked

slide-75
SLIDE 75

Alternative flow

Adress Check Credit Check Registration Criminal Check @berndruecker Customer

Kafka

Customer registered Registration requested Address checked Credit checked Criminal checked

„Credit checks got more expensive, do that only if all

  • ther checks succeed“
slide-76
SLIDE 76

Keep it stable, just move sticks with yellow color to the top. How hard can it be?

slide-77
SLIDE 77

What we wanted

Photo by Lijian Zhang, available under Creative Commons SA 2.0 License and Pedobear19 / CC BY-SA 4.0 @berndruecker

slide-78
SLIDE 78

Orchestration

Adress Check Credit Check Registration Customer

Kafka

Registration requested Credit checked Address checked Customer registered Check credit Check address Customer checked Customer On-boarding

Of course these two services could be merged

slide-79
SLIDE 79

Changes

Adress Check Credit Check Registration Customer

Kafka

Registration requested Credit checked Address checked Customer registered Check credit Check address Customer checked Customer On-boarding Criminal

Check

Crimes checked Check crimes

slide-80
SLIDE 80

Comparison

2 2 change nges criminal check can be deployed first 2 2 change nges, criminal check can be deployed first See also https://www.infoworld.com/article/3391592/ how-to-tame-event-driven-microservices.html

slide-81
SLIDE 81

In my world…

Customer On-boarding

Leverage Workflow Engine & BPMN within Service

slide-82
SLIDE 82

Customer On-boarding

Local Orchestration

Central Orchestration Service

slide-83
SLIDE 83
slide-84
SLIDE 84

Recap 2 # Commands vs. Events: Decide about the direction of dependencies # Beware of event-chains and avoid losing sight # Balance choreography and orchestration

@berndruecker

slide-85
SLIDE 85

Service 2 Service 1

Recap

Events on the inside Events on the outside

1 3

Persistent state vs persistent change Event sourcing & Event Store Consistency & CAP Read Models & CQRS Events as API Event vs Command Event chains & visibility Orchestration vs Choreography Shared Event Store

Events inside out

2

slide-86
SLIDE 86

Want to see code?

slide-87
SLIDE 87

Nothing for the faint of heart…

Events on the inside Events on the outside

slide-88
SLIDE 88

Nothing for the faint of heart…

…but doable… …and worth it

slide-89
SLIDE 89

Thank you!

@berndruecker

slide-90
SLIDE 90

mail@berndruecker.io @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/