Microservices and Redis
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
About Chris
@crichardson
Why microservices?
@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
….
Eventually, agile
development
and
deployment
becomes
impossible
@crichardson
Microservice architecture = functional
decomposition
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
@crichardson
Develop and deploy faster
@crichardson
Scale more easily
@crichardson
Pick the best technology
@crichardson
Microservices are not a free lunch
BUT
for most large, complex applications are good
choice
@crichardson
Where does Redis fit in?
Using Redis within a service
Caching
High performance messaging
High performance database
Catalog
Service
Catalog
DB
@crichardson
Using Redis for inter-service communication
API
Gateway
Catalog
Service
Review
Service
…
Service
Pub/Sub
Need Product
Details X
GET /product/X
Need … Need … Need …Info Info Info
@crichardson
Redis and the API Gateway
API
Gateway
Caching
Rate limiting
Routing
@crichardson
How to maintain data consistency?
@crichardson
Database per service
Catalog
Service
Review
Service
Order
Service
…
Service
Catalog
Database
Review
Database
Order
Database
…
Database
How to maintain data
consistency without
2PC?
@crichardson
Customer DB
How to maintain data consistency without
2PC?
Order DB
Order Service
placeOrder()
Customer Service
updateCreditLimit()
Customer
creditLimit
...
has ordersbelongs toOrder
total
Invariant:
sum(open order.total) <= customer.creditLimit
?
@crichardson
Use event-driven, eventually consistent order
processing
Order
Service
Customer
Service
Order created
Credit Reserved
Credit Check Failed
Place Order
OR
@crichardson
How to atomically update database and
publish an event without 2PC?
Order Service
Order
Database
Message Broker
insert Order
publish
OrderCreatedEvent
dual write problem
?
@crichardson
Use event sourcing
Order Service
Order
Database
Event Store
insert Order
Save
OrderCreatedEvent
X
Persistence
and notification
@crichardson
Event sourcing: 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 Order
state
total
Replay
@crichardson
How to implement 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
@crichardson
Food to Go example - on github.com
http://bit.ly/redis-pia
94619:Monday [0700_2:1430, 1130_1:1430, ...]
94619:Tuesday [0700_2:1430, 1130_1:1430, ...]
… …
Restaurant
View
Updater
Service
Restaurant
View
Query
Service
ZRANGEBYSCORE
Event Store
Restaurant
events Materialized view of
restaurant locations
and opening hours
GET
available
restaurants
ZADD
ZREM
Sorted sets
@crichardson
Summary
Organize into small, agile, autonomous teams responsible for one or more
microservices
Use an event-driven architecture, based on event sourcing and CQRS
Use Redis throughout a microservices architecture
Thank you!
@crichardson
http://eventuate.io

Microservices and Redis #redisconf Keynote