pragmatic event-driven microservices - goto conference · 2018-05-07 · pragmatic event-driven...

56
Pragmatic Event-Driven Microservices Allard Buijze – @allardbz – [email protected]

Upload: others

Post on 22-May-2020

4 views

Category:

Documents


0 download

TRANSCRIPT

Page 1: Pragmatic Event-Driven Microservices - GOTO Conference · 2018-05-07 · Pragmatic Event-Driven Microservices Allard Buijze –@allardbz –allard@axoniq.io. Service Service Service

Pragmatic Event-Driven MicroservicesAllard Buijze – @allardbz – [email protected]

Page 2: Pragmatic Event-Driven Microservices - GOTO Conference · 2018-05-07 · Pragmatic Event-Driven Microservices Allard Buijze –@allardbz –allard@axoniq.io. Service Service Service

Service

Service

Service

Page 3: Pragmatic Event-Driven Microservices - GOTO Conference · 2018-05-07 · Pragmatic Event-Driven Microservices Allard Buijze –@allardbz –allard@axoniq.io. Service Service Service

‘Normal’ SQL QUERY

22 JOINS 6 SUBQUERIES

Page 4: Pragmatic Event-Driven Microservices - GOTO Conference · 2018-05-07 · Pragmatic Event-Driven Microservices Allard Buijze –@allardbz –allard@axoniq.io. Service Service Service

Layered architectureUser Interface

Service Layer

Data Access Layer

Do

main

Mod

el

Method invocation Cache

Worker pools

Web

Cache

Session replication

Distributed 2nd level cache Query Cache

Page 5: Pragmatic Event-Driven Microservices - GOTO Conference · 2018-05-07 · Pragmatic Event-Driven Microservices Allard Buijze –@allardbz –allard@axoniq.io. Service Service Service
Page 6: Pragmatic Event-Driven Microservices - GOTO Conference · 2018-05-07 · Pragmatic Event-Driven Microservices Allard Buijze –@allardbz –allard@axoniq.io. Service Service Service
Page 7: Pragmatic Event-Driven Microservices - GOTO Conference · 2018-05-07 · Pragmatic Event-Driven Microservices Allard Buijze –@allardbz –allard@axoniq.io. Service Service Service

Source: http://www.sabisabi.com/images/DungBeetle-on-dung.JPG

Page 8: Pragmatic Event-Driven Microservices - GOTO Conference · 2018-05-07 · Pragmatic Event-Driven Microservices Allard Buijze –@allardbz –allard@axoniq.io. Service Service Service

AxonFramework

Page 9: Pragmatic Event-Driven Microservices - GOTO Conference · 2018-05-07 · Pragmatic Event-Driven Microservices Allard Buijze –@allardbz –allard@axoniq.io. Service Service Service

Command Query Responsibility Segregation

Command

modelProjections

Client

Events

Page 10: Pragmatic Event-Driven Microservices - GOTO Conference · 2018-05-07 · Pragmatic Event-Driven Microservices Allard Buijze –@allardbz –allard@axoniq.io. Service Service Service

Monoliths

St Breock Downs Monolith - www.cornwalls.co.uk

Page 11: Pragmatic Event-Driven Microservices - GOTO Conference · 2018-05-07 · Pragmatic Event-Driven Microservices Allard Buijze –@allardbz –allard@axoniq.io. Service Service Service

Microservices vs Monoliths

Microservices system

Almost all the cases where I've heard of a system that was built as a

microservice system from scratch, it has ended up in serious trouble.

Monoliths

Almost all the successful microservice stories have started with a

monolith that got too big and was broken up

Martin Fowler

Source: http://martinfowler.com/bliki/MonolithFirst.html

Page 12: Pragmatic Event-Driven Microservices - GOTO Conference · 2018-05-07 · Pragmatic Event-Driven Microservices Allard Buijze –@allardbz –allard@axoniq.io. Service Service Service

Are you tall enough?

Source: martinfowler.com/bliki/MicroservicePrerequisites.html

Page 13: Pragmatic Event-Driven Microservices - GOTO Conference · 2018-05-07 · Pragmatic Event-Driven Microservices Allard Buijze –@allardbz –allard@axoniq.io. Service Service Service

Noun Driven Design

Noun? Service!

Page 14: Pragmatic Event-Driven Microservices - GOTO Conference · 2018-05-07 · Pragmatic Event-Driven Microservices Allard Buijze –@allardbz –allard@axoniq.io. Service Service Service

Noun Driven Design

OrderService

Page 15: Pragmatic Event-Driven Microservices - GOTO Conference · 2018-05-07 · Pragmatic Event-Driven Microservices Allard Buijze –@allardbz –allard@axoniq.io. Service Service Service

Noun Driven Design

CustomerService

Page 16: Pragmatic Event-Driven Microservices - GOTO Conference · 2018-05-07 · Pragmatic Event-Driven Microservices Allard Buijze –@allardbz –allard@axoniq.io. Service Service Service

Noun Driven Design

ProductService

Page 17: Pragmatic Event-Driven Microservices - GOTO Conference · 2018-05-07 · Pragmatic Event-Driven Microservices Allard Buijze –@allardbz –allard@axoniq.io. Service Service Service

Noun Driven Design

InventoryService

Page 18: Pragmatic Event-Driven Microservices - GOTO Conference · 2018-05-07 · Pragmatic Event-Driven Microservices Allard Buijze –@allardbz –allard@axoniq.io. Service Service Service

Noun Driven Design

Page 19: Pragmatic Event-Driven Microservices - GOTO Conference · 2018-05-07 · Pragmatic Event-Driven Microservices Allard Buijze –@allardbz –allard@axoniq.io. Service Service Service

$

Page 20: Pragmatic Event-Driven Microservices - GOTO Conference · 2018-05-07 · Pragmatic Event-Driven Microservices Allard Buijze –@allardbz –allard@axoniq.io. Service Service Service

Location transparency

A Component should not be aware, nor make any

assumptions, of the location of Components it

interacts with

A component should neither be aware of nor make any

assumptions about the location of components it interacts with.

Location transparency starts with good API design (but doesn’t end there)

Page 21: Pragmatic Event-Driven Microservices - GOTO Conference · 2018-05-07 · Pragmatic Event-Driven Microservices Allard Buijze –@allardbz –allard@axoniq.io. Service Service Service

Reasons to send a message• Event Something has happened

Page 22: Pragmatic Event-Driven Microservices - GOTO Conference · 2018-05-07 · Pragmatic Event-Driven Microservices Allard Buijze –@allardbz –allard@axoniq.io. Service Service Service

‘Event-Driven’ Microservices

Need to know

ordered itemsOrder service

OrderCreated

ItemAdded

ItemRemoved

OrderConfirmed

Page 23: Pragmatic Event-Driven Microservices - GOTO Conference · 2018-05-07 · Pragmatic Event-Driven Microservices Allard Buijze –@allardbz –allard@axoniq.io. Service Service Service

Reasons to send a message• Event Something has happened

• Command I want the system to do something

• Query I want to know something

Page 24: Pragmatic Event-Driven Microservices - GOTO Conference · 2018-05-07 · Pragmatic Event-Driven Microservices Allard Buijze –@allardbz –allard@axoniq.io. Service Service Service

Something has happened - Event• Data change

• Deadline passing

• Or anything else that’s relevant in the domain

Page 25: Pragmatic Event-Driven Microservices - GOTO Conference · 2018-05-07 · Pragmatic Event-Driven Microservices Allard Buijze –@allardbz –allard@axoniq.io. Service Service Service

Want to know

Want to know

Want to knowSomething

happened!

Publish-subscribe

Page 26: Pragmatic Event-Driven Microservices - GOTO Conference · 2018-05-07 · Pragmatic Event-Driven Microservices Allard Buijze –@allardbz –allard@axoniq.io. Service Service Service

One of us

wants to know

Want to know

One of us

wants to know

Something

happened!

Exclusive consumers

Page 27: Pragmatic Event-Driven Microservices - GOTO Conference · 2018-05-07 · Pragmatic Event-Driven Microservices Allard Buijze –@allardbz –allard@axoniq.io. Service Service Service

One of us

wants to know

Want to know

One of us

wants to know

Something

happened!

Competing consumers

Page 28: Pragmatic Event-Driven Microservices - GOTO Conference · 2018-05-07 · Pragmatic Event-Driven Microservices Allard Buijze –@allardbz –allard@axoniq.io. Service Service Service

One of us

wants to know

Want to know

One of us

wants to know

Something

happened!

Balanced consumers

If (%)

Page 29: Pragmatic Event-Driven Microservices - GOTO Conference · 2018-05-07 · Pragmatic Event-Driven Microservices Allard Buijze –@allardbz –allard@axoniq.io. Service Service Service

I want something done -Command• Request-a-side-effect

• Change data / application state

• Send email

• Exactly 1 destination

• OK / NOK reply

• Maybe some data

Page 30: Pragmatic Event-Driven Microservices - GOTO Conference · 2018-05-07 · Pragmatic Event-Driven Microservices Allard Buijze –@allardbz –allard@axoniq.io. Service Service Service

Can do

something

Can do

something

Can do

somethingDo something!

Command Routing

Page 31: Pragmatic Event-Driven Microservices - GOTO Conference · 2018-05-07 · Pragmatic Event-Driven Microservices Allard Buijze –@allardbz –allard@axoniq.io. Service Service Service

Can do

something

Can do

something

Can do

somethingDo something!

Command Routing

if (%)

Page 32: Pragmatic Event-Driven Microservices - GOTO Conference · 2018-05-07 · Pragmatic Event-Driven Microservices Allard Buijze –@allardbz –allard@axoniq.io. Service Service Service

I want to know something -Query• Desire for information

• The response has more value than the question

• (Usually) side-effect free

• Different messaging patterns• Single destination

• Scatter – gather query

• Subscription

Page 33: Pragmatic Event-Driven Microservices - GOTO Conference · 2018-05-07 · Pragmatic Event-Driven Microservices Allard Buijze –@allardbz –allard@axoniq.io. Service Service Service

Price = 49Result = Price

Query – point to point

Page 34: Pragmatic Event-Driven Microservices - GOTO Conference · 2018-05-07 · Pragmatic Event-Driven Microservices Allard Buijze –@allardbz –allard@axoniq.io. Service Service Service

Price = 199

If (…)

Price = 149

Query – scatter-gather

Result = Min(Price)

(wait 100ms)

Page 35: Pragmatic Event-Driven Microservices - GOTO Conference · 2018-05-07 · Pragmatic Event-Driven Microservices Allard Buijze –@allardbz –allard@axoniq.io. Service Service Service

Price = 199

If (…)

Price = 149

If (…)

Price = 99

Query – scatter-gather

Result = Min(Price)

(wait 100ms)

Page 36: Pragmatic Event-Driven Microservices - GOTO Conference · 2018-05-07 · Pragmatic Event-Driven Microservices Allard Buijze –@allardbz –allard@axoniq.io. Service Service Service

Query – subscription

Price = 49Result = Price +

sum(Δ)Price = 39

Δ = -10

Page 37: Pragmatic Event-Driven Microservices - GOTO Conference · 2018-05-07 · Pragmatic Event-Driven Microservices Allard Buijze –@allardbz –allard@axoniq.io. Service Service Service

OrderConfirmed

‘Event-Driven’ Microservices

Need to know

ordered itemsOrder service

ItemAdded

ItemRemoved

OrderConfirmed

OrderCreated

GetOrderDetails

OrderDetails

Page 38: Pragmatic Event-Driven Microservices - GOTO Conference · 2018-05-07 · Pragmatic Event-Driven Microservices Allard Buijze –@allardbz –allard@axoniq.io. Service Service Service

Events retain valueEvent Sourcing is an Architectural pattern in which Events are considered the “source of truth”, based on which components (re)build their internal state.

Page 39: Pragmatic Event-Driven Microservices - GOTO Conference · 2018-05-07 · Pragmatic Event-Driven Microservices Allard Buijze –@allardbz –allard@axoniq.io. Service Service Service

Event StoreAn Event Store stores the published events to be retrieved both

by consumers as well as the publishing component itself.

Page 40: Pragmatic Event-Driven Microservices - GOTO Conference · 2018-05-07 · Pragmatic Event-Driven Microservices Allard Buijze –@allardbz –allard@axoniq.io. Service Service Service

OrderConfirmed

Event Sourcing

Some smart

analyticsOrder service

ItemRemoved

OrderCreated

OrderDetails

Page 41: Pragmatic Event-Driven Microservices - GOTO Conference · 2018-05-07 · Pragmatic Event-Driven Microservices Allard Buijze –@allardbz –allard@axoniq.io. Service Service Service

Event Store operations• Append

• Validate ‘sequence’

Page 42: Pragmatic Event-Driven Microservices - GOTO Conference · 2018-05-07 · Pragmatic Event-Driven Microservices Allard Buijze –@allardbz –allard@axoniq.io. Service Service Service

Event Store operations• Full sequential read

Page 43: Pragmatic Event-Driven Microservices - GOTO Conference · 2018-05-07 · Pragmatic Event-Driven Microservices Allard Buijze –@allardbz –allard@axoniq.io. Service Service Service

Event Store operations• Read aggregate’s events

Page 44: Pragmatic Event-Driven Microservices - GOTO Conference · 2018-05-07 · Pragmatic Event-Driven Microservices Allard Buijze –@allardbz –allard@axoniq.io. Service Service Service

1. Define which routing patterns to apply

2. Choose technology/protocol accordingly

Page 45: Pragmatic Event-Driven Microservices - GOTO Conference · 2018-05-07 · Pragmatic Event-Driven Microservices Allard Buijze –@allardbz –allard@axoniq.io. Service Service Service

At scale, different rules apply

Page 46: Pragmatic Event-Driven Microservices - GOTO Conference · 2018-05-07 · Pragmatic Event-Driven Microservices Allard Buijze –@allardbz –allard@axoniq.io. Service Service Service

How do you route all these events to all components?

How will this scale?

Page 47: Pragmatic Event-Driven Microservices - GOTO Conference · 2018-05-07 · Pragmatic Event-Driven Microservices Allard Buijze –@allardbz –allard@axoniq.io. Service Service Service

You Don't!

It Won't!

Page 48: Pragmatic Event-Driven Microservices - GOTO Conference · 2018-05-07 · Pragmatic Event-Driven Microservices Allard Buijze –@allardbz –allard@axoniq.io. Service Service Service

Unmanageable messOrder Created

Item Added to Order

Shipping Address Added

Billing Address Added

Order Confirmed

As shipping

module, I want to

know when an

order is placed

Page 49: Pragmatic Event-Driven Microservices - GOTO Conference · 2018-05-07 · Pragmatic Event-Driven Microservices Allard Buijze –@allardbz –allard@axoniq.io. Service Service Service

Bounded contextExplicitly define the context within which a model applies. Explicitly set boundaries in terms of team organization, usage within specific parts of the application, and physical manifestations such as code bases and database schemas. Keep the model strictly consistent within these bounds, but don’t be distracted or confused by issues outside.

Page 50: Pragmatic Event-Driven Microservices - GOTO Conference · 2018-05-07 · Pragmatic Event-Driven Microservices Allard Buijze –@allardbz –allard@axoniq.io. Service Service Service

Within a context, share ‘everything’

Page 51: Pragmatic Event-Driven Microservices - GOTO Conference · 2018-05-07 · Pragmatic Event-Driven Microservices Allard Buijze –@allardbz –allard@axoniq.io. Service Service Service

Between contexts, share ‘consciously’

As shipping

module, I want to

know when an

order is placed

Order Created

Item Added to Order

Shipping Address Added

Billing Address Added

Order Confirmed

Order Created

+ Item Added

+ Order Confirmed

Order Placed

Page 52: Pragmatic Event-Driven Microservices - GOTO Conference · 2018-05-07 · Pragmatic Event-Driven Microservices Allard Buijze –@allardbz –allard@axoniq.io. Service Service Service

Between contexts, share ‘consciously’

As shipping

module, I want to

know when an

order is placed

Order Created

Item Added to Order

Shipping Address Added

Billing Address Added

Order Confirmed

Order Confirmed

+ Get Order Details

Order Placed

Page 53: Pragmatic Event-Driven Microservices - GOTO Conference · 2018-05-07 · Pragmatic Event-Driven Microservices Allard Buijze –@allardbz –allard@axoniq.io. Service Service Service

Where does AxonFramework fit?• Inside each component in bounded context

• Axon provides the Java APIs towards platform

• EventBus, CommandBus, QueryBus

• Separation of business logic and infrastructure logic

Page 54: Pragmatic Event-Driven Microservices - GOTO Conference · 2018-05-07 · Pragmatic Event-Driven Microservices Allard Buijze –@allardbz –allard@axoniq.io. Service Service Service

Microservices Messaging

Page 55: Pragmatic Event-Driven Microservices - GOTO Conference · 2018-05-07 · Pragmatic Event-Driven Microservices Allard Buijze –@allardbz –allard@axoniq.io. Service Service Service

“Just enough” intelligence

dumb smart

Message Broker

Sends messages. Main

value add is reliability.

Enterprise Service Bus

Understands message

content. Hard to configure

and maintain.

AxonHub

Understands difference between Commands,

Events, Queries and their routing patterns.

Does not care about the content of these

messages.

Page 56: Pragmatic Event-Driven Microservices - GOTO Conference · 2018-05-07 · Pragmatic Event-Driven Microservices Allard Buijze –@allardbz –allard@axoniq.io. Service Service Service

Our mission

Provide the APIs and implementations necessary for event-driven microservices to cooperate harmoniously, allowing each of them to focus on the business logic.