A pattern language for microservices (#gluecon #gluecon2016)
@crichardson
A pattern language for
microservices
Chris Richardson
Founder of Eventuate.io
Founder of the original CloudFoundry.com
Author of POJOs in Action
@crichardson
chris@chrisrichardson.net
http://eventuate.io
@crichardson
Microservice architecture = functional
decomposition
Brows
er
Mobile
Device
Store
Front UI
API
Gateway
Catalog
Service
Review
Service
Order
Service
…
Service
Catalog
Database
Review
Database
Order
Database
…
Database
HTML
REST
REST
@crichardson
Drawbacks
Complexity of developing a distributed system
Implementing inter-process communication
Handling partial failures
Complexity of implementing business transactions that span multiple
databases (without 2PC)
Complexity of testing a distributed system
Complexity of deploying and operating a distributed system
Managing the development and deployment of features that span
multiple services
Fortunately solutions exists
@crichardson
Issues to address
How to deploy the services?
How do the services communicate?
How do clients of the application communicate with the
services?
How to partition the system into services?
How to deal with distributed data management
problems?
….
@crichardson
Agenda
Why a pattern language for microservices?
Core patterns: monolith vs. microservices
Distributed data management patterns
@crichardson
Database per service
Order Service Customer Service
Order Database Customer Database
Order table
Customer
table
orderTotal creditLimit
Loose coupling 😀 but more complex 😓 and….
@crichardson
Customer management
How to maintain data consistency
without 2PC?
Order management
Order Service
placeOrder()
Customer Service
updateCreditLimit()
Customer
creditLimit
...
has ordersbelongs toOrder
total
Invariant:
sum(open order.total) <= customer.creditLimit
?
@crichardson
Replay events to recreate
state
Order
state
OrderCreated(…)
OrderAccepted(…)
OrderShipped(…)
Events
Periodically snapshot to avoid loading all events
@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
Command Query Responsibility
Segregation (CQRS)
Command side
Commands
Aggregate
Event Store
Events
Query side
Queries
Materialized
View
Events
POST
PUT
DELETE
GET
MongoDB
Redis
Neo4j
SQL
ElasticSearch
More complex 😓 but high performance, scalable views 😀