The Challenge
Let's start out with a basic reimbursement function that:
- Withdraws a set amount from the company bank
- Deposits that amount into the employee bank
- Lets 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 function into a durable Workflow.
Basic Reimbursement Function
export async function reimbursementWorkflow(userId: string, amount: number): Promise<string> {
// Insert code that withdraws money from company bank
// Insert code that deposits money into employee bank
return `reimbursement to ${userId} successfully complete`;
}