“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:
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 Withdraw
ing them from the source and Deposit
ing 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:
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:
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. ;)