Events are very much on the edge of traditional applications, which use them as an application integration mechanism. The classic example is an ecommerce system. When a customer places an order, the order management application publishes an event, which triggers the fulfillment application to action. But today, microservices and DDD—which is a great foundation for microservices—are at the core of the application.
Events play an essential role in modern applications. Chris Richardson explains why events are a key application integration mechanism and how they are used by applications to communicate with the outside world. You’ll learn how the microservices inside an application use events to maintain data consistency and discover how to go one step further and make events an integral part of your domain logic.
OReilly SACON2018 - Events on the outside, on the inside, and at the core
1. 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
8. @crichardson
An application emits an event when…
OrderCreated
E-commerce
Application
OrderShipped InvoiceBecameOverdue
State changes Passage of time
9. @crichardson
Who consumes an event?
Notification
Service
Dashboard/
Monitoring
OrderCreated
Email
SMS
…
E-commerce
Application
Fulfilment
Application
14. @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
16. @crichardson
Webhooks = web friendly publish/
subscribe
Client Application
register(events, callbackUrl)
POST callbackUrl
POST callbackUrl
…
Low latency, more efficient, but what about past events?
19. @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
21. @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
25. @crichardson
Use Command Query Responsibility
Segregation
Order
Service
Customer
Service
Message Broker
Customer *
Customer
View
Service
Order *
findNewHighValueCustomers()
View database
designed to
support query
27. @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
28. @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(…)
29. @crichardson
Use event sourcing
Event 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
+