27
Microservices and Redis Chris Richardson Founder of Eventuate.io Founder of the original CloudFoundry.com Author of POJOs in Action @crichardson [email protected] http://eventuate.io

Microservices and Redis #redisconf Keynote

Embed Size (px)

Citation preview

Page 1: Microservices and Redis #redisconf Keynote

Microservices and Redis

Chris Richardson

Founder of Eventuate.io Founder of the original CloudFoundry.com Author of POJOs in Action

@crichardson [email protected] http://eventuate.io

Page 2: Microservices and Redis #redisconf Keynote

@crichardson

About Chris

Page 3: Microservices and Redis #redisconf Keynote

@crichardson

Why microservices?

Page 4: Microservices and Redis #redisconf Keynote

@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

Page 5: Microservices and Redis #redisconf Keynote

@crichardson

But successful applications

keep growing ….

Eventually, agile development

and deployment becomes

impossible

Page 6: Microservices and Redis #redisconf Keynote

@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

Page 7: Microservices and Redis #redisconf Keynote

@crichardson

Develop and deploy faster

Page 8: Microservices and Redis #redisconf Keynote

@crichardson

Scale more easily

Page 9: Microservices and Redis #redisconf Keynote

@crichardson

Pick the best technology

Page 10: Microservices and Redis #redisconf Keynote

@crichardson

Microservices are not a free lunch

BUT

for most large, complex applications are good choice

Page 11: Microservices and Redis #redisconf Keynote

@crichardson

Where does Redis fit in?

Page 12: Microservices and Redis #redisconf Keynote

Using Redis within a service

Caching High performance messaging High performance database

Catalog Service

Catalog DB

Page 13: Microservices and Redis #redisconf Keynote

@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

Page 14: Microservices and Redis #redisconf Keynote

@crichardson

Redis and the API Gateway

API Gateway

Caching Rate limiting Routing

Page 15: Microservices and Redis #redisconf Keynote

@crichardson

How to maintain data consistency?

Page 16: Microservices and Redis #redisconf Keynote

@crichardson

Database per serviceCatalog Service

Review Service

Order Service

… Service

Catalog Database

Review Database

Order Database

… Database

How to maintain data consistency without 2PC?

Page 17: Microservices and Redis #redisconf Keynote

@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

?

Page 18: Microservices and Redis #redisconf Keynote

@crichardson

Use event-driven, eventually consistent order processing

Order Service

Customer Service

Order created

Credit Reserved

Credit Check Failed

Place Order

OR

Page 19: Microservices and Redis #redisconf Keynote

@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

?

Page 20: Microservices and Redis #redisconf Keynote

@crichardson

Use event sourcing

Order Service

Order Database

Event Store

insert Order

Save OrderCreatedEvent

X Persistence and notification

Page 21: Microservices and Redis #redisconf Keynote

@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

Page 22: Microservices and Redis #redisconf Keynote

@crichardson

How to implement queries?

Page 23: Microservices and Redis #redisconf Keynote

@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 ServiceOrder Service

What if event sourcing is used?

…. is no longer easy

Page 24: Microservices and Redis #redisconf Keynote

@crichardson

Command Query Responsibility Segregation (CQRS)

Command side

Commands

Aggregate

Event Store

Events

Query side

Queries

Materialized View

Events

POST PUT DELETE GET

Page 25: Microservices and Redis #redisconf Keynote

@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

Page 26: Microservices and Redis #redisconf Keynote

@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

Page 27: Microservices and Redis #redisconf Keynote

Thank you!

@crichardson

http://eventuate.io