Page 1
@crichardson
Microservices in Java and Scala
Chris Richardson
Founder of Eventuate.io Founder of the original CloudFoundry.com Author of POJOs in Action
@crichardson [email protected] http://microservices.io http://eventuate.io http://plainoldobjects.com
Copyright © 2015. Chris Richardson Consulting, Inc. All rights reserved
Page 2
@crichardson
About Chris
Page 3
@crichardson
About Chris
Consultant and trainer focusing on
microservices (public class: April 28th, Oakland, CA)
http://www.chrisrichardson.net/
Page 4
@crichardson
About Chris
Founder of a startup that is creating a platform that makes it easy for
application developers write microservices
(http://eventuate.io)
Page 5
@crichardson
For more information
http://bit.ly/eventsmarch17
Page 6
AgendaWhy a pattern language for microservices?
Monolith architecture vs. microservices
Developing microservices with event sourcing and CQRS
Microservices in Java
Microservices in Scala Example: real-time, collaborative Kanban board application
Page 7
@crichardson
In 1986…
http://en.wikipedia.org/wiki/Fred_Brooks
Page 8
@crichardson
Yet 30 years later….
If you __________________ a puppy will die Therefore you must _______________
Page 9
@crichardson
Quiz - fill in the blanks….
mutate state
use monads
use objects
use functions
block a thread
use async.
make a REST call
send a messageuse Spring
use ….
Page 10
@crichardson
How we make decisions
Decide using
emotions
Rationalize with our intellect
http://en.wikipedia.org/wiki/Mahout
Page 11
@crichardson
The structure of a pattern
=
Great framework for discussing and thinking about technology
Page 12
@crichardson
The structure of a pattern
Resulting context
aka the situation
Name
Context
Problem
Related patterns
(conflicting) issues etc to address Forces
Solution
Page 14
AgendaWhy a pattern language for microservices?
Monolith architecture vs. microservices
Developing microservices with event sourcing and CQRS
Microservices in Java
Microservices in Scala Example: real-time, collaborative Kanban board application
Page 16
In theory:
We can build a modular monolith
But in practice:
We build a big ball of mud
Page 17
Microservices are not a silver bullet but …
Page 18
@crichardson
The benefits typically outweigh the drawbacks
for large, complex applications
Page 19
@crichardson
Build and deliver better software faster
Page 20
@crichardson
Easily try other technologies
... and fail safely
Page 21
AgendaWhy a pattern language for microservices?
Monolith architecture vs. microservices
Developing microservices with event sourcing and CQRS
Microservices in Java
Microservices in Scala Example: real-time, collaborative Kanban board application
Page 22
Data management patterns
Database per Service
Event-driven architecture
Shared database
Event sourcing
Transaction log tailing
Database triggers
Application events
CQRS
Database architecture
Page 23
Database per Service
Orders Service Customer Service
Order Database
Customer Database
Sharded SQL
NoSQL DB
Page 24
@crichardson
Customer management
How to maintain invariants?
Order management
Order Service
placeOrder()
Customer Service
updateCreditLimit()
Customer
creditLimit ...
has ordersbelongs toOrder
total
Invariant: sum(open order.total) <= customer.creditLimit
?
Page 25
@crichardson
Event-driven architecture
Page 26
@crichardson
How atomically update database and publish an event
Order Service
Order Database
Message Broker
insert Order
publish OrderCreatedEvent
dual write problem
?
Page 27
@crichardson
Reliably generating events
Page 28
@crichardson
Use event-sourcingEvent table
Aggregate type
Event id
Aggregate id
Event data
Order 902101 …OrderApproved
Order 903101 …OrderShipped
Event type
Order 901101 …OrderCreated
Page 29
@crichardson
Replay events to recreate state
Order
state
OrderCreated(…) OrderAccepted(…) OrderShipped(…)
Events
Periodically snapshot to avoid loading all events
Page 30
But what about queries?
Page 31
@crichardson
Command Query Responsibility Segregation (CQRS)
Command side
Commands
Aggregate
Event Store
Events
Query side
Queries
(Materialized) View
Events
Page 32
@crichardson
Query-side design
Event Store
Updater
View Updater Service
Events
Reader
HTTP GET Request
View Query Service
View Store
e.g. MongoDB
Neo4J CloudSearch
update query
Page 33
Eventuate architecture
Page 34
Eventuate platform
Page 35
Multiple flavors of client frameworks
“Traditional Java” mutable object-oriented domain objects
https://github.com/cer/event-sourcing-examples/tree/master/java-spring
Functional Scala with immutable domain objects
https://github.com/cer/event-sourcing-using-scala-typeclasses
Hybrid OO/Functional Scala with immutable domain objects
https://github.com/cer/event-sourcing-examples/tree/master/scala-spring
Page 36
AgendaWhy a pattern language for microservices?
Monolith architecture vs. microservices
Developing microservices with event sourcing and CQRS
Microservices in Java
Microservices in Scala Example: real-time, collaborative Kanban board application
Page 37
Customer command side
Page 38
@crichardson
The Customer aggregate
creditLimit creditReservations : Map<OrderId, Money>
Customer
List<Event> process(CreateCustomerCommand cmd) { … } List<Event> process(ReserveCreditCommand cmd) { … } … void apply(CustomerCreatedEvent anEvent) { … } void apply(CreditServedEvent anEvent) { … } …
State
Behavior
Page 39
@crichardson
Familiar concepts restructured
class Customer {
public void reserveCredit( orderId : String, amount : Money) {
// verify
// update state this.xyz = … }
public List<Event> process( ReserveCreditCommand cmd) { // verify … return … new CreditReservedEvent(); }
public void apply( CreditReservedEvent event) { // update state this.xyz = event.xyz }
Page 40
@crichardson
Customer command processing
Page 41
@crichardson
Customer applying events
Page 42
@crichardson
Creating an order
save() concisely specifies: 1.Creates Order aggregate 2.Processes command 3.Applies events 4.Persists events
Page 43
@crichardson
Event handling in Customers
1. Load Customer aggregate 2. Processes command 3. Applies events 4. Persists events
Triggers BeanPostProcessor Durable subscription name
Page 44
Customer - query side
Page 45
@crichardson
MongoDB view: customer and their order history
{ "_id" : "0000014f9a45004b 0a00270000000000", "_class" : "net.chrisrichardson…..views.orderhistory.CustomerView", "version" : NumberLong(5), "orders" : { "0000014f9a450063 0a00270000000000" : { "state" : "APPROVED", "orderId" : "0000014f9a450063 0a00270000000000", "orderTotal" : { "amount" : "1234" } }, "0000014f9a450063 0a00270000000001" : { "state" : "REJECTED", "orderId" : "0000014f9a450063 0a00270000000001", "orderTotal" : { "amount" : "3000" } } }, "name" : "Fred", "creditLimit" : { "amount" : "2000" } }
Denormalized = efficient lookup
Page 46
@crichardson
Query-side event handler that updates customer view
Page 47
@crichardson
Updating and query view using Spring Data for MongoDB...
Page 48
AgendaWhy a pattern language for microservices?
Monolith architecture vs. microservices
Developing microservices with event sourcing and CQRS
Microservices in Java
Microservices in Scala Example: real-time, collaborative Kanban board application
Page 49
@crichardson
Functional Customer aggregate
Customer
creditLimit creditReservations …
CustomerAggregate
processCommand(Account, Command) : Seq[Events]
applyEvent(Account, Event) : Account
Immutable state Behavior
Page 50
@crichardson
Aggregate type classesUsed by
Event Store to
reconstitute aggregates
Hardwired
Page 51
@crichardson
Customer Aggregate….State
Behavior
Page 52
@crichardson
…command processing…
Page 53
@crichardson
… applying events
Page 54
AgendaWhy a pattern language for microservices?
Monolith architecture vs. microservices
Developing microservices with event sourcing and CQRS
Microservices in Java
Microservices in Scala Example: real-time, collaborative Kanban board application
Page 55
@crichardson
Kanban board example
Page 56
@crichardson
ArchitectureCreate/update boards
and tasks
Change notifications Materialized views
Event Store
Page 57
@crichardson
Demo
Page 58
Summary
Microservices are not a silver bullet but they are the best choice for large/complex applications
Use an event-driven microservices architecture
Build services using event sourcing + CQRS
Using a language/framework specific programming model
Page 59
@crichardson
@crichardson [email protected]
http://bit.ly/eventsmarch17
Questions?