And run workflow, we'll just run the workflow again from the beginning. But each time the workflow gets to a step, it'll see that the step has already been checkpointed. And instead of re-executing the step, it will return the step's checkpointed output. And by doing this over and over again, the workflow will eventually recover, it'll eventually get to the first step that doesn't have a checkpoint and resume normal execution from that point. So in other words, our workflow will recover from the last completed step by following this procedure. Great.
So now we can see roughly what our library looks like. One last thing I want to show you is a diagrammatic process of what this library might do when applied to a real application. So we're going to look at an example of a workflow. So a checkout service, a simplified checkout service of the sort that might be, that you might run when you click the buy button on a website. So this workflow would be implemented as a JavaScript function. And each of these steps like create order, reserve inventory, process payment, would also be JavaScript functions. So when we run this workflow, we call run workflow.
The first thing we do is checkpoint the inputs. So we'll write a record to the database containing the workflow inputs, the unique ID, and mark the workflow with a pending status. Then once we've written that checkpoint, we can start actually executing the workflow. So we create an order, we reserve inventory, we process payment, and after each step, we checkpoint the steps outcome. Then let's say something breaks. We have just processed payment, and suddenly our server fails. And our process isn't running anymore. Normally, that would be pretty bad because our customers paid for something, and they're expecting to get their order fulfilled, but we don't know that because our process just crashed.
Recovering that might be tricky, but we have durable workflows, so it's much easier. So when our server resumes, it'll look at the database and see that there's this workflow that's in a pending state, so it needs to be recovered. It'll then recover the workflow, looking up each of its steps, and returning the checkpointed outcome. So it'll see that the create order, reserve inventory, and process payment steps were all successful. So it'll fast forward past them, reload from their checkpoints, and now re-execute them. Then finally, we'll get to the first, not yet completed step, fulfill order. We'll run it, we'll fulfill the order, we'll make the customer happy, and then we'll mark the workflows as success, and we'll do that all without re-executing anything, without double preserving inventory, or double charging the customer.
Comments