Simplifying an event sourced application

May 01, 2018 0 Comments

Simplifying an event sourced application

 

 



Every time you make a change to the application state, you record the change as an event.

You can replay the events since the beginning of the recording, up to a certain time. Then you've recreated the state of the application at that time.

That's what Event Sourcing is about. It's like you can time travel to the past. I find it fascinating.

Event sourcing provides an audit trail when you need to meet regulatory requirements. It can help with debugging. And you can even explore alternate realities: what would have happened if...

I recently saw a great talk by Jakub Pilimon and Kenny Bastani about event sourcing.

The talk is a 1 hour session of life coding. The two speakers start with a simple application that is not event sourced. Then they refactor it to use events.

They end up wiring the application up with Apache Kafka. I will skip that part in this article and focus on the conceptual part of event sourcing instead.

A recap of the talk

As a user of a Credit Card management application, you can:


  • Assign a limit to the credit card

  • Withdraw money

  • Repay money

For each of these commands, there is a method in the CreditCard class.

Here's the original code of the assignLimit method:

public void assignLimit(BigDecimal amount) { if(limitAlreadyAssigned()) { throw new IllegalStateException(); } this.initialLimit = amount; } 

Here's the withdraw method:

 public void withdraw(BigDecimal amount) { if(notEnoughMoneyToWithdraw(amount)) { throw new IllegalStateException(); } if(tooManyWithdrawalsInCycle()) { throw new IllegalStateException(); } this.usedLimit = usedLimit.add(amount); withdrawals++; } 

The repay method is similiar.

Remember that for event sourcing, you need to record an event any time the application changes its state?


So the speakers extract each state change to its own method in the CreditCard class.

Here's the refactored withdraw method:

 public void withdraw(BigDecimal amount) { if(notEnoughMoneyToWithdraw(amount)) { throw new IllegalStateException(); } if(tooManyWithdrawalsInCycle()) { throw new IllegalStateException(); } cardWithdrawn(new CardWithdrawn(uuid, amount, Instant.now())); } private CreditCard cardWithdrawn(CardWithdrawn event) { this.usedLimit = usedLimit.add(event.getAmount()); withdrawals++; pendingEvents.add(event); return this; } 

An instance of CardWithdrawn represents the event that a user has successfully withdrawn money. After the state has changed, the event is added to the list of pending events.

You call the save method of the CreditCardRepository class to flush the pending events to the event stream. Event listeners may handle the events then.

Apart from the payload, each event has its own unique identifier and timestamp. So you can sequence and replay the events later.

To replay the events for a specific credit card, the repository calls the recreateFrom method of the CreditCard class, passing in the id of the card and the events stored for it:

 public static CreditCard recreateFrom(UUID uuid, List<DomainEvent> events) { return ofAll(events).foldLeft(new CreditCard(uuid), CreditCard::handle); } private CreditCard handle(DomainEvent event) { return Match(event).of( Case($(Predicates.instanceOf(LimitAssigned.class)), this::limitAssigned), Case($(Predicates.instanceOf(CardWithdrawn.class)), this::cardWithdrawn), Case($(Predicates.instanceOf(CardRepaid.class)), this::cardRepaid), Case($(Predicates.instanceOf(CycleClosed.class)), this::cycleWasClosed) ); } 

This code uses the vavr.io library to call the handle method for each event. The handle method dispatches the event object to the appropriate method.

For example: for each LimitAssigned event, the handle method calls the limitAssigned method with the event as parameter.

Simplifying the application

I used the requirements as code library for simplifying the code. First, I put all of the event classes and the handling methods in a model. Like this:

this.eventHandlingModel = Model.builder() .handles(LimitAssigned.class).with(this::limitAssigned) .handles(CardWithdrawn.class).with(this::cardWithdrawn) .handles(CardRepaid.class).with(this::cardRepaid) .handles(CycleClosed.class).with(this::cycleWasClosed) .build(); 

I had to change the return type of the handling methods (e.g. limitAssigned) to void. Apart from that, the conversion from vavr.io was straight forward.

Then, I created a runner and started it for the model:

this.modelRunner = new ModelRunner(); 
modelRunner.run(eventHandlingModel);

After that, I changed the recreateFrom and handle methods to this:

public static CreditCard recreateFrom(UUID uuid, List<DomainEvent> events) { CreditCard creditCard = new CreditCard(uuid); events.forEach(ev -> creditCard.handle(ev)); return creditCard; 
} private void handle(DomainEvent event) { modelRunner.reactTo(event);
}

At that point, I could get rid of the dependency to vavr.io.

Transition complete. Now I could get some more simplifying done.

I revisited the withdraw method:

public void withdraw(BigDecimal amount) { if(notEnoughMoneyToWithdraw(amount)) { throw new IllegalStateException(); } if(tooManyWithdrawalsInCycle()) { throw new IllegalStateException(); } cardWithdrawn(new CardWithdrawn(uuid, amount, Instant.now())); 
}

The check tooManyWithdrawalsInCycle() didn't depend on the data of the event. It only depended on the state of the CreditCard.

State checks like this can be represented in the model as when conditions.

After I moved all state checks for all methods to the model, it looked like this:

this.eventHandlingModel = Model.builder() .when(this::limitNotAssigned) .handles(LimitAssigned.class).with(this::limitAssigned) .when(this::limitAlreadyAssigned) .handles(LimitAssigned.class).with(this::throwsException) .when(this::notTooManyWithdrawalsInCycle) .handles(CardWithdrawn.class).with(this::cardWithdrawn) .when(this::tooManyWithdrawalsInCycle) .handles(CardWithdrawn.class).with(this::throwsException) .handles(CardRepaid.class).with(this::cardRepaid) .handles(CycleClosed.class).with(this::cycleWasClosed) 
.build();

For this to work, I needed to replace the direct calls to methods that change the state with the handle method. After that, the assignLimit and withdraw methods looked like this:

public void assignLimit(BigDecimal amount) { handle(new LimitAssigned(uuid, amount, Instant.now())); 
} private void limitAssigned(LimitAssigned event) { this.initialLimit = event.getAmount(); pendingEvents.add(event);
} public void withdraw(BigDecimal amount) { if(notEnoughMoneyToWithdraw(amount)) { throw new IllegalStateException(); } handle(new CardWithdrawn(uuid, amount, Instant.now()));
} private void cardWithdrawn(CardWithdrawn event) { this.usedLimit = usedLimit.add(event.getAmount()); withdrawals++; pendingEvents.add(event);
}

As you can see, most of the conditional logic has moved out of the methods into the model. This makes the methods easier to understand.

One thing that bothered me is that you must not forget to add the event to the pending events. Every time. Or your code won't work.

Requirements as code allows you to control how the system handles the events. So I extracted pendingEvents.add(event) from the methods as well:

modelRunner.handleWith(this::addingPendingEvents); 
... public void addingPendingEvents(StandardEventHandler eventHandler) { eventHandler.handleEvent(); DomainEvent domainEvent = (DomainEvent)eventHandler.getEvent(); pendingEvents.add(domainEvent);
}

I could have gone further and extract the validation logic as well.

But I leave that as a thought exercise to you, dear reader.

What's the result? What's the point?

You can do a comparison between the original version and the refactored version of the CreditCard class:

Original version

Refactored version

What I tried to achieve is a clear separation of concerns:


  • The state dependent execution of methods is defined in the model

  • The data validation and state changes are in the implementations of the methods

  • The events are automatically added to the pending events. In general: the infrastructure code is clearly separated from the business logic.

Simplifying an example that is already very simple is good for explaining.

But that's not the point I want to make.

The point is: having such a clear separation of concerns pays out in practice.

Especially, if you work with multiple teams. On complicated problems.

Separation of concerns helps with changing different parts of code at a different pace. You have simple rules where to find something. The code is easier to understand. And it's easier to isolate units for testing purposes.

Conclusion

I hope you enjoyed my article. Please give me feedback in the comments.

Have you been working on event sourcing applications? What were your experiences like?


Can you relate to what I wrote in this article?

I also want to invite you to look at my library that I used throughout the article. I would be thrilled if you try it out in practice, and tell me what you think.


Tag cloud