Quick revision
The short answer
Spring Statemachine models a lifecycle as states and events, usually enums, and transitions configured in a StateMachineConfigurerAdapter, such as withExternal().source(NEW).target(PAID).event(PAY). Guards decide whether a transition may happen and actions run when it does. We create one machine per order from a StateMachineFactory, and between requests we persist the state, either with the persistence support or by storing only the current state in the order row and rehydrating a machine for each request.
What to remember
In this answer, we saw the vocabulary of Spring Statemachine: enum states and events, transitions declared in a configurer adapter, guards that can refuse a transition and actions that run with it. We created one machine per order from a StateMachineFactory instead of sharing a singleton, persisted state either through the persister support or by storing the current state in the order row and rehydrating a machine per request, rejected events the machine denies, and noted that a plain enum with an allowed-transition map is often enough.
Your turn
Answer it aloud as you would in the interview, or type your answer below. Then show one part at a time and compare.
Spring Statemachine models a lifecycle as states and events, usually enums, and transitions configured in a StateMachineConfigurerAdapter, such as withExternal().source(NEW).target(PAID).event(PAY). Guards decide whether a transition may happen and actions run when it does. We create one machine per order from a StateMachineFactory, and between requests we persist the state, either with the persistence support or by storing only the current state in the order row and rehydrating a machine for each request.
Why this question is asked
Order lifecycles look simple until the rules pile up: an order may be cancelled only before shipping, payment must match the total, and a notification must go out on every change. Scattered if statements stop scaling, and a state machine gives the rules one home. Interviewers ask about Spring Statemachine to see whether we know its vocabulary, its main pitfall of sharing one machine, and how state survives between HTTP requests. In this answer, we will recap the concepts, configure the order lifecycle, add a guard and an action, create machines per order, persist them, and compare with a plain enum.
1A Quick Recap of State Machine Terms
A state is a situation the order can be in: NEW, PAID, SHIPPED or CANCELLED. An event is something that happens to the order from outside: PAY, SHIP or CANCEL. A transition says that in a given source state, a given event moves the order to a target state. A guard is a condition checked before a transition, and if it returns false the transition does not happen. An action is code that runs as part of a transition, such as sending an email.
The value of writing these down as a machine is that the allowed moves are declared in one place. Any event that has no transition from the current state is simply not accepted, so SHIP on a NEW order cannot happen by accident.
| Source | Event | Target | Guard / action |
|---|---|---|---|
| NEW | PAY | PAID | guard: amount matches; action: notify customer |
| PAID | SHIP | SHIPPED | action: notify customer |
| NEW | CANCEL | CANCELLED | none |
| PAID | CANCEL | CANCELLED | action: start refund |
2Configuring the Machine
States and events are enums, and the configuration class extends EnumStateMachineConfigurerAdapter, the enum flavour of StateMachineConfigurerAdapter. One method declares the states, the other the transitions.
Notice @EnableStateMachineFactory rather than @EnableStateMachine. The second creates a single machine bean, and we will see in a moment why that is the wrong shape for orders.
3Guards and Actions
A guard receives a StateContext, which gives access to the event, its message headers and the machine's extended state, a map of variables the machine carries. An action receives the same context and does the side effect.
Keep guards free of side effects, because the machine may evaluate them while it decides which transition applies. Keep actions small and delegate to services, so the business logic stays testable without the machine.
4One Machine per Order
A state machine holds the current state of one thing. If we inject a single machine bean and use it for every order, order 7 paying would move the shared machine to PAID, and order 8, still new, would suddenly be PAID too. Under concurrent requests it gets worse, because two threads change the same machine.
So we inject StateMachineFactory and call getStateMachine(orderId) to get a fresh machine for each order we handle. A machine is cheap to create, and it lives only for the duration of the request.
5Persisting State Between Requests
A machine lives in memory, but an order lives for days. We have two common approaches. The project's persistence support, a StateMachinePersister with a JPA, Redis or MongoDB repository, saves the whole machine context, including extended state, and restores it later. The simpler approach stores only the current state in the order row, which is already our source of truth, and rehydrates a machine at the start of each request by resetting it to that state.
7 messages in order. Play them, or step through them one at a time.
Notice that the new state is written back to the order row inside our normal transaction, so the state and the rest of the order change together. Recent versions use the reactive methods shown here, while older code calls the deprecated sendEvent(event) that returns a boolean; check your version.
6When a Plain Enum Is Enough
For a lifecycle of four states and four transitions, a framework may be more than we need. An enum with a map of allowed transitions gives the same safety in a dozen lines, with no extra dependency.
Spring Statemachine pays off with hierarchical states, regions, timers and many guards. Its release activity is low compared with core Spring projects, so before adopting it we should check its current maintenance status and its compatibility with our Spring Boot version.
7Follow-up questions
How do you reject an invalid event, such as SHIP for a NEW order?
The machine has no transition from NEW on SHIP, so it does not accept the event: the reactive sendEvent returns a result of type DENIED and the state stays NEW. Our service checks that result and throws a domain exception, which the controller turns into 409 Conflict with a problem detail such as Order 7 cannot be shipped in state NEW. We never let the call succeed silently, because the client would believe the order shipped.
Why not use one machine and synchronize access?
Because the state belongs to the order, not to the application. Synchronizing would serialize all orders through one lock and still mix up their states. One machine per order, rebuilt from the stored state, is both correct and scalable.
Where should business logic live, in actions or in services?
In services. Actions should call them, so the logic can be tested and reused without starting a machine.
8Conclusion
In this answer, we saw the vocabulary of Spring Statemachine: enum states and events, transitions declared in a configurer adapter, guards that can refuse a transition and actions that run with it. We created one machine per order from a StateMachineFactory instead of sharing a singleton, persisted state either through the persister support or by storing the current state in the order row and rehydrating a machine per request, rejected events the machine denies, and noted that a plain enum with an allowed-transition map is often enough.
Open with
Spring Statemachine describes a lifecycle with states and events, which are normally enums, here NEW, PAID, SHIPPED, CANCELLED and PAY, SHIP, CANCEL. In a configuration class that extends the enum configurer adapter, I declare the states with NEW as initial and SHIPPED and CANCELLED as end states, and the transitions, for example withExternal source NEW target PAID event PAY.
Then, if they want more
A guard decides if a transition may happen, like checking the payment amount in the message header against the order total, and an action runs on the transition, like notifying the customer or starting a refund on cancel from PAID.
I enable a StateMachineFactory, not a single machine, because a machine holds the state of one order; sharing one would mix orders and break under concurrency.
Between requests, the simplest persistence is to keep only the current state in the order row, and at the start of each request get a machine from the factory, reset it to that state, send the event, check that the result is accepted, and write the new state back in the same transaction.
The project also has StateMachinePersister with JPA or Redis when we need extended state saved. An event without a transition, like SHIP for a NEW order, is denied, and I return a 409. For a lifecycle this small, an enum with an allowed-transition map is often enough, and I would check the project's maintenance status before adopting it.
About this answer
- Last updated
- 12 November 2026
- Checked
- Its structure and format are checked automatically. Use it as a carefully prepared study note: where a detail matters, confirm it against the documentation below. What the checks cover
- Where the question comes from
- Written by Scrivio for practice. It is not taken from an interview.
- Official documentation
- Spring Boot reference
Report an issue with this answer
Copy the line below and send it to the author with what you found. It carries the address of this page.
Issue with "Explain the main concepts of Spring Statemachine (states, events, transitions, guards...": https://scrivio-interview-library.netlify.app/spring-boot/spring-projects/1324e0cb7d/