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

Embracing Event Sourcing with Akka

Embracing Event Sourcing with Akka

Event Sourcing is too hard, nobody understands it! Event Sourcing is super easy, it’s straightforward, and simple to understand! This is what developers say about Event Sourcing, how can there be such a big difference in these opinions?

Event Sourcing is a game-changer with dramatic benefits. It lets you model your business in terms of events (facts) instead of just state. Applying Event Sourcing is a journey where you learn how to think differently about modelling and how to deal with consistency and transactions in distributed systems. Understanding its principles and rules is fundamental to reach that Aha! moment that everybody is talking about.

In this talk Renato will walk you through a few patterns and techniques to apply when writing an event sourced application with the building blocks provided by the Akka Platform.

Renato Cavalcanti

November 05, 2021
Tweet

More Decks by Renato Cavalcanti

Other Decks in Programming

Transcript

  1. Who was I 10 years ago? • Java developer •

    Enterprise one • Object Oriented Developer • Spring / JPA / Hibernate • @Transactional 2
  2. Domain-Driven Design, the Book 3 • Eric Evans • Tackling

    Complexity in the Heart of Software • Appeared in 2003 • Aggregates and many other concepts • Aggregate - transactional boundary • Aggregate - the Guardian
  3. Event Sourcing and CQRS 4 • Event Sourcing main idea

    is to persist events (facts) and then derive all state from the persisted Events, the source of your state are the events • CQRS stands for Command Query Responsibility Segregation • CQRS: observation that our needs to validate and write data are not the same as our needs to read data. Hence the segregation. • Event Sourcing and CQRS are two distinct things, but they are complementary and fit very well together
  4. Event Sourcing - write-side 15 • receive a command •

    instantiate/load aggregate • apply command (validation) • generate events (persist) • apply events (after persisting) • on new command, start over again • on re-instantiating, apply existing events before handling new command
  5. How does Akka help you with this? 16 • One

    command at a time • Aggregate isolation • Akka Persistence - write-side • plugins - save events and snapshots • Akka Projections - read-side • consume from journal and track offsets
  6. In picture - the read-side 17 Evt:ParticipantAdded(name:String) Evt:RaffleCreated(prize: String) Evt:ParticipantAdded(name:String)

    Evt:ParticipantAdded(name:String) Evt:WinnerSelected(name:String) • generate views • statistics reports • publish to other systems, via broker • consume one by one and needs to track where you stopped
  7. => How powerful is that? 22 Event Sourced Microservices Many

    services, indirectly dependent. Broker
  8. => How powerful is that? 23 Event Sourced Microservices Many

    services, indirectly dependent. Broker
  9. The Golden Hammer 24 • Event Sourcing is cool •

    Let’s start with it tomorrow Sometimes old CRUD is all you need. Use good judgment
  10. The Fake Aggregate 25 • Accept all commands without validation

    • Commands are transformed to Events and persisted 
 (often verbatim copies) • Has zero business logic Just persist incoming data to some storage
  11. Phone a Friend 26 • Aggregates has no friends •

    Aggregates are self-confident • Aggregates only operate on their own internal state and incoming commands data Call a friend from outside. 
 Enrich the command instead.
  12. // a participant can only subscribe once // after the

    drawing is done, subscriptions must be rejected sealed trait Raffle case object EmptyRaffle extends Raffle case class OpenRaffle(participantIds: Set[String]) extends Raffle case object FinishedRaffle extends Raffle sealed trait Command case class CreateRaffle(prize: String) extends Command case class AddParticipant(participantId: String) extends Command case object Draw extends Command sealed trait Event case class RaffleCreated(prize: String) extends Event case class ParticipantAdded(participantId: String) extends Event case class WinnerSelected(participantId: String) extends Event All you can eat 27
  13. The Good, the Bad and the Ugly 28 The Good

    The Bad The Ugly Events that are meaningful to the business: RaffleCreated, ParticipantAdded, ParticipantRemoved, WinnerSelected Over designed events: ParticipantRejected (looks more like an exception, no?) Completely missing the point: RaffleUpdated(participantIds: Set[String], winner: Option[String])
  14. Taming your Event Sourced Microservices 29 • Embracing the pattern,

    learn it, know it • Follow the principles, apply discipline • Aggregates as transactional boundaries • Event Handlers are pure functions • Command handlers highly recommended as pure functions as well • Linear • When diverging, make sure you have a very good reason for it
  15. That’s all folks, thank you! 30 Renato Cavalcanti [email protected] @octonato

    • Akka Platform Guide 
 https://www.lightbend.com/akka-platform 
 https://developer.lightbend.com/docs/akka-platform-guide/ • Akka Serverless 
 https://www.lightbend.com/akka-serverless 
 https://developer.lightbend.com/docs/akka-serverless/