Moving clams (?) with events

“Why ‘clams’?” you ask? Because spam filters eat the domain language we’d normally use in this email for breakfast. So, when you read “clam” think “thing we use to exchange goods and services.”

All right, continuing with Daniel’s question from yesterday, transferring clams from one, um, tank to another. You might start with a typical command/event pair. We’re transferring, so Transfer/Transferred seems reasonable:

funds-transfer-step-1.png

We give the transfer an ID. Transfers go from one place to another, so we have the identifiers for those two places. Skip the transaction IDs for the moment. We have the number of clams we’re moving, amount, and some time fields.

Great, so how would we actually handle Transfer to produce Transferred? You move clams by Withdrawing them from the source and Depositing them into the destination, otherwise known as two distinct, non-atomic steps. You don’t want to do non-atomic things in the same handler if you can avoid it because you could crash between the steps, losing your weekend debugging it.

Fine, we’ll model those two distinct steps as well:

funds-transfer-step-2.png

Note that Withdrawn and Deposited are steps in our transfer process. We may collaborate with components that have events with similar names and data, but these are steps in the transfer process that belong to the transfer process.

If we only wrote these events to our transfer stream, the relevant accounts wouldn’t get updated. So, we coordinate with account-component, sending it the commands it defines:

funds-transfer-step-3.png

We send account-component its commands and observe the resulting events to know when it has completed that leg of our transfer. At a tactical level, account-component won’t need to change at all to be part of a transfer, and we’ll cover why probably two days from now.

Not quite there yet though. There’s a flaw in our design.

If you recall from our series on idempotence, networks fail, running components get restarted, etc. We will handle our Transfer command more than once, what we call recycling a message.

Take action

An exercise for the reader! What will happen when we recycle Transfer? If you want a hint, notice withdrawalId on Withdraw. Where did that come from? Where does it need to come from?

Yes, you could look at the diagram from yesterday’s email, but see if you can articulate the issue in your mind and come up with a solution. Then articulate both in a response to this email. ;)


Like this message? I send out a short email each day to help software development leaders build organizations the deliver value. Join us!


Get the book!

Ready to learn how to build an autonomous, event-sourced microservices-based system? Practical Microservices is the hands-on guidance you've been looking for.

Roll up your sleeves and get ready to build Video Tutorials, the next-gen web-based learning platform. You'll build it as a collection of loosely-coupled autonomous services, developing a message store interface along the way.

When you're done, you'll be ready to contribute to microservices-based projects.

In ebook or in print.