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 TRADITIONAL APPROACH 4
EXAMPLE Online Restaurant 5
ONLINE RESTAURANT Data Model 6
ONLINE RESTAURANT Services 7
ONLINE RESTAURANT Restaurant I want to open an order ! POST 📲 G E T 200 ✔ 8
ONLINE RESTAURANT Restaurant I want to add an item to my order! GET POST 📲 200 ✔ 9
THE TRADITIONAL APPROACH • Simple to implement • Technology agnostic GET POST PUT DELETE 10
CHALLENGES 11
CHALLENGES Coupling 12
CHALLENGES Coupling Resilience Restaurant I want to add an item to my order! GET POST 📲 503 👏 13
CHALLENGES Coupling Resilience Response time 14
CHALLENGES Coupling Resilience Add item to order Response time User intent POST /orders/8659a0d6/items/ 15
CHALLENGES Coupling Resilience { "id": “6a208c41…”, Response time "status": "OPEN", "items": [ { User intent "id": “be596c8e…”, "quantity": 1 Current / mutable state }, { "id": “5d09509c…”, "quantity": 2 }, { "id": “cc52d1b6…”, "quantity": 1 } ] } 16
EVENT SOURCING 17
EVENT SOURCING 18
DOMAIN EVENTS Model the past Immutable Permanent { "payload": { "orderId": “be596c8e…”, "itemId": “5d09509c…”, "quantity": 1 }, "payloadType": “com.restaurant.ItemAddedToOrderEvent”, "timestamp": “2017-03-25 08:48:51 -03:00”, "revision": “2”, "aggregateId": “6a208c41…” } 19
EVENT STORE Append only Sequential Long-lived 20
AGGREGATES Reducing events into state state = reduce(handle, {}, events) Reducer function 21
AGGREGATES Example { } 22
AGGREGATES Example { "id": “42”, "status": "OPEN", "items": [ ] } payload: { id: “42” } 23
AGGREGATES Example { "id": “42”, "status": "OPEN", "items": [ { "id": “43”, “quantity": 1 } ] } payload: { orderId: “42”, itemId: “43”, quantity: 1 } 24
AGGREGATES Example { "id": “42”, "status": "OPEN", "items": [ { "id": “43”, “quantity": 1 }, { "id": “44”, “quantity": 2 } ] } payload: { orderId: “42”, itemId: “44”, quantity: 2 } 25
AGGREGATES Example { "id": “42”, "status": "OPEN", "items": [ { "id": “44”, “quantity": 2 } ] } payload: { orderId: “42”, itemId: “43” } 26
AGGREGATES Example { "id": “42”, "status": "PLACED", "items": [ { "id": “44”, “quantity": 2 } ] } payload: { orderId: “42” } 27
AGGREGATES Example Order Aggregate { "id": “42”, "status": "PLACED", "items": [ { "id": “44”, “quantity": 2 } ] } 28
COMMANDS Add item to order { "type": “com.restaurant. AddItemToOrderCommand ”, "payload": { "orderId": “be596c8e…”, "itemId": “5d09509c…”, "quantity": 1 }, "timestamp": “2017-03-25 08:48:51 -03:00” } 29
COMMANDS Handling a command (state, command) -> [event] 30
COMMANDS How do clients send commands? I want to add an item to my order! ? 📲 31
COMMANDS Reviewing REST Resource Collection host/api/orders 32
COMMANDS Reviewing REST Resource host/api/orders/8659a0d6 33
COMMANDS Reviewing REST Nested Resource host/api/orders/8659a0d6/items/5d1a8457 34
COMMANDS Reviewing REST HTTP Methods GET POST PUT/PATCH DELETE 35
COMMANDS Reviewing REST Hypermedia Formats 36
COMMANDS Commands can be resources 37
COMMANDS Option #1 Generic “Commands” nested-resource POST /orders/8659a0d6/commands { “type": “AddItemToOrderCommand”, "menuItem": “/menu/items/8d30d99“, “quantity": 2 } 38
COMMANDS Option #2 Command URI POST /orders/8659a0d6/items/commands/add { "menuItem": “/menu/items/8d30d99“, “quantity": 2 } 39
COMMANDS Command discoverability 40
EVENT SOURCING 41
Queries? 42
QUERIES Bad news 😕 The Event Store is not good for queries Most applications •A few writes •A lot of reads 43
CQRS 44
CQRS C ommand Write ✍ Q uery Read 📗 R esponsibility S egregation 45
CQRS Service Publish Command 📲 Query n e t s i L 46
CQRS Service A Service B 47
CQRS Command model Query model •Command de fi nitions •Event listeners •Event de fi nitions •Query entities •The aggregate •Repositories •Aggregate repository •Read only API •Write only API 48
DEMO 49
DEMO Online Restaurant Service 50
DEMO Online Restaurant 51
EXAMPLE: ONLINE RESTAURANT Online Restaurant Listen to Publishes 52
EXAMPLE: ONLINE RESTAURANT Online Restaurant Listen to Publishes P u o t b n l e i s t s h i L e s 53
TECHNOLOGIES Java Spring Boot Axon Framework Spring Data REST RabbitMQ https://github.com/vvgomes/event-driven-restaurant 54
SUMMARY 55
MICROSERVICES + EVENT SOURCING Bene fi ts Challenges •History based queries •Complexity •Audit log by design •Snapshots •Immutability •Upcasting •User intent •Race conditions •Decoupling •Event contracts •Resilience •Eventual consistency 56
WHEN MICROSERVICES MEET EVENT SOURCING Vinicius Gomes 57
Recommend
More recommend