when microservices meet event sourcing
play

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


  1. WHEN MICROSERVICES MEET EVENT SOURCING Vinicius Gomes 1

  2. VINICIUS GOMES Software developer at ThoughtWorks vvgomes.com/blog twitter.com/vvgomes 2

  3. AGENDA The Traditional Approach Introduction to Event Sourcing CQRS Demo Summary 3

  4. THE TRADITIONAL APPROACH 4

  5. EXAMPLE Online Restaurant 5

  6. ONLINE RESTAURANT Data Model 6

  7. ONLINE RESTAURANT Services 7

  8. ONLINE RESTAURANT Restaurant I want to open an order ! POST 📲 G E T 200 ✔ 8

  9. ONLINE RESTAURANT Restaurant I want to add an item to my order! GET POST 📲 200 ✔ 9

  10. THE TRADITIONAL APPROACH • Simple to implement • Technology agnostic GET POST PUT DELETE 10

  11. CHALLENGES 11

  12. CHALLENGES Coupling 12

  13. CHALLENGES Coupling Resilience Restaurant I want to add an item to my order! GET POST 📲 503 👏 13

  14. CHALLENGES Coupling Resilience Response time 14

  15. CHALLENGES Coupling Resilience Add item to order Response time User intent POST /orders/8659a0d6/items/ 15

  16. 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

  17. EVENT SOURCING 17

  18. EVENT SOURCING 18

  19. 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

  20. EVENT STORE Append only Sequential Long-lived 20

  21. AGGREGATES Reducing events into state state = reduce(handle, {}, events) Reducer function 21

  22. AGGREGATES Example { } 22

  23. AGGREGATES Example { "id": “42”, "status": "OPEN", "items": [ ] } payload: { id: “42” } 23

  24. AGGREGATES Example { "id": “42”, "status": "OPEN", "items": [ { "id": “43”, “quantity": 1 } ] } payload: { orderId: “42”, itemId: “43”, quantity: 1 } 24

  25. AGGREGATES Example { "id": “42”, "status": "OPEN", "items": [ { "id": “43”, “quantity": 1 }, { "id": “44”, “quantity": 2 } ] } payload: { orderId: “42”, itemId: “44”, quantity: 2 } 25

  26. AGGREGATES Example { "id": “42”, "status": "OPEN", "items": [ { "id": “44”, “quantity": 2 } ] } payload: { orderId: “42”, itemId: “43” } 26

  27. AGGREGATES Example { "id": “42”, "status": "PLACED", "items": [ { "id": “44”, “quantity": 2 } ] } payload: { orderId: “42” } 27

  28. AGGREGATES Example Order Aggregate { "id": “42”, "status": "PLACED", "items": [ { "id": “44”, “quantity": 2 } ] } 28

  29. 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

  30. COMMANDS Handling a command (state, command) -> [event] 30

  31. COMMANDS How do clients send commands? I want to add an item to my order! ? 📲 31

  32. COMMANDS Reviewing REST Resource Collection host/api/orders 32

  33. COMMANDS Reviewing REST Resource host/api/orders/8659a0d6 33

  34. COMMANDS Reviewing REST Nested Resource host/api/orders/8659a0d6/items/5d1a8457 34

  35. COMMANDS Reviewing REST HTTP Methods GET POST PUT/PATCH DELETE 35

  36. COMMANDS Reviewing REST Hypermedia Formats 36

  37. COMMANDS Commands can be resources 37

  38. COMMANDS Option #1 Generic “Commands” nested-resource POST /orders/8659a0d6/commands { “type": “AddItemToOrderCommand”, "menuItem": “/menu/items/8d30d99“, “quantity": 2 } 38

  39. COMMANDS Option #2 Command URI POST /orders/8659a0d6/items/commands/add { "menuItem": “/menu/items/8d30d99“, “quantity": 2 } 39

  40. COMMANDS Command discoverability 40

  41. EVENT SOURCING 41

  42. Queries? 42

  43. QUERIES Bad news 😕 The Event Store is not good for queries Most applications •A few writes •A lot of reads 43

  44. CQRS 44

  45. CQRS C ommand Write ✍ Q uery Read 📗 R esponsibility S egregation 45

  46. CQRS Service Publish Command 📲 Query n e t s i L 46

  47. CQRS Service A Service B 47

  48. 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

  49. DEMO 49

  50. DEMO Online Restaurant Service 50

  51. DEMO Online Restaurant 51

  52. EXAMPLE: ONLINE RESTAURANT Online Restaurant Listen to Publishes 52

  53. 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

  54. TECHNOLOGIES Java Spring Boot Axon Framework Spring Data REST RabbitMQ https://github.com/vvgomes/event-driven-restaurant 54

  55. SUMMARY 55

  56. 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

  57. WHEN MICROSERVICES MEET EVENT SOURCING Vinicius Gomes 57

Recommend


More recommend