Persistent serverless state machines with XState and Restate
XState is a fantastic JavaScript framework for building state machines. You can do this with a DSL that describes states and state transitions, or with Stately Studio which allows you to build visually and export to code. XState is particularly popular for modelling frontend state, where each page load starts out with a fresh state machine that is updated by user input and HTTP responses. To make it possible to create state machines that span across time, XState integrates directly into the JS event loop, allowing you to have delayed transitions, or even react to a Promise (itself a form of asynchronous state machine). However, this requires your code to be running continuously and store state in memory, which means it can be tricky to move state machines to the backend.
Restate is a new kind of durable execution engine that has the goal of making it easy to turn straightforward imperative code into fault-tolerant code that runs as part of a distributed system. For JS users, it might seem a little like a distributed and persistent event loop - implementing its own ways to reliably schedule work and react to events, without your code needing to run all the time. That got us thinking - could we run XState on top of the Restate event loop? Our goal is to create ‘virtual state machines’ that wake up, react to events, and then go back to sleep, storing their state durably so they can operate indefinitely and across transient failures.