r/AskProgramming • • Jun 24 '26

Other FSM Persistence Question

Hi everyone,

I'm working on implementing a MFA login flow utilizing a finite state machine, and have a question about persisting the state machine to a database in order to survive an API crash. Lets say I have the following states, which are a subset of the overall machine

  1. Send OTP SMS
  2. Wait For OTP Input
  3. Mint Session

Directly after the "Send OTP SMS" state, we automatically transition into the "Wait For OTP Input" state. On invalid input, we stay in that state, and on valid input, we go to "Mint Session" state. Now that the machine is defined, let me explain. my issue with persistence

In order to persist a state machine state, it is typically advised that the "work" done by a state must be done in a database transaction alongside the persistence of the FSM so that there are no data inconsistencies. Redhat advises this in this article, outlined in the first bullet underneath "Implementing State Machines". I have interpreted this to mean that the state machine should persist the state the machine is transitioning into, because upon completion of the database transaction the current state is completed. Once it is completed if the application crashes, we want to restore it in the state we would have transitioned into.

In order to persist the state machine and send the SMS OTP atomically, we will have to use the transactional outbox pattern to write a message to the database for sending the OTP SMS. In the "Send OTP SMS" state, we will open a database transaction, write the outbox message and the "Wait For OTP Input" state, and commit the transaction. The state machine will then transition into the "Wait For OTP Input" state, and all is good. The only issue with this is that the state "Send OTP SMS" itself is determining, outside of the rules of the state machine, that the next state will be "Wait For OTP Input" by writing this to the database. From the literature that I've read, it seems as though states themselves should not have context of what the state-to-be is, but rather should be isolated and focus on any tasks that must be completed in the state itself. If this is the case, how can we achieve full atomicity with the persistence of a state machine and the completion of work in such a way that we can consistently restore state machines from the database during a crash at any point in time?

If it helps, I'm particularly looking at using XState as the state machine implementation to drive the process. Thank you in advance

4 Upvotes

10 comments sorted by

View all comments

Show parent comment

1

u/Cadnerak Jun 25 '26

I see, so you think the states should more-so represent resting states, while the transitions are actually what does the work. So for example we have a resting states called “Waiting to send OTP SMS”. On event “Sending OTP SMS” the machine transitions into “OTP SMS sent”. The transition will persist the new state and otp sms message to the outbox, and if this fails we simply return to the “Waiting to send OTP SMS” state right? I think that would work if I’m understanding correctly. Is it typical that states of a machine represent “idle” states or can be represented with past tense naming like “Sent OTP SMS” or “Minted Session” rather than “Sending OTP SMS” and “Minting Session”?

1

u/balefrost Jun 26 '26

Like many things, it can be a little loosey goosey. States do generally represent resting states. But a state could represent an ongoing activity.

For example, in a FSM representing an OS process, we might have a "running" state. Clearly a lot is actively happening while the process is in the "running" state. But from the state machine's point of view, it is "resting" until something causes it to transition (e.g. the process self-terminates or the user force-terminates the process).

But it's not like the state machine police will come arrest you if you model things differently. You can name the states whatever you want. If you would prefer to associate action with the states instead of the transitions, that's fine. There's a reason that many state machine models allow that.

I was mostly just trying to get you to look at it from a different perspective.

1

u/Cadnerak Jun 26 '26

Yes I get it, thanks!