Upgrade to Pro — share decks privately, control downloads, hide ads and more …

Designing Events-first Microservices

Designing Events-first Microservices

In this talk, we will explore the nature of events, what it means to be event-driven, and how we can unleash the power of events and commands by applying an events-first domain-driven design to microservices-based architectures.

We will start by developing a solid theoretical understanding of how to design systems of event-driven microservices. Then we will discuss the practical tools and techniques you can use to reap the most benefit from that design, as well as, most importantly, what to avoid along the way.

We’ll discuss how an events-first design approach to building microservices can improve the following characteristics over competing techniques:
- increase certainty
- increase resilience
- increase scalability
- increase traceability
- increase loose coupling
- reduce risk

Skeptics should definitely attend.

Jonas Bonér

June 29, 2018
Tweet

More Decks by Jonas Bonér

Other Decks in Programming

Transcript

  1. 1. Events drive autonomy 2. Events help reduce risk 3.

    Events help you move faster 4. Events Increase Loose coupling 5. Events increase stability 6. Events increase scalability 7. Events increase resilience 8. Events increase traceability 9. Events allow for time-travel Why Should you care about Events?
  2. Why Now? 1. Cloud and multicore architectures 2. Microservices and

    distributed systems 3. Data-centric applications - Data in motion 4. “We want more, of everything, and we want it now.” -Your Customers
  3. “When you start modeling events, it forces you to think

    about the behaviour of the system. As opposed to thinking about the structure of the system.” - Greg Young A Decade of DDD, CQRS, Event Sourcing, Greg Young (Presentation from 2016)
  4. ✴ Don’t focus on the things The Nouns The Domain

    Objects ✴ Focus on what happens The Verbs The Events
  5. ✴ Events represent Facts of information ➡ Facts are immutable

    ➡ Facts Accrue - Knowledge can only grow The Nature of Events
  6. ✴ Events represent Facts of information ➡ Facts are immutable

    ➡ Facts Accrue - Knowledge can only grow ✴ Events/Facts can be disregarded/Ignored The Nature of Events
  7. ✴ Events represent Facts of information ➡ Facts are immutable

    ➡ Facts Accrue - Knowledge can only grow ✴ Events/Facts can be disregarded/Ignored ✴ Events/Facts Can not be retracted (once accepted) The Nature of Events
  8. ✴ Events represent Facts of information ➡ Facts are immutable

    ➡ Facts Accrue - Knowledge can only grow ✴ Events/Facts can be disregarded/Ignored ✴ Events/Facts Can not be retracted (once accepted) ✴ Events/Facts Can not be deleted (once accepted) ➡ Might be needed for legal or moral reasons The Nature of Events
  9. ✴ Events represent Facts of information ➡ Facts are immutable

    ➡ Facts Accrue - Knowledge can only grow ✴ Events/Facts can be disregarded/Ignored ✴ Events/Facts Can not be retracted (once accepted) ✴ Events/Facts Can not be deleted (once accepted) ➡ Might be needed for legal or moral reasons ✴ Events/Facts (new) can invalidate existing Facts The Nature of Events
  10. ✴ IntentS ➡ Communication ➡ Conversations ➡ Expectations ➡ Contracts

    ➡ Control Transfer Event Driven Design ✴ Facts ➡ State ➡ History ➡ Causality ➡ Notifications ➡ State Transfer
  11. ✴ IntentS ➡ Communication ➡ Conversations ➡ Expectations ➡ Contracts

    ➡ Control Transfer Event Driven Design ✴ Facts ➡ State ➡ History ➡ Causality ➡ Notifications ➡ State Transfer Commands
  12. ✴ IntentS ➡ Communication ➡ Conversations ➡ Expectations ➡ Contracts

    ➡ Control Transfer Event Driven Design ✴ Facts ➡ State ➡ History ➡ Causality ➡ Notifications ➡ State Transfer Commands Events
  13. ✴Commands ➡ Object form of method/Action request ➡ Imperative: CreateOrder,

    ShipProduct ✴Reactions ➡ Represents side-effects Event Driven Design
  14. ✴Commands ➡ Object form of method/Action request ➡ Imperative: CreateOrder,

    ShipProduct ✴Reactions ➡ Represents side-effects ✴Events ➡ Represents something that has happened ➡ Past-tense: OrderCreated, ProductShipped Event Driven Design
  15. Commands Events vs 1. All about intent 2. Directed 3.

    Single addressable destination 1. Intentless 2. Anonymous 3. Just happens - for others (0-N) to observe
  16. Commands Events vs 1. All about intent 2. Directed 3.

    Single addressable destination 4. Models personal communication 1. Intentless 2. Anonymous 3. Just happens - for others (0-N) to observe 4. Models broadcast (speakers corner)
  17. Commands Events vs 1. All about intent 2. Directed 3.

    Single addressable destination 4. Models personal communication 5. Distributed focus 1. Intentless 2. Anonymous 3. Just happens - for others (0-N) to observe 4. Models broadcast (speakers corner) 5. Local focus
  18. Commands Events vs 1. All about intent 2. Directed 3.

    Single addressable destination 4. Models personal communication 5. Distributed focus 6. Command & Control 1. Intentless 2. Anonymous 3. Just happens - for others (0-N) to observe 4. Models broadcast (speakers corner) 5. Local focus 6. Autonomy
  19. 1. REceive & react (or not) 
 to facts* that

    are coming its way Event Driven Services
  20. 1. REceive & react (or not) 
 to facts* that

    are coming its way 2. Publish new facts* asynchronously 
 to the rest of the world Event Driven Services
  21. 1. REceive & react (or not) 
 to facts* that

    are coming its way 2. Publish new facts* asynchronously 
 to the rest of the world 3. Invert the control flow
 to minimize coupling and increase autonomy Event Driven Services
  22. 1. REceive & react (or not) 
 to facts* that

    are coming its way 2. Publish new facts* asynchronously 
 to the rest of the world 3. Invert the control flow
 to minimize coupling and increase autonomy Event Driven Services *Facts == Immutable Events
  23. ✴ Maintains Integrity & consistency ✴ Is our Unit of

    Consistency ✴ Is our Unit of Failure ✴ Is our Unit of Determinism ✴ Is fully Autonomous The Aggregate
  24. ✴ CRUD is fine for totally isolated data ✴ But,

    cross CRUD services consistency The Problem With CRUD Services
  25. ✴ CRUD is fine for totally isolated data ✴ But,

    cross CRUD services consistency ➡ Is hard ⇨ No Joins The Problem With CRUD Services
  26. ✴ CRUD is fine for totally isolated data ✴ But,

    cross CRUD services consistency ➡ Is hard ⇨ No Joins ➡ Has ad-hoc & weak guarantees The Problem With CRUD Services
  27. ✴ CRUD is fine for totally isolated data ✴ But,

    cross CRUD services consistency ➡ Is hard ⇨ No Joins ➡ Has ad-hoc & weak guarantees The Problem With CRUD Services
  28. “In general, application developers simply do not implement large scalable

    applications assuming distributed transactions” - Pat Helland Life Beyond Distributed Transactions - Pat Helland
  29. “In a system which cannot count on distributed transactions, the

    management of uncertainty must be implemented in the business logic.” - Pat Helland Life Beyond Distributed Transactions, Pat Helland (2007) We Need To Model Uncertainty
  30. “An autonomus component can only promise its own behavior.” “Autonomy

    makes information local, leading to greater certainty and stability.” - Mark Burgess In Search of Certainty, Thinking in Promises - Mark Burgess
  31. Data on the inside vs Data on the outside -

    Pat Helland Inside Data Our current present ⇨ state
  32. Data on the inside vs Data on the outside -

    Pat Helland Inside Data Our current present ⇨ state Outside Data Blast from the past ⇨ Events/facts
  33. Data on the inside vs Data on the outside -

    Pat Helland Inside Data Our current present ⇨ state Outside Data Blast from the past ⇨ Events/facts Between Services Hope for the future ⇨ commands
  34. There Is No Now A system of microservices is a

    never ending stream towards convergence
  35. Requirements for a Sane Failure Model 1. Contained—Avoid cascading failures

    2. Reified—as Events 3. Signalled—Asynchronously 4. Observed—by 1-N 5. Managed—Outside failed Context Failures need to be
  36. You can use CRUD Together with Event Streams To get

    an internally consistent Materialized View
  37. Service B Service A You can use CRUD Together with

    Event Streams To get an internally consistent Materialized View
  38. Service B Service A CRUD You can use CRUD Together

    with Event Streams To get an internally consistent Materialized View CRUD
  39. Service B Service A TABLE A CRUD TABLE B You

    can use CRUD Together with Event Streams To get an internally consistent Materialized View CRUD
  40. Service B Service A TABLE A CRUD TABLE B You

    can use CRUD Together with Event Streams To get an internally consistent Materialized View CRUD
  41. Service B Service A TABLE A CRUD TABLE B You

    can use CRUD Together with Event Streams To get an internally consistent Materialized View CRUD Atomic Update & event
  42. Service B Service A TABLE A CRUD TABLE B You

    can use CRUD Together with Event Streams To get an internally consistent Materialized View CRUD Atomic Update & event
  43. Service C Service B Service A TABLE A CRUD TABLE

    B You can use CRUD Together with Event Streams To get an internally consistent Materialized View CRUD Atomic Update & event
  44. Service C Service B Service A TABLE A CRUD TABLE

    B You can use CRUD Together with Event Streams To get an internally consistent Materialized View CRUD Atomic Update & event
  45. Service C Service B Service A TABLE A CRUD TABLE

    B JOINS Table A Table B You can use CRUD Together with Event Streams To get an internally consistent Materialized View CRUD Atomic Update & event
  46. Service C Service B Service A TABLE A CRUD TABLE

    B JOINS Table A Table B You can use CRUD Together with Event Streams To get an internally consistent Materialized View CRUD Read Only Atomic Update & event
  47. Service C Service B Service A Eventual Consistency TABLE A

    CRUD TABLE B JOINS Table A Table B You can use CRUD Together with Event Streams To get an internally consistent Materialized View CRUD Read Only Atomic Update & event
  48. “Update-in-place strikes systems designers as a cardinal sin: it violates

    traditional accounting practices that have been observed for hundreds of years.” - jim Gray The Transaction Concept, Jim Gray (1981)
  49. “The truth is the log. The database is a cache

    of a subset of the log.” - Pat Helland Immutability Changes Everything, Pat Helland (2015)
  50. Happy Path Event Sourced Services 2) Create new Event (“PaymentApproved”)

    1) Receive and verify Command (“ApprovePayment”)
  51. Happy Path Event Sourced Services 2) Create new Event (“PaymentApproved”)

    1) Receive and verify Command (“ApprovePayment”) 3) Append Event to Event Log
  52. Happy Path Event Sourced Services 2) Create new Event (“PaymentApproved”)

    1) Receive and verify Command (“ApprovePayment”) 3) Append Event to Event Log 4) Update internal component state
  53. Happy Path Event Sourced Services 5) Run side-effects (approve the

    payment) 2) Create new Event (“PaymentApproved”) 1) Receive and verify Command (“ApprovePayment”) 3) Append Event to Event Log 4) Update internal component state
  54. SAD Path - recover from failure Happy Path Event Sourced

    Services 5) Run side-effects (approve the payment) 2) Create new Event (“PaymentApproved”) 1) Receive and verify Command (“ApprovePayment”) 3) Append Event to Event Log 4) Update internal component state
  55. SAD Path - recover from failure Happy Path Event Sourced

    Services 5) Run side-effects (approve the payment) 2) Create new Event (“PaymentApproved”) 1) Receive and verify Command (“ApprovePayment”) 3) Append Event to Event Log 4) Update internal component state 1) Rehydrate Events from Event Log
  56. SAD Path - recover from failure Happy Path Event Sourced

    Services 5) Run side-effects (approve the payment) 2) Create new Event (“PaymentApproved”) 1) Receive and verify Command (“ApprovePayment”) 3) Append Event to Event Log 4) Update internal component state 1) Rehydrate Events from Event Log 2) Update internal component state
  57. SAD Path - recover from failure Happy Path Event Sourced

    Services 5) Run side-effects (approve the payment) 2) Create new Event (“PaymentApproved”) 1) Receive and verify Command (“ApprovePayment”) 3) Append Event to Event Log 4) Update internal component state 1) Rehydrate Events from Event Log 2) Update internal component state Memory Image
  58. Event Sourcing ✴ One single Source of Truth with All

    history ✴ Allows for Memory Image (Durable In-Memory State)
  59. Event Sourcing ✴ One single Source of Truth with All

    history ✴ Allows for Memory Image (Durable In-Memory State) ✴ Avoids the Object-relational mismatch
  60. Event Sourcing ✴ One single Source of Truth with All

    history ✴ Allows for Memory Image (Durable In-Memory State) ✴ Avoids the Object-relational mismatch ✴ Allows others to Subscribe to state changes
  61. Event Sourcing ✴ One single Source of Truth with All

    history ✴ Allows for Memory Image (Durable In-Memory State) ✴ Avoids the Object-relational mismatch ✴ Allows others to Subscribe to state changes ✴ Has good Mechanical sympathy
 (Single Writer Principle etc.)
  62. ✴ Unfamiliar model ✴ Versioning of events ✴ Deletion of

    events (legal or moral reasons) Disadvantages Of Using Event Sourcing
  63. CQRS Read Side Model Write Side Model Events Events Read

    Data Store Write to Event Log Commands
  64. CQRS Read Side Model Write Side Model Events Queries Events

    Read Data Store Write to Event Log Commands
  65. CQRS Read Side Model Write Side Model Events Queries Events

    Read Data Store Write to Event Log Eventual Consistency Commands
  66. ✴ Temporal Decoupling ✴ Increased Resilience ✴ Increased Scalability ✴

    Allows for Multiple Event subscribers ✴ Allows for Polyglot Persistence Advantages Of Using CQRS
  67. ✴ More infrastructure to manage ✴ 2 data models (or

    more) to maintain ✴ Eventual Consistency Disadvantages Of Using CQRS
  68. “Modelling events forces you to have a 
 temporal focus

    on what’s going on in the system. Time becomes a crucial factor of the system.” - Greg Young A Decade of DDD, CQRS, Event Sourcing, Greg Young (Presentation from 2016)
  69. ✴ Event is a snapshot in time ✴ Event ID

    is an index for time ✴ Event Log is our full history The database of Our past The path to Our present Event Sourcing Allows Us To Model Time
  70. Event Sourcing Allows For Time Travel ✴Replay the log for

    historic debugging ✴Replay the log for auditing & traceability ✴Replay the log on failure ✴Replay the log for replication
  71. Key Takeaways Events-First design helps you to: ✴ reduce risk

    when modernizing applications ✴ Move Faster towards a Resilient and Scalable architecture ✴ Design autonomous services ✴ Balance Certainty and Uncertainty
  72. Key Takeaways Events-First design helps you to: ✴ reduce risk

    when modernizing applications ✴ Move Faster towards a Resilient and Scalable architecture ✴ Design autonomous services ✴ Balance Certainty and Uncertainty Event Logging allows you to: ✴ AVOID CRUD and ORM ✴ take control of your system’s history ✴ time-travel ✴ Balance Strong and eventual consistency