Post on 17-Mar-2018
transcript
Chris Richardson
Founder of Eventuate.io Founder of the original CloudFoundry.com Author of POJOs in Action and Microservices Patterns
@crichardson chris@chrisrichardson.net http://learn.microservices.io
Events on the outside, on the inside and at the core
@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
messaging system
Inside the firewall: Messaging-based IPC
Sender Recipientmessage
Channel
AWS Kinesis
@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
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 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
Use Command Query Responsibility Segregation
Order Service
Customer Service
Message Broker
Customer *
Customer View
Service
Order *
findNewHighValueCustomers()
View database designed to
support query
@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: applications publish events
Order created
≪Aggregate≫ Order
Order Service
E-commerce application