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

Event Sourcing architecture using Elixir

Event Sourcing architecture using Elixir

Talk at "Nerdzão #37 - Back-End".

Video: https://youtu.be/tixWGkkZbXE
Projects used on this talk: https://github.com/macabeus/event-sourcing-example
My blog: http://macalogs.com.br/

Bruno Macabeus

March 14, 2018
Tweet

More Decks by Bruno Macabeus

Other Decks in Technology

Transcript

  1. “The core idea of event sourcing is that whenever we

    make a change to the state of a system, we record that state change as an event, and we can confidently rebuild the system state by reprocessing the events at any time in the future.” - Martin Fowler youtu.be/STKCRSUsyP0 bit.ly/martin-event-driven
  2. This architecture has many interesting benefits! Event log provides a

    strong audit capability We can recreate historic states by replaying the event log up to a point
  3. This architecture has many interesting benefits! We can explore alternative

    histories by injecting hypothetical events when replaying Event log provides a strong audit capability We can recreate historic states by replaying the event log up to a point
  4. This architecture has many interesting benefits! Event sourcing makes it

    plausible to have non-durable working copies, such as a Memory Image. We can explore alternative histories by injecting hypothetical events when replaying Event log provides a strong audit capability We can recreate historic states by replaying the event log up to a point
  5. This architecture has many interesting benefits! Event sourcing makes it

    plausible to have non-durable working copies, such as a Memory Image. We can explore alternative histories by injecting hypothetical events when replaying Event log provides a strong audit capability We can recreate historic states by replaying the event log up to a point
  6. User Server Current state: Events log: 0 In this example,

    the user “creates an event”. But, on the others implementations of this architecture, another part could create the events
  7. User Server Current state: Events log: 0 Event “arrives" on

    the server. On the others implementations, the server could “create" the event
  8. User Server Current state: Events log: -1 1 +1 +1

    It’s a non-deterministic event
  9. User Server Current state: Events log: -1 5 +1 +1

    +4 Now the events log has too many events
  10. User Server Current state: Events log: -1 5 +1 +1

    +4 Snapshot: state 5 This is an aggregate. That is, a stream of events
  11. User Server Current state: Events log: -1 ?? +1 +1

    +4 +1 Snapshot: state 5 What if we lose the state?
  12. User Server Current state: Events log: -1 ?? +1 +1

    +4 +1 Snapshot: state 5 We could run again the events to restore the previous state! Using the events, we could convert the stream of events into a structural representation. This process is called “projection".
  13. Hey! It’s important the events be agnostic of a language,

    then you’ll can work using another languages on same project. Flux Standard Action is an example of solution about it. I didn't do it in my example. My example works only on Elixir
  14. - An in-memory database, Mnesia, using the wrapper Amnesia In

    this project, I used two databases: - A storage file database, DETS
  15. - An in-memory database, Mnesia, using the wrapper Amnesia In

    this project, I used two databases: - A storage file database, DETS Both, Mnesia and DETS, are in OTP! That is, built-in on Erlang/Elixir!
  16. CQRS: split the responsibility of "read" and “write" on the

    database. Reporting database: a database optimized for read. If a database is optimized for read, then could has redundancy and multiples databases to query.
  17. CQRS: split the responsibility of "read" and “write" on the

    database. Reporting database: a database optimized for read. If a database is optimized for read, then could has redundancy and multiples databases to query. And, applying it with event sourcing: we could has a database that centralize all events received by the system (“event storage”), and many small databases optimized to read. This both reads the same bus and be updates when a new event is forwarded on the bus.
  18. CQRS and Reporting database aren’t exclusive for event sourcing. But,

    it’s useful when we are using event sourcing. !
  19. A projection is the transformation of a set of data

    from one form into another. In the case of event sourcing architecture, we convert a stream of events into any structural representation.
  20. A projection is the transformation of a set of data

    from one form into another. In the case of event sourcing architecture, we convert a stream of events into any structural representation. -1 +1 +1 +4
  21. Life cycle of an event: VALIDATION Validations: checks that input

    makes sense and objects are properly suited for further actions. (this event is to me?)
  22. Life cycle of an event: VALIDATION CONSEQUENCE Validations: checks that

    input makes sense and objects are properly suited for further actions. (this event is to me?) Consequences: initiating some action that will change the state of the world (I need forward a new event?)
  23. Life cycle of an event: VALIDATION CONSEQUENCE DERIVATION Validations: checks

    that input makes sense and objects are properly suited for further actions. (this event is to me?) Consequences: initiating some action that will change the state of the world (I need forward a new event?) Derivations: figuring out some information based on information we already have (I created a token and I’ll send it to user)
  24. Life cycle of an event: VALIDATION CONSEQUENCE DERIVATION Validations: checks

    that input makes sense and objects are properly suited for further actions. (this event is to me?) Consequences: initiating some action that will change the state of the world (I need forward a new event?) Derivations: figuring out some information based on information we already have (I created a token and I’ll send it to user) Using Eager Read Derivation, this is done async between events. It’s useful because it otimize the system, but we’ll have eventual inconsistencies.
  25. User Event storage Create an account List accounts Send a

    validate e-mail Validate an account User can send an event on bus, and many process manager (PM) reads this bus…
  26. User Event storage Create an account List accounts Send a

    validate e-mail Validate an account User can send an event on bus, and many process manager (PM) reads this bus. Maybe a PM could send a new event on the bus.
  27. User Event storage Create an account Send a validate e-mail

    List accounts Validate an account User can send an event on bus, and many process manager (PM) reads this bus. Maybe a PM could send a new event on the bus. And, the PM needs read events in Event Storage, to re-build when it loses the data.
  28. User Event storage Create an account Send a validate e-mail

    List accounts Validate an account Events processing are asynchronous! User can send an event on bus, and many process manager (PM) reads this bus. Maybe a PM could send a new event on the bus. And, the PM needs read events in Event Storage, to re-build when it loses the data. And everything could be async!
  29. User Event storage Create an account Send a validate e-mail

    List accounts Validate an account Events processing are asynchronous! User can send an event on bus, and many process manager (PM) reads this bus. Maybe a PM could send a new event on the bus. And, the PM needs read events in Event Storage, to re-build when it loses the data. And everything could be async!
  30. User Event storage Create an account Send a validate e-mail

    List accounts Validate an account Events processing are asynchronous! User can send an event on bus, and many process manager (PM) reads this bus. Maybe a PM could send a new event on the bus. And, the PM needs read events in Event Storage, to re-build when it loses the data. And everything could be async!
  31. User Event storage Create an account Send a validate e-mail

    List accounts Validate an account Events processing are asynchronous! User can send an event on bus, and many process manager (PM) reads this bus. Maybe a PM could send a new event on the bus. And, the PM needs read events in Event Storage, to re-build when it loses the data. And everything could be async!
  32. User Event storage Create an account Send a validate e-mail

    List accounts Validate an account Events processing are asynchronous! User can send an event on bus, and many process manager (PM) reads this bus. Maybe a PM could send a new event on the bus. And, the PM needs read events in Event Storage, to re-build when it loses the data. And everything could be async!
  33. User Event storage Create an account Send a validate e-mail

    List accounts Validate an account Events processing are asynchronous! User can send an event on bus, and many process manager (PM) reads this bus. Maybe a PM could send a new event on the bus. And, the PM needs read events in Event Storage, to re-build when it loses the data. And everything could be async!
  34. User Event storage Create an account Send a validate e-mail

    List accounts Validate an account Events processing are asynchronous! User can send an event on bus, and many process manager (PM) reads this bus. Maybe a PM could send a new event on the bus. And, the PM needs read events in Event Storage, to re-build when it loses the data. And everything could be async!
  35. User Event storage Create an account Send a validate e-mail

    List accounts Validate an account Events processing are asynchronous! User can send an event on bus, and many process manager (PM) reads this bus. Maybe a PM could send a new event on the bus. And, the PM needs read events in Event Storage, to re-build when it loses the data. And everything could be async!
  36. User Event storage Create an account Send a validate e-mail

    List accounts Validate an account Events processing are asynchronous! User can send an event on bus, and many process manager (PM) reads this bus. Maybe a PM could send a new event on the bus. And, the PM needs read events in Event Storage, to re-build when it loses the data. And everything could be async!
  37. User Event storage Create an account Send a validate e-mail

    List accounts Validate an account Events processing are asynchronous! User can send an event on bus, and many process manager (PM) reads this bus. Maybe a PM could send a new event on the bus. And, the PM needs read events in Event Storage, to re-build when it loses the data. And everything could be async!
  38. User Event storage Create an account Send a validate e-mail

    List accounts Validate an account Events processing are asynchronous! User can send an event on bus, and many process manager (PM) reads this bus. Maybe a PM could send a new event on the bus. And, the PM needs read events in Event Storage, to re-build when it loses the data. And everything could be async!
  39. User Event storage Create an account Send a validate e-mail

    List accounts Validate an account Events processing are asynchronous! User can send an event on bus, and many process manager (PM) reads this bus. Maybe a PM could send a new event on the bus. And, the PM needs read events in Event Storage, to re-build when it loses the data. And everything could be async!
  40. User Event storage Create an account Send a validate e-mail

    List accounts Validate an account Events processing are asynchronous! User can send an event on bus, and many process manager (PM) reads this bus. Maybe a PM could send a new event on the bus. And, the PM needs read events in Event Storage, to re-build when it loses the data. And everything could be async!
  41. User Event storage Create an account Send a validate e-mail

    List accounts Validate an account Events processing are asynchronous! User can send an event on bus, and many process manager (PM) reads this bus. Maybe a PM could send a new event on the bus. And, the PM needs read events in Event Storage, to re-build when it loses the data. And everything could be async!
  42. User Event storage Create an account Send a validate e-mail

    List accounts Validate an account Events processing are asynchronous! User can send an event on bus, and many process manager (PM) reads this bus. Maybe a PM could send a new event on the bus. And, the PM needs read events in Event Storage, to re-build when it loses the data. And everything could be async!
  43. User Event storage Create an account Send a validate e-mail

    List accounts Validate an account Events processing are asynchronous! User can send an event on bus, and many process manager (PM) reads this bus. Maybe a PM could send a new event on the bus. And, the PM needs read events in Event Storage, to re-build when it loses the data. And everything could be async!
  44. User Event storage Create an account Send a validate e-mail

    List accounts Validate an account Events processing are asynchronous! User can send an event on bus, and many process manager (PM) reads this bus. Maybe a PM could send a new event on the bus. And, the PM needs read events in Event Storage, to re-build when it loses the data. And everything could be async!