Developing event-driven microservices with event sourcing and CQRS (svcc, svcc2015)
- 10. @crichardson
Today: use a microservice, polyglot
architecture
Shopping UI
Orders Service Customer Service
Order
Database Customer Database
Standalone
services
Sharded SQLNoSQL DB
- 13. @crichardson
Customer management
How to maintain invariants?
Order management
Order Service
placeOrder()
Customer Service
updateCreditLimit()
Customer
creditLimit
...
has ordersbelongs toOrder
total
Invariant:
sum(open order.total) <= customer.creditLimit
?
- 15. @crichardson
….Use an event-driven
architecture
Services publish events when something important happens,
e.g. state changes
Services subscribe to events and update their state
Maintain eventual consistency across multiple aggregates
(in multiple datastores)
Synchronize replicated data
- 16. @crichardson
Order Management
Order
id : 4567
total: 343
state = CREATED
Customer Management
Customer
creditLimit : 12000
creditReservations: {}
Customer
creditLimit : 12000
creditReservations: { 4567 -> 343}
Order
id : 4567
total: 343
state = OPEN
Eventually consistent credit checking
Message Bus
createOrder()
Publishes:
Subscribes to:
Subscribes to:
publishes:
OrderCreatedEvent
CreditReservedEvent
OrderCreatedEvent CreditReservedEvent
- 19. @crichardson
Problem #2: How atomicity update
database and publish an event
Order Service
Order
Database
Message Broker
insert Order
publish
OrderCreatedEvent
dual write problem
?
- 20. @crichardson
Update and publish using
2PC
Guaranteed atomicity BUT
Need a distributed transaction manager
Database and message broker must support 2PC
Impacts reliability
Not fashionable
2PC is best avoided
- 21. @crichardson
Transaction log tailing
How:
Read the database “transaction log” = single source of truth
Publish events to message broker
LinkedIn databus https://github.com/linkedin/databus
Supports Oracle and MySQL
Publish as events
AWS DynamoDB streams
Ordered sequence of creates, updates, deletes made to a DynamoDB table
Last 24 hours
Subscribe to get changes
MongoDB
Read the oplog
- 22. Transaction log tailing: benefits
and drawbacks
Benefits
No 2PC
No application changes
required
Guaranteed to be
accurate
Drawbacks
Immature
Database specific
solutions
Low-level DB changes
rather business level
events = need to reverse
engineer domain events
- 24. Database triggers: benefits
and drawbacks
Benefits
No 2PC
No application changes
required
Drawbacks
Requires the database to
support them
Database specific solutions
Low-level DB changes rather
business level events = need
to reverse engineer domain
events
Error-prone, e.g. missing
trigger
- 25. @crichardson
Application created events
Use datastore as a message queue
Txn #1: Update database: new entity state & event
Txn #2: Consume event
Txn #3: Mark event as consumed
Eventually consistent mechanism (used by eBay)
See BASE: An Acid Alternative, http://bit.ly/ebaybase
- 29. @crichardson
Event sourcing
For each aggregate (business entity):
Identify (state-changing) domain events
Define Event classes
For example,
ShoppingCart: ItemAddedEvent, ItemRemovedEvent,
OrderPlacedEvent
Order: OrderCreated, OrderCancelled, OrderApproved,
OrderRejected, OrderShipped
- 31. @crichardson
Replay events to recreate
state
Order
state
OrderCreated(…)
OrderAccepted(…)
OrderShipped(…)
Events
Periodically snapshot to avoid loading all events
- 35. @crichardson
Request handling in an event-sourced application
HTTP
Handler
Event
Store
pastEvents = findEvents(entityId)
Order
new()
applyEvents(pastEvents)
newEvents = processCmd(SomeCmd)
saveEvents(newEvents)
Microservice A
(optimistic locking)
- 36. @crichardson
Event Store publishes events -
consumed by other services
Event
Store
Event
Subscriber
subscribe(EventTypes)
publish(event)
publish(event)
Aggregate
CQRS View
update()
update()
Microservice B
send notifications
…
- 37. Event store = database + message
broker
Hybrid database and
message broker
Implementations:
Home-grown/DIY
geteventstore.com by
Greg Young
http://eventuate.io
(mine)
Event Store
Save
aggregate
events
Get
aggregate
events
Subscribe
to events
- 38. @crichardson
Benefits of event sourcing
Solves data consistency issues in a Microservice/NoSQL-based
architecture
Reliable event publishing: publishes events needed by predictive
analytics etc, user notifications,…
Eliminates O/R mapping problem (mostly)
Reifies state changes:
Built-in, reliable audit log,
temporal queries
Preserved history More easily implement future requirements
- 39. @crichardson
Drawbacks of event sourcing
Weird and unfamiliar
Events = a historical record of your bad design decisions
Handling duplicate events can be tricky
Application must handle eventually consistent data
Event store only directly supports PK-based lookup => use
Command Query Responsibility Segregation (CQRS) to handle
queries
- 42. @crichardson
Implement requirements and preserve
invariants
As a customer
I want to place an order
So that I get the needed products
Given that my available credit is $1500
When I place a $250 order
Then the order is created
Then my available credit is $1250
Story
Scenario
Post
conditions
Pre
conditions
Given that my available credit is $1500
When I place a $2500 order
Then the order is rejected
Then my available credit is $1500
Scenario
Invariant:
sum(open order.total) <= customer.creditLimit
- 45. @crichardson
… becomes eventually
consistent (BASE)
Updating multiple aggregates
multi-step, event-driven flow
each step updates one Aggregate
Service creates saga to coordinate workflow
A state machine
Part of the domain, e.g. Order aggregate OR Synthetic
aggregate
Post-conditions and invariants eventually become true
- 50. Aggregate design
Graph consisting of a root
entity and one or more other
entities and value objects
Each core business entity =
Aggregate: e.g. customer,
Account, Order, Product, ….
Reference other aggregate
roots via primary key
Often contains partial copy
of other aggregates’ data
Order
OrderLine
Item
quantity
productId
productName
productPrice
customerId
Address
street
city
…
- 52. @crichardson
Transaction = processing one
command by one aggregate
No opportunity to update multiple aggregates within a
transaction
Aggregate granularity is important
If an update must be atomic (i.e. no compensating
transaction) then it must be handled by a single aggregate
e.g. scanning boarding pass at security checkpoint or when
entering jetway
- 54. Designing domain events
Record state changes for an
aggregate
Part of the public API of the domain
model ProductAddedToCart
id : TimeUUID
productId
productName
productPrice
shoppingCartId
Required by
aggregate
Enrichment:
Required by by
consumers
- 59. @crichardson
OO = State + Behavior
creditLimit
creditReservations : Map[OrderId, Money]
Customer
List<Event> processCommand (
Command aCommand) { … }
void applyEvent (Event anEvent) { … }
State
Behavior
- 66. @crichardson
Event handling in Customers
1.Load Customer aggregate
2.Processes command
3.Applies events
4.Persists events
Triggers BeanPostProcessor
Durable subscription name
- 73. @crichardson
Persisting a customer and
order history in MongoDB
{
"_id" : "0000014f9a45004b-0a00270000000000",
"_class" : "net.chrisrichardson…..views.orderhistory.CustomerView",
"version" : NumberLong(5),
"orders" : {
"0000014f9a450063-0a00270000000000" : {
"state" : "APPROVED",
"orderId" : "0000014f9a450063-0a00270000000000",
"orderTotal" : {
"amount" : "1234"
}
},
"0000014f9a450063-0a00270000000001" : {
"state" : "REJECTED",
"orderId" : "0000014f9a450063-0a00270000000001",
"orderTotal" : {
"amount" : "3000"
}
}
},
"name" : "Fred",
"creditLimit" : {
"amount" : "2000"
}
}
Denormalized = efficient lookup
- 76. Other kinds of views
AWS Cloud Search
Text search as-a-Service
View updater batches
aggregates to index
View query service does
text search
AWS DynamoDB
NoSQL as-a-Service
On-demand scalable -
specify desired read/write
capacity
Document and key-value
data models
Useful for denormalized,
UI oriented views
- 77. Benefits and drawbacks of
CQRS
Benefits
Necessary in an event-sourced
architecture
Separation of concerns =
simpler command and query
models
Supports multiple denormalized
views
Improved scalability and
performance
Drawbacks
Complexity
Potential code duplication
Replication lag/eventually
consistent views
- 82. @crichardson
Bounded context = microservices
Customer
management
domain
model
Order management
domain
model
Catalog
management
domain
model
…
domain
model
Customer view
Command side Query side
- 85. @crichardson
When to use microservices?
In the beginning:
•You don’t need it
•It will slow you down
Later on:
•You need it
•Refactoring dependencies
is painful
- 91. @crichardson
Summary
Event sourcing solves key data consistency issues with:
Microservices
Partitioned SQL/NoSQL databases
Apply strategic DDD to identify microservices
Apply tactical DDD to design individual services
Use CQRS to implement materialized views for queries