34
Chris Richardson Founder of Eventuate.io Founder of the original CloudFoundry.com Author of POJOs in Action and Microservices Patterns @crichardson [email protected] http://learn.microservices.io Events on the outside, on the inside and at the core

OReilly SACON2018 - Events on the outside, on the inside, and at the core

Embed Size (px)

Citation preview

Chris Richardson

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

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

Events on the outside, on the inside and at the core

@crichardson

About Chris

http://learn.microservices.io ctwsoftarchconf18

@crichardson

What’s an event?

@crichardson

http://www.merriam-webster.com/dictionary/event

@crichardson

Examples of events

@crichardson

Events play a role at every level of an architecture

@crichardson

Events on the outside E-commerce Application

Fulfilment Application

@crichardson

An application emits an event when…

OrderCreated

E-commerce Application

OrderShipped InvoiceBecameOverdue

State changes Passage of time

@crichardson

Who consumes an event?

Notification Service

Dashboard/ Monitoring

OrderCreated Email SMS …

E-commerce Application

Fulfilment Application

@crichardson

Implements the Open for extension/Closed for

modification principle for applications

Legacy Application New Feature

@crichardson

How to transport events?

@crichardson

messaging system

Inside the firewall: Messaging-based IPC

Sender Recipientmessage

Channel

AWS Kinesis

@crichardson

Outside the firewall

@crichardson

Polling for events

HTTP Periodically poll for events

Atom Publishing Protocol (AtomPub) Based on HTTP Head is constantly changing Tail is immutable and can be efficiently cached

High-latency, inefficient

@crichardson

Using WebSockets

Browser

Web Socket

STOMP

Client application

Service

Web Socket

STOMP

ApplicationSUBSCRIBE

MESSAGE

MESSAGE

Low latency, more efficient, but what about past events?

@crichardson

Webhooks = web friendly publish/subscribe

Client Application

register(events, callbackUrl)

POST callbackUrl

POST callbackUrl

Low latency, more efficient, but what about past events?

@crichardson

Events on the inside

E-commerce application

Order Service

Catalog Service

@crichardson

The Microservice architecture enables the rapid, safe delivery of

large, complex applications

@crichardson

E-commerce application - refactored to services

Order Service

Catalog Service

API

API

Customer Service

API

…. Service

API

But what about transactions and queries that span

services?

Private DBs

@crichardson

How to implement transactions that span services?

@crichardson

How to enforce the customer’s credit limit?

Customer

creditLimit ...

has ordersbelongs toOrder

total

Invariant: sum(open order.total) <= customer.creditLimit

?placeOrder()

Order Service

updateCreditLimit()

Customer Service

No 2PC

@crichardson

Use event-driven, choreography-based sagas

Customer

creditLimit creditReservations ...

Order

total

placeOrder()Order Service Customer Service

create()

Order created

Credit Reserved

reserveCredit()

approve()

@crichardson

How to implement queries that span services?

@crichardson

Find new customers who have placed high value orders that have shipped

@crichardson

Use Command Query Responsibility Segregation

Order Service

Customer Service

Message Broker

Customer *

Customer View

Service

Order *

findNewHighValueCustomers()

View database designed to

support query

@crichardson

Events at the core

OrderService

Order created≪aggregate≫

Order

@crichardson

DDD: Aggregates publish domain events

Order

OrderLine Item

quantity productId productName productPrice

customerId

Address

street city …

Aggregate

Order created

Domain event

Publishes

when created

or state

changes

@crichardson

Business logic explicitly invoking event publishing API

≪service≫ OrderService

≪aggregate≫ Order

DomainEvent Publisher

placeOrder(…)

new(…)

≪event≫ OrderCreated

publish(…)

new(…)

• Simple • Easy to adapt existing code

But • Events are not automatic • Tricky to publish messages transactionally

publish(…)

@crichardson

Use event sourcingEvent table

Entity type Event id

Entity id

Event data

Order 902101 …OrderApproved

Order 903101 …OrderShipped

Event type

Order 901101 …OrderCreated

≪aggregate≫ Order

List<Event> process(CreateOrderCommand)void apply (OrderCreatedEvent)

List<Event> process(CancelOrderCommand)void apply (OrderCanceledEvent)

Reliable publishing, aggregate history, auditing, …. YET radically different

Event-based persistence Event-based business logic

+

@crichardson

Summary

Events play a role at every level of an architecture

Order created

@crichardson

Summary: aggregates publish events

Order created≪aggregate≫

Order

@crichardson

Summary: services publish events

Order created≪Aggregate≫ Order

Order Service

@crichardson

Summary: applications publish events

Order created

≪Aggregate≫ Order

Order Service

E-commerce application

@crichardson

@crichardson [email protected]

http://learn.microservices.io

Questions?ctwsoftarchconf18