Building and deploying microservices with event sourcing, CQRS and Docker (Berlin microxchg, Munich microservices meetup)
This document discusses building and deploying microservices using event sourcing, CQRS and Docker. It covers an overview of event sourcing and how it solves data consistency issues in microservice architectures. CQRS is used to implement materialized views for queries. Spring Boot is recommended for building microservices and Docker is used to package the microservices. A continuous integration pipeline is used to build, test, publish Docker images and deploy updates.
Introduction to building and deploying microservices using event sourcing, CQRS, and Docker, presented by Chris Richardson.
Discusses traditional application architecture vs microservices, highlighting limitations of monolithic architecture and advantages of using microservices.
Explores the use of sharded relational and NoSQL databases to overcome limitations of single databases, with focus on managing distributed data.
Explains event-based architectures for maintaining consistency across microservices, discussing specific examples and the need for eventual consistency.
Details event sourcing principles including identifying domain events and persisting them, emphasizing atomic operations over state updates.
Describes handling commands and events in event-sourced applications, introducing aggregation and the concept of snapshots to improve efficiency.
Introduction to the Event Store API functionality essential for implementing event sourcing in microservices and various implementations.
Analyzes the business and technical benefits of event sourcing, while also addressing the challenges and complexities involved.
Discusses the architecture of microservices using CQRS for segregating command and query responsibilities, illustrating data persistence.
Overview of building, testing, and deploying microservices with Docker; emphasizes CI/CD using Jenkins pipelines and deployment strategies.
Summarizes the key concepts of event sourcing in microservices, including the integration of CQRS and Docker.
Building and deploying microservices with event sourcing, CQRS and Docker (Berlin microxchg, Munich microservices meetup)
1.
@crichardson
Building and deploying
microserviceswith event
sourcing, CQRS and Docker
Chris Richardson
Author of POJOs in Action
Founder of the original CloudFoundry.com
@crichardson
[email protected]
http://plainoldobjects.com
http://microservices.io
2.
@crichardson
Presentation goal
Share myexperiences with building and
deploying an application using Scala, functional
domain models, microservices, event sourcing,
CQRS, and Docker
@crichardson
About Chris
Founder ofa buzzword compliant (stealthy, social, mobile, big
data, machine learning, ...) startup
Consultant helping organizations improve how they architect
and deploy applications using cloud, micro services, polyglot
applications, NoSQL, ...
Creator of http://microservices.io
@crichardson
Agenda
Why build event-drivenmicroservices?
Overview of event sourcing
Designing microservices with event sourcing
Implementing queries in an event sourced application
Building and deploying microservices
@crichardson
Limitations of themonolithic
architecture
Intimidates developers
Obstacle to frequent deployments
Overloads your IDE and container
Obstacle to scaling development
Modules having conflicting scaling requirements
Requires long-term commitment to a technology stack
9.
@crichardson
Apply the scalecube
X axis
- horizontal duplication
Z
axis
-data
partitioning
Y axis -
functional
decomposition
Scale
by
splitting
sim
ilar
things
Scale by
splitting
different things
10.
@crichardson
Use a microservicearchitecture
Banking UI
Account Management
Service
MoneyTransfer
Management Service
Account
Database
MoneyTransfer
Database
Standalone
services
11.
@crichardson
Limitations of asingle
relational database
Scalability
Distribution
Schema updates
O/R impedance mismatch
Handling semi-structured data
@crichardson
Use NoSQL databases
Avoidsthe limitations of RDBMS
For example,
text search Solr/Cloud Search
social (graph) data Neo4J
highly distributed/available database Cassandra
...
@crichardson
Example #1 -SQL + Text
Search engine
Application
MySQL ElasticSearch
How to maintain consistency without 2PC?
Product #1 Product #1
17.
@crichardson
Example #2 -Cassandra main
table <=> index table
Application
Cassandra
How to maintain consistency without 2PC?
Main Table
Denormalized
view
Index Table
18.
@crichardson
Example #3: Moneytransfer
Account Management
Service
MoneyTransfer
Management Service
Account
Database
MoneyTransfer
Database
Account #2
Account #1 Money Transfer
How to maintain consistency without 2PC?
19.
@crichardson
Event-based architecture to
therescue
Components (e.g. services) publish events when state
changes
Components subscribe to events
Maintains eventual consistency across multiple aggregates
(in multiple datastores)
Synchronize replicated data
20.
@crichardson
Event-driven synchronization:
SQL +Text Search engine
Catalog Service
MySQL ElasticSearch
Product #1 Product #1
Search Service
Message Bus
Insert
Product
Created
Product
Created
Index Doc
create product
21.
@crichardson
MoneyTransferService
MoneyTransfer
fromAccountId = 101
toAccountId= 202
amount = 55
state = INITIAL
MoneyTransfer
fromAccountId = 101
toAccountId = 202
amount = 55
state = DEBITED
MoneyTransfer
fromAccountId = 101
toAccountId = 202
amount = 55
state = COMPLETED
Eventually consistent money transfer
Message Bus
AccountService
transferMoney()
Publishes:
Subscribes to:
Subscribes to:
publishes:
MoneyTransferCreatedEvent
AccountDebitedEvent
DebitRecordedEvent
AccountCreditedEvent
MoneyTransferCreatedEvent
DebitRecordedEvent
AccountDebitedEvent
AccountCreditedEvent
Account
id = 101
balance = 250
Account
id = 202
balance = 125
Account
id = 101
balance = 195
Account
id = 202
balance = 180
How to atomicallyupdate the
datastore and publish event(s)?
Use 2PC
Guaranteed atomicity BUT
Need a distributed transaction
manager
Database and message broker must
support 2PC
Impacts reliability
Not fashionable
2PC is best avoided
Use datastore as a message queue
1. Update database: new entity state
& event
2. Consume event & mark event as
consumed
Eventually consistent mechanism
See BASE: An Acid Alternative,
http://bit.ly/ebaybase
• BUT Tangled business logic and
event publishing code
• Difficult to implement when using a
NoSQL database :-(
24.
@crichardson
Agenda
Why build event-drivenmicroservices?
Overview of event sourcing
Designing microservices with event sourcing
Implementing queries in an event sourced application
Building and deploying microservices
25.
@crichardson
Event sourcing
For eachaggregate:
Identify (state-changing) domain events
Define Event classes
For example,
Account: AccountOpenedEvent, AccountDebitedEvent,
AccountCreditedEvent
ShoppingCart: ItemAddedEvent, ItemRemovedEvent,
OrderPlacedEvent
@crichardson
Before: update state+ publish
events
Two actions that must be atomic
Single action that can
be done atomically
Now: persist (and publish)
events
@crichardson
Event Store publishesevents -
consumed by other services
Event
Store
Event
Subscriber
subscribe(EventTypes)
publish(event)
publish(event)
Aggregate
NoSQL
materialized
view
update()
update()
Microservice B
34.
@crichardson
Optimizing using snapshots
Mostaggregates have relatively few events
BUT consider a 10-year old Account many transactions
Therefore, use snapshots:
Periodically save snapshot of aggregate state
Typically serialize a memento of the aggregate
Load latest snapshot + subsequent events
@crichardson
Business benefits ofevent
sourcing
Built-in, reliable audit log
Enables temporal queries
Publishes events needed by big data/predictive analytics etc.
Preserved history More easily implement future
requirements
38.
@crichardson
Technical benefits ofevent
sourcing
Solves data consistency issues in a Microservice/NoSQL-
based architecture:
Atomically save and publish events
Event subscribers update other aggregates ensuring
eventual consistency
Event subscribers update materialized views in SQL and
NoSQL databases (more on that later)
Eliminates O/R mapping problem
39.
@crichardson
Drawbacks of eventsourcing
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 (more on
that later)
40.
@crichardson
Agenda
Why build event-drivenmicroservices?
Overview of event sourcing
Designing microservices with event sourcing
Implementing queries in an event sourced application
Building and deploying microservices
@crichardson
Agenda
Why build event-drivenmicroservices?
Overview of event sourcing
Designing microservices with event sourcing
Implementing queries in an event sourced application
Building and deploying microservices
@crichardson
Displaying balance +recent
credits and debits
We need to do a “join: between the Account and the
corresponding MoneyTransfers
(Assuming Debit/Credit events don’t include other account, ...)
BUT
Event Store = primary key lookup of individual aggregates, ...
Use Command Query Responsibility Segregation
Other kinds ofviews
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
54.
Benefits and drawbacksof
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
55.
@crichardson
Agenda
Why build event-drivenmicroservices?
Overview of event sourcing
Designing microservices with event sourcing
Implementing queries in an event sourced application
Building and deploying microservices
@crichardson
Spring Boot basedmicro
services
Makes it easy to create stand-alone, production ready
microservices
Automatically configures Spring using Convention over
Configuration
Externalizes configuration
Built-in health checks and (Codahale) metrics
Generates standalone executable JARs with embedded
web server
@crichardson
Building Docker images
cp../build/libs/service.${1}.jar build/service.jar
docker build -t service-${VERSION} .
docker/build.sh
Building only takes 5
seconds!
60.
@crichardson
Smoke testing dockerimages
Smoke test
Docker
daemon
Service
containerGET /health
POST /containers/create
creates
POST /containers/{id}/start
Docker daemon must listen on
TCP port
@crichardson
CI environment runson
Docker
EC2 Instance
Jenkins
Container
Artifactory
container
EBS volume
/jenkins-
home
/gradle-home
/artifactory-
home
63.
@crichardson
Updating the production
environment
LargeEC2 instance running Docker
Deployment tool:
1. Compares running containers with what’s been built by Jenkins
2. Pulls latest images from Docker registry
3. Stops old versions
4. Launches new versions
One day: use Docker clustering solution and a service discovery mechanism,
Most likely, AWS container service
Mesos and Marathon + Zookeeper, Kubernetes or ???
64.
@crichardson
Summary
Event sourcing solveskey data consistency issues with:
Microservices
Partitioned SQL/NoSQL databases
Use CQRS to implement materialized views for queries
Spring Boot is a great foundation for microservices
Docker is a great way to package microservices