WHEN MICROSERVICES MEET EVENT SOURCING
Vinicius Gomes
1
WHEN MICROSERVICES MEET EVENT SOURCING Vinicius Gomes 1 - - PowerPoint PPT Presentation
WHEN MICROSERVICES MEET EVENT SOURCING Vinicius Gomes 1 VINICIUS GOMES Software developer at ThoughtWorks vvgomes.com/blog twitter.com/vvgomes 2 AGENDA The Traditional Approach Introduction to Event Sourcing CQRS Demo Summary 3 THE
Vinicius Gomes
1
VINICIUS GOMES
Software developer at ThoughtWorks vvgomes.com/blog twitter.com/vvgomes
2
AGENDA The Traditional Approach Introduction to Event Sourcing CQRS Demo Summary
3
4
EXAMPLE
Online Restaurant
5
ONLINE RESTAURANT
Data Model
6
ONLINE RESTAURANT
Services
7
Restaurant
ONLINE RESTAURANT
8
📲
POST G E T
I want to
Restaurant
ONLINE RESTAURANT
9
📲
POST GET
I want to add an item to my order!
THE TRADITIONAL APPROACH
10
GET POST PUT DELETE
11
CHALLENGES
Coupling
12
CHALLENGES
Coupling Resilience
13
Restaurant
📲
POST GET
I want to add an item to my order!
CHALLENGES
Coupling Resilience Response time
14
CHALLENGES
Coupling Resilience Response time User intent
15
Add item to order
CHALLENGES
Coupling Resilience Response time User intent Current / mutable state
16
{ "id": “6a208c41…”, "status": "OPEN", "items": [ { "id": “be596c8e…”, "quantity": 1 }, { "id": “5d09509c…”, "quantity": 2 }, { "id": “cc52d1b6…”, "quantity": 1 } ] }
17
EVENT SOURCING
18
DOMAIN EVENTS
19
{ "payload": { "orderId": “be596c8e…”, "itemId": “5d09509c…”, "quantity": 1 }, "payloadType": “com.restaurant.ItemAddedToOrderEvent”, "timestamp": “2017-03-25 08:48:51 -03:00”, "revision": “2”, "aggregateId": “6a208c41…” }
Model the past Immutable Permanent
EVENT STORE
Append only Sequential Long-lived
20
AGGREGATES
21
Reducer function
Reducing events into state
AGGREGATES
Example
22
{ }
AGGREGATES
Example
23
payload: { id: “42” }
{ "id": “42”, "status": "OPEN", "items": [ ] }
AGGREGATES
Example
24
{ "id": “42”, "status": "OPEN", "items": [ { "id": “43”, “quantity": 1 } ] }
payload: {
itemId: “43”, quantity: 1 }
payload: {
itemId: “44”, quantity: 2 }
AGGREGATES
Example
25
{ "id": “42”, "status": "OPEN", "items": [ { "id": “43”, “quantity": 1 }, { "id": “44”, “quantity": 2 } ] }
AGGREGATES
Example
26
{ "id": “42”, "status": "OPEN", "items": [ { "id": “44”, “quantity": 2 } ] }
payload: {
itemId: “43” }
AGGREGATES
Example
27
{ "id": “42”, "status": "PLACED", "items": [ { "id": “44”, “quantity": 2 } ] }
payload: {
}
AGGREGATES
Example
28
{ "id": “42”, "status": "PLACED", "items": [ { "id": “44”, “quantity": 2 } ] }
Order Aggregate
COMMANDS
29
Add item to order
{ "type": “com.restaurant.AddItemToOrderCommand”, "payload": { "orderId": “be596c8e…”, "itemId": “5d09509c…”, "quantity": 1 }, "timestamp": “2017-03-25 08:48:51 -03:00” }
COMMANDS
30
Handling a command
COMMANDS
31
How do clients send commands?
📲
I want to add an item to my order!
COMMANDS
32
Reviewing REST
COMMANDS
33
Reviewing REST
COMMANDS
34
Reviewing REST
host/api/orders/8659a0d6/items/5d1a8457
COMMANDS
35
Reviewing REST
COMMANDS
36
Reviewing REST
COMMANDS
37
COMMANDS
Option #1 Generic “Commands” nested-resource
38
POST /orders/8659a0d6/commands
{ “type": “AddItemToOrderCommand”, "menuItem": “/menu/items/8d30d99“, “quantity": 2 }
COMMANDS
Option #2 Command URI
39
POST /orders/8659a0d6/items/commands/add
{ "menuItem": “/menu/items/8d30d99“, “quantity": 2 }
COMMANDS
Command discoverability
40
EVENT SOURCING
41
42
QUERIES
Bad news 😕 The Event Store is not good for queries
43
Most applications
44
CQRS
45
Write ✍ Read 📗
Service
CQRS
46
📲
Command Query Publish L i s t e n
Service A
CQRS
47
Service B
CQRS
48
Command model
Query model
49
DEMO
Online Restaurant
50
Service
DEMO
Online Restaurant
51
EXAMPLE: ONLINE RESTAURANT
52
Online Restaurant
Publishes Listen to
EXAMPLE: ONLINE RESTAURANT
53
Online Restaurant
Publishes Listen to P u b l i s h e s L i s t e n t
TECHNOLOGIES
Java Spring Boot Axon Framework Spring Data REST RabbitMQ
54
https://github.com/vvgomes/event-driven-restaurant
55
MICROSERVICES + EVENT SOURCING Benefits
56
Challenges
Vinicius Gomes
57