@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
Presentation goal
Why patterns and pattern
languages?
A pattern language for
microservices
@crichardson
About Chris
@crichardson
Agenda
Why a pattern language for microservices?
Core patterns: monolith vs. microservices
Distributed data management patterns
@crichardson
Let’s imagine you are building
a large, complex application,
e.g. an online store
@crichardson
Successful software development
Architecture
Process Organization
Agile
Continuous delivery
…
Small,
autonomous,
teams
@crichardson
?Architecture
@crichardson
No silver bullets
http://en.wikipedia.org/wiki/Fred_Brooks
@crichardson
We need architecture
and design patterns
Reusable solution
to a problem
occurring
in a particular context
@crichardson
Patterns force you to consider
tradeoffs
Resulting context
Benefits
Drawbacks
Issues to resolve
@crichardson
Patterns force you to consider
other patterns
Related patterns
Alternative solutions
Solutions to problems
introduced by this pattern
@crichardson
Microservices pattern language
http://microservices.io/
@crichardson
Agenda
Why a pattern language for microservices?
Core patterns: monolith vs. microservices
Distributed data management patterns
@crichardson
@crichardson
The monolithic architecture
Tomcat
Browser
WAR
SQL
database
HTML
REST/JSON
Client
App
Simple to ….
Develop
Test
Deploy
Scale
Catalog
Module
Reviews
Module
Orders
Module
StoreFront UI
Module
@crichardson
But
successful
applications
keep
growing
….
@crichardson
Monolithic architecture
Process Organization
Agile
Continuous delivery
…
Small,
autonomous,
teams
@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
Microservice architecture
Process Organization
Agile
Continuous delivery
…
Small,
autonomous,
teams✔ ✔
@crichardson
Drawbacks
Complexity
@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
The benefits typically
outweigh the drawbacks
for
large, complex applications
@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
Data management patterns
@crichardson
The Database
Shared database
Order Service Customer Service … Service
Order table
Customer
table
…
orderTotal creditLimit
Tight coupling
Simple and
ACID
@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
2PC (aka. distributed
transactions)
are not viable choice
for most modern applications
@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
Event-driven architecture
@crichardson
Use event-driven, eventually
consistent order processing
Order
Service
Customer
Service
Order created
Credit Reserved
Credit Check Failed
Place Order
OR
@crichardson
How atomically update
database and publish an event
Order Service
Order
Database
Message Broker
insert Order
publish
OrderCreatedEvent
dual write problem
?
@crichardson
Reliably publish events when
state changes
@crichardson
Use event-sourcing
Event table
Aggregate
type
Event
id
Aggregate
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
But what about queries?
@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 😀
@crichardson
Many more patterns…
@crichardson
Summary: Patterns and pattern
languages are a great way to …
Think about technology
Discuss technology
Apply technology
@crichardson
Summary: The Microservices pattern
language is a great way to …
Think about microservices
Discuss microservices
Apply microservices (or not)
@crichardson
@crichardson chris@chrisrichardson.net
http://eventuate.io
http://plainoldobjects.com http://microservices.io
Questions?

A pattern language for microservices (#gluecon #gluecon2016)