Skip to main content

The Challenge

Let's start out with a common money transfer problem. Let's say that you need to write some code that handles basic reimbursement. It should do the following:

  • Withdraw a set amount from the company bank
  • Deposit that amount into the employee bank
  • Let the user know that the reimbursement was successfully completed

But with distributed systems, a lot of failures can occur. For example, what if the withdrawal succeeds but the network crashes before the deposit? Not only would the service go down, but you could also lose the code’s state. If you try to rerun it, you might end up with two withdrawals but only one deposit.

Let's change this code into a durable Workflow.

Basic Reimbursement Code
public interface ReimbursementService{
String reimbursementWorkflow(String userId, double amount);
}

public class ReimbursementServiceImpl implements ReimbursementService{
@Override
public String reimbursementWorkflow(String userId, double amount) {
// Insert code that withdraws money from company bank
// Insert code that deposits money into employee bank
return String.format("reimbursement to %s successfully complete", userId);
}
}
2 / 9