When Microservices Meet Event Sourcing

When Microservices Meet Event Sourcing

Presented at:
* ThoughtWorks Tech Talks NYC Meetup on May 2017
* Developer Week Conference NYC on Jun 2017

439e623c5d770ae0da8e61ec08452032?s=128

Vinicius Gomes

May 11, 2017
Tweet

Transcript

  1. 16.

    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 } ] }
  2. 19.

    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
  3. 23.

    AGGREGATES Example 23 payload: { id: “42” } { "id":

    “42”, "status": "OPEN", "items": [ ] }
  4. 24.

    AGGREGATES Example 24 { "id": “42”, "status": "OPEN", "items": [

    { "id": “43”, “quantity": 1 } ] } payload: { orderId: “42”, itemId: “43”, quantity: 1 }
  5. 25.

    payload: { orderId: “42”, itemId: “44”, quantity: 2 } AGGREGATES

    Example 25 { "id": “42”, "status": "OPEN", "items": [ { "id": “43”, “quantity": 1 }, { "id": “44”, “quantity": 2 } ] }
  6. 26.

    AGGREGATES Example 26 { "id": “42”, "status": "OPEN", "items": [

    { "id": “44”, “quantity": 2 } ] } payload: { orderId: “42”, itemId: “43” }
  7. 27.

    AGGREGATES Example 27 { "id": “42”, "status": "PLACED", "items": [

    { "id": “44”, “quantity": 2 } ] } payload: { orderId: “42” }
  8. 28.

    AGGREGATES Example 28 { "id": “42”, "status": "PLACED", "items": [

    { "id": “44”, “quantity": 2 } ] } Order Aggregate
  9. 29.

    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” }
  10. 38.

    COMMANDS Option #1 Generic “Commands” nested-resource 38 POST /orders/8659a0d6/commands {

    “type": “AddItemToOrderCommand”, "menuItem": “/menu/items/8d30d99“, “quantity": 2 }
  11. 43.

    QUERIES Bad news The Event Store is not good for

    queries 43 Most applications •A few writes •A lot of reads
  12. 44.
  13. 48.

    CQRS 48 Command model •Command definitions •Event definitions •The aggregate

    •Aggregate repository •Write only API Query model •Event listeners •Query entities •Repositories •Read only API
  14. 49.
  15. 54.

    TECHNOLOGIES Java Spring Boot Axon Framework Spring Data REST RabbitMQ

    54 https://github.com/vvgomes/event-driven-restaurant
  16. 56.

    MICROSERVICES + EVENT SOURCING Benefits •History based queries •Audit log

    by design •Immutability •User intent •Decoupling •Resilience 56 Challenges •Complexity •Snapshots •Upcasting •Race conditions •Event contracts •Eventual consistency