SlideShare a Scribd company logo
@crichardson
Handling Eventual Consistency in
JVM Microservices with Event
Sourcing
Chris Richardson
Founder of Eventuate.io
@crichardson
http://eventuate.io
Copyright © 2015. Chris Richardson Consulting, Inc. All rights reserved
Kenny Bastani
Spring Developer Advocate
@kennybastani
http://pivotal.io
@crichardson
Presentation goal
Show how event sourcing is a great
foundation for a microservice
architecture
@crichardson
About Chris
http://learnmicroservices.io
@crichardson
About Kenny
@kennybastani
@crichardson
Agenda
Overview of event sourcing
The problem with microservices, transactions and queries
Using events to maintain consistency
Event sourcing in a microservice architecture
Implementing queries with CQRS
@crichardson
Traditional persistence
Order
id
state
….
101 ACCEPTED
Order table
…
ID STATE …
102 … …
@crichardson
But how did we get here?
Who did what and when?
What was the state of the
Order last Monday at 3:01pm?
@crichardson
Order example
History
@crichardson
Task example State
History
Audit
@crichardson
Usually
auditing, history and temporal
queries
is additional code and/or
an after-thought
@crichardson
Event sourcing
For each domain object (i.e. DDD aggregate):
Identify (state changing) domain events, e.g. use Event
Storming
Define Event classes
For example, Order events: OrderCreated, OrderCancelled,
OrderApproved, OrderRejected, OrderShipped
@crichardson
Persists events NOT current state
Event table
Entity type
Event
id
Entity
id
Event
data
Order 902101 …OrderApproved
Order 903101 …OrderShipped
Event
type
Order 901101 …OrderCreated
@crichardson
Replay events to recreate
state
Order
state
OrderCreated(…)
OrderAccepted(…)
OrderShipped(…)
Events
Periodically snapshot to avoid loading all events
@crichardson
Benefits of event sourcing
Reifies state changes:
Built in, reliable audit log
temporal queries
Preserved history More easily implement future requirements
Eliminates O/R mapping problem (mostly)
Reliable event publishing: publishes events needed by
predictive analytics etc, user notifications,…
@crichardson
Drawbacks of event sourcing
Requires application rewrite
Weird and unfamiliar style of programming
Events live forever carefully evolve schema
Querying the event store can be challenging
Current state is no longer directly available
Often need to maintain views for efficiency
@crichardson
Demo
@crichardson
Agenda
Overview of event sourcing
The problem with microservices, transactions and queries
Using events to maintain consistency
Event sourcing in a microservice architecture
Implementing queries with CQRS
@crichardson
The Microservice architecture
tackles complexity through
modularization
@crichardson
Microservice architecture
Browser
Mobile
Device
Store
Front UI
API
Gateway
Catalog
Service
Review
Service
Order
Service
…
Service
Catalog
Database
Review
Database
Order
Database
…
Database
HTML
REST
REST
Apply X-axis and Z-axis
scaling to each service
independently
@crichardson
But there are challenges
implementing
transactions and queries
@crichardson
ACID transactions cannot be
used
BEGIN TRANSACTION
…
SELECT ORDER_TOTAL
FROM ORDERS WHERE CUSTOMER_ID = ?
…
SELECT CREDIT_LIMIT
FROM CUSTOMERS WHERE CUSTOMER_ID = ?
…
INSERT INTO ORDERS …
…
COMMIT TRANSACTION
Private to the
Order Service
Private to the
Customer Service
Requires 2PC
@crichardson
2PC is not a viable option
Guarantees consistency
BUT
2PC is best avoided
Not supported by many NoSQL databases etc.
CAP theorem 2PC impacts availability
….
@crichardson
Queries can’t use joins
SELECT *
FROM CUSTOMER c, ORDER o
WHERE
c.id = o.ID
AND o.ORDER_TOTAL > 100000
AND o.STATE = 'SHIPPED'
AND c.CREATION_DATE > ?
Customer
Service
Order Service
@crichardson
Agenda
Overview of event sourcing
The problem with microservices, transactions and queries
Using events to maintain consistency
Event sourcing in a microservice architecture
Implementing queries with CQRS
@crichardson
Event-driven architecture
@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 = APPROVED
Eventually consistent credit checking
Message Bus
createOrder()
Publishes:
Subscribes to:
Subscribes to:
publishes:
OrderCreatedEvent
CreditReservedEvent
OrderCreatedEvent CreditReservedEvent
@crichardson
How atomically update
database and publish an event
Order Service
Order
Database
Message Broker
insert Order
publish
OrderCreatedEvent
dual write problem
?
@crichardson
Failure = inconsistent system
Order Service
Order
Database
Message Broker
insert Order
publish
OrderCreatedEvent
X
@crichardson
2PC is not an option
@crichardson
How to reliably publish
events when state changes?
@crichardson
Agenda
Overview of event sourcing
The problem with microservices, transactions and queries
Using events to maintain consistency
Event sourcing in a microservice architecture
Implementing queries with CQRS
@crichardson
Event sourcing = event-centric
persistence
Application
Database
Event store
update
publish
X
@crichardson
Event Store
Application architecture
Order 123 Customer 456
OrderCreated
OrderApproved
…
CustomerCreated
CustomerCreditReserved
…
CreateOrder
UpdateOrder
GetOrder
Subscribe
Order
Service
CreateCustomer
UpdateCustomer
GetCustomer
Subscribe
Customer
Service
@crichardson
Request handling in an event sourced application
HTTP
Handler
Event
Store
pastEvents = findEvents(entityId)
Order
new()
applyEvents(pastEvents)
newEvents = processCmd(someCmd)
saveEvents(newEvents) (optimistic locking)
Order Service
applyEvents(newEvents)
@crichardson
Event Store publishes events
consumed by other services
Event
Store
Event
Subscriber
subscribe(EventTypes)
publish(event)
publish(event)
Customer
update()
Customer Service
@crichardson
Event Store publishes events
consumed by other services
Event
Store
Event
Subscriber
subscribe(EventTypes)
publish(event)
publish(event)
CQRS View
update()
Service Xyz
send notifications
…
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
@crichardson
Agenda
Overview of event sourcing
The problem with microservices, transactions and queries
Using events to maintain consistency
Event sourcing in a microservice architecture
Implementing queries with CQRS
@crichardson
Find recent, valuable
customers
SELECT *
FROM CUSTOMER c, ORDER o
WHERE
c.id = o.ID
AND o.ORDER_TOTAL > 100000
AND o.STATE = 'SHIPPED'
AND c.CREATION_DATE > ?
Customer
Service
Order Service
What if event
sourcing is
used?…. is no longer easy
@crichardson
Use Command Query
Responsibility Segregation
(CQRS)
@crichardson
Command Query Responsibility
Segregation (CQRS)
Application logic
Commands Queries
X
POST
PUT
DELETE
GET
@crichardson
Command Query Responsibility
Segregation (CQRS)
Command side
Commands
Event Sourcing
domain
objects
Event Store
Events
Query side
Queries
(Materialized)
View
Events
POST
PUT
DELETE
GET
@crichardson
Query side design
Event Store
Updater
View Updater
Service
Events
Reader
HTTP GET
Request
View Query
Service
View
Store
e.g.
MongoDB
ElasticSearch
Neo4J
update query
@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
@crichardson
Persisting customers and order info
using Spring Data for MongoDB...
@crichardson
Persisting customers and order
using Spring Data for MongoDB...
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
@crichardson
Demo
@crichardson
Summary
Microservice architecture functionally decomposes an
application into services
Transactions and queries resist decomposition
Use an event-driven architecture based on event sourcing to
maintain data consistency
Implement queries using CQRS
@crichardson
chris@chrisrichardson.net kbastani@pivotal.io
http://learnmicroservices.io http://pivotal.io
Questions?

More Related Content

PDF
Developing microservices with aggregates (melbourne)
PDF
Microservices in Java and Scala (sfscala)
PDF
Omnikron webbinar - Microservices: enabling the rapid, frequent, and reliable...
PDF
Microservices + Events + Docker = A Perfect Trio (dockercon)
PDF
JavaOne2017: ACID Is So Yesterday: Maintaining Data Consistency with Sagas
PDF
#hacksummit 2016 - event-driven microservices – Events on the outside, on the...
PDF
Gluecon: Using sagas to maintain data consistency in a microservice architecture
PDF
QCONSF - ACID Is So Yesterday: Maintaining Data Consistency with Sagas
Developing microservices with aggregates (melbourne)
Microservices in Java and Scala (sfscala)
Omnikron webbinar - Microservices: enabling the rapid, frequent, and reliable...
Microservices + Events + Docker = A Perfect Trio (dockercon)
JavaOne2017: ACID Is So Yesterday: Maintaining Data Consistency with Sagas
#hacksummit 2016 - event-driven microservices – Events on the outside, on the...
Gluecon: Using sagas to maintain data consistency in a microservice architecture
QCONSF - ACID Is So Yesterday: Maintaining Data Consistency with Sagas

What's hot (20)

PDF
Microservices and Redis #redisconf Keynote
PDF
Spring Days NYC - A pattern language for microservices
PDF
There is no such thing as a microservice! (oracle code nyc)
PDF
Events on the outside, on the inside and at the core (jfokus jfokus2016)
PDF
Developing event-driven microservices with event sourcing and CQRS (london Ja...
PDF
Developing microservices with aggregates (devnexus2017)
PDF
Building and deploying microservices with event sourcing, CQRS and Docker (Be...
PDF
A Pattern Language for Microservices (@futurestack)
PDF
A Pattern Language for Microservices
PDF
ArchSummit Shenzhen - Using sagas to maintain data consistency in a microserv...
PDF
Kong Summit 2018 - Microservices: decomposing applications for testability an...
PDF
OReilly SACON2018 - Events on the outside, on the inside, and at the core
PDF
NodeJS: the good parts? A skeptic’s view (jax jax2013)
PDF
Developing Event-driven Microservices with Event Sourcing & CQRS (gotoams)
PDF
A pattern language for microservices (#gluecon #gluecon2016)
PDF
SVCC Developing Asynchronous, Message-Driven Microservices
PDF
Oracle Code One: Events and commands: developing asynchronous microservices
PDF
#JaxLondon keynote: Developing applications with a microservice architecture
PDF
Events to the rescue: solving distributed data problems in a microservice arc...
PDF
QConPlus 2021: Minimizing Design Time Coupling in a Microservice Architecture
Microservices and Redis #redisconf Keynote
Spring Days NYC - A pattern language for microservices
There is no such thing as a microservice! (oracle code nyc)
Events on the outside, on the inside and at the core (jfokus jfokus2016)
Developing event-driven microservices with event sourcing and CQRS (london Ja...
Developing microservices with aggregates (devnexus2017)
Building and deploying microservices with event sourcing, CQRS and Docker (Be...
A Pattern Language for Microservices (@futurestack)
A Pattern Language for Microservices
ArchSummit Shenzhen - Using sagas to maintain data consistency in a microserv...
Kong Summit 2018 - Microservices: decomposing applications for testability an...
OReilly SACON2018 - Events on the outside, on the inside, and at the core
NodeJS: the good parts? A skeptic’s view (jax jax2013)
Developing Event-driven Microservices with Event Sourcing & CQRS (gotoams)
A pattern language for microservices (#gluecon #gluecon2016)
SVCC Developing Asynchronous, Message-Driven Microservices
Oracle Code One: Events and commands: developing asynchronous microservices
#JaxLondon keynote: Developing applications with a microservice architecture
Events to the rescue: solving distributed data problems in a microservice arc...
QConPlus 2021: Minimizing Design Time Coupling in a Microservice Architecture
Ad

Viewers also liked (7)

PDF
Developing microservices with aggregates (SpringOne platform, #s1p)
PPTX
Wix.com Back-end Engineering Guild Manifesto
PPTX
Scaling wix.com to 100 million users
PPTX
Microservices - it's déjà vu all over again
PDF
Building reliable systems from unreliable components
PPTX
Scaling Wix engineering
PPTX
Scaling wix with microservices architecture devoxx London 2015
Developing microservices with aggregates (SpringOne platform, #s1p)
Wix.com Back-end Engineering Guild Manifesto
Scaling wix.com to 100 million users
Microservices - it's déjà vu all over again
Building reliable systems from unreliable components
Scaling Wix engineering
Scaling wix with microservices architecture devoxx London 2015
Ad

Similar to Handling Eventual Consistency in JVM Microservices with Event Sourcing (javaone 2016) (20)

PDF
Developing event-driven microservices with event sourcing and CQRS (Shanghai)
PDF
Solving distributed data management problems in a microservice architecture (...
PDF
Building and deploying microservices with event sourcing, CQRS and Docker (Me...
PDF
Building microservices with Scala, functional domain models and Spring Boot (...
PDF
Developing functional domain models with event sourcing (sbtb, sbtb2015)
PDF
Microservices + Events + Docker = A Perfect Trio by Docker Captain Chris Rich...
PDF
Building and deploying microservices with event sourcing, CQRS and Docker (Ha...
PDF
Building and deploying microservices with event sourcing, CQRS and Docker (QC...
PDF
Developing event-driven microservices with event sourcing and CQRS (svcc, sv...
PDF
Developing event-driven microservices with event sourcing and CQRS (phillyete)
PDF
Building microservices with Scala, functional domain models and Spring Boot
PDF
YOW2018 - Events and Commands: Developing Asynchronous Microservices
PDF
Saturn 2018: Managing data consistency in a microservice architecture using S...
PDF
Mucon: Not Just Events: Developing Asynchronous Microservices
PDF
Building Microservices with Scala, functional domain models and Spring Boot -...
PDF
#JaxLondon: Building microservices with Scala, functional domain models and S...
PDF
An overview of the Eventuate Platform
PDF
GotoChgo 2019: Not Just Events: Developing Asynchronous Microservices
PDF
Events on the outside, on the inside and at the core - Chris Richardson
PDF
Events on the outside, on the inside and at the core (jaxlondon)
Developing event-driven microservices with event sourcing and CQRS (Shanghai)
Solving distributed data management problems in a microservice architecture (...
Building and deploying microservices with event sourcing, CQRS and Docker (Me...
Building microservices with Scala, functional domain models and Spring Boot (...
Developing functional domain models with event sourcing (sbtb, sbtb2015)
Microservices + Events + Docker = A Perfect Trio by Docker Captain Chris Rich...
Building and deploying microservices with event sourcing, CQRS and Docker (Ha...
Building and deploying microservices with event sourcing, CQRS and Docker (QC...
Developing event-driven microservices with event sourcing and CQRS (svcc, sv...
Developing event-driven microservices with event sourcing and CQRS (phillyete)
Building microservices with Scala, functional domain models and Spring Boot
YOW2018 - Events and Commands: Developing Asynchronous Microservices
Saturn 2018: Managing data consistency in a microservice architecture using S...
Mucon: Not Just Events: Developing Asynchronous Microservices
Building Microservices with Scala, functional domain models and Spring Boot -...
#JaxLondon: Building microservices with Scala, functional domain models and S...
An overview of the Eventuate Platform
GotoChgo 2019: Not Just Events: Developing Asynchronous Microservices
Events on the outside, on the inside and at the core - Chris Richardson
Events on the outside, on the inside and at the core (jaxlondon)

More from Chris Richardson (20)

PDF
The microservice architecture: what, why, when and how?
PDF
More the merrier: a microservices anti-pattern
PDF
YOW London - Considering Migrating a Monolith to Microservices? A Dark Energy...
PDF
Dark Energy, Dark Matter and the Microservices Patterns?!
PDF
Dark energy, dark matter and microservice architecture collaboration patterns
PDF
Scenarios_and_Architecture_SkillsMatter_April_2022.pdf
PDF
Using patterns and pattern languages to make better architectural decisions
PDF
iSAQB gathering 2021 keynote - Architectural patterns for rapid, reliable, fr...
PDF
A pattern language for microservices - June 2021
PDF
Mucon 2021 - Dark energy, dark matter: imperfect metaphors for designing micr...
PDF
Designing loosely coupled services
PDF
Microservices - an architecture that enables DevOps (T Systems DevOps day)
PDF
DDD SoCal: Decompose your monolith: Ten principles for refactoring a monolith...
PDF
Decompose your monolith: Six principles for refactoring a monolith to microse...
PDF
TDC2020 - The microservice architecture: enabling rapid, reliable, frequent a...
PDF
Overview of the Eventuate Tram Customers and Orders application
PDF
#DevNexus202 Decompose your monolith
PDF
JFokus: Cubes, Hexagons, Triangles, and More: Understanding Microservices
PDF
Decompose your monolith: strategies for migrating to microservices (Tide)
PDF
Oracle CodeOne 2019: Descending the Testing Pyramid: Effective Testing Strate...
The microservice architecture: what, why, when and how?
More the merrier: a microservices anti-pattern
YOW London - Considering Migrating a Monolith to Microservices? A Dark Energy...
Dark Energy, Dark Matter and the Microservices Patterns?!
Dark energy, dark matter and microservice architecture collaboration patterns
Scenarios_and_Architecture_SkillsMatter_April_2022.pdf
Using patterns and pattern languages to make better architectural decisions
iSAQB gathering 2021 keynote - Architectural patterns for rapid, reliable, fr...
A pattern language for microservices - June 2021
Mucon 2021 - Dark energy, dark matter: imperfect metaphors for designing micr...
Designing loosely coupled services
Microservices - an architecture that enables DevOps (T Systems DevOps day)
DDD SoCal: Decompose your monolith: Ten principles for refactoring a monolith...
Decompose your monolith: Six principles for refactoring a monolith to microse...
TDC2020 - The microservice architecture: enabling rapid, reliable, frequent a...
Overview of the Eventuate Tram Customers and Orders application
#DevNexus202 Decompose your monolith
JFokus: Cubes, Hexagons, Triangles, and More: Understanding Microservices
Decompose your monolith: strategies for migrating to microservices (Tide)
Oracle CodeOne 2019: Descending the Testing Pyramid: Effective Testing Strate...

Recently uploaded (20)

PDF
Raksha Bandhan Grocery Pricing Trends in India 2025.pdf
PDF
How to Choose the Right IT Partner for Your Business in Malaysia
PPTX
L1 - Introduction to python Backend.pptx
PPTX
Oracle E-Business Suite: A Comprehensive Guide for Modern Enterprises
PPTX
ISO 45001 Occupational Health and Safety Management System
PDF
PTS Company Brochure 2025 (1).pdf.......
PPT
Introduction Database Management System for Course Database
PDF
Upgrade and Innovation Strategies for SAP ERP Customers
PDF
System and Network Administraation Chapter 3
PDF
System and Network Administration Chapter 2
PDF
Flood Susceptibility Mapping Using Image-Based 2D-CNN Deep Learnin. Overview ...
PDF
medical staffing services at VALiNTRY
PPTX
Transform Your Business with a Software ERP System
PPTX
ManageIQ - Sprint 268 Review - Slide Deck
PDF
Internet Downloader Manager (IDM) Crack 6.42 Build 41
PDF
Digital Strategies for Manufacturing Companies
PDF
Claude Code: Everyone is a 10x Developer - A Comprehensive AI-Powered CLI Tool
PPTX
Operating system designcfffgfgggggggvggggggggg
PDF
Which alternative to Crystal Reports is best for small or large businesses.pdf
PDF
Internet Downloader Manager (IDM) Crack 6.42 Build 42 Updates Latest 2025
Raksha Bandhan Grocery Pricing Trends in India 2025.pdf
How to Choose the Right IT Partner for Your Business in Malaysia
L1 - Introduction to python Backend.pptx
Oracle E-Business Suite: A Comprehensive Guide for Modern Enterprises
ISO 45001 Occupational Health and Safety Management System
PTS Company Brochure 2025 (1).pdf.......
Introduction Database Management System for Course Database
Upgrade and Innovation Strategies for SAP ERP Customers
System and Network Administraation Chapter 3
System and Network Administration Chapter 2
Flood Susceptibility Mapping Using Image-Based 2D-CNN Deep Learnin. Overview ...
medical staffing services at VALiNTRY
Transform Your Business with a Software ERP System
ManageIQ - Sprint 268 Review - Slide Deck
Internet Downloader Manager (IDM) Crack 6.42 Build 41
Digital Strategies for Manufacturing Companies
Claude Code: Everyone is a 10x Developer - A Comprehensive AI-Powered CLI Tool
Operating system designcfffgfgggggggvggggggggg
Which alternative to Crystal Reports is best for small or large businesses.pdf
Internet Downloader Manager (IDM) Crack 6.42 Build 42 Updates Latest 2025

Handling Eventual Consistency in JVM Microservices with Event Sourcing (javaone 2016)