Quick revision
The short answer
The concurrency bugs come from two events for the same claim being processed at once, usually on different instances, each with its own in-memory machine; events per claim must be serialized, for example with optimistic locking on the claim row or a per-claim queue. I would also check that state and extended variables are saved atomically with domain changes, that timers survive restarts, and whether 30 states with nested regions should be several smaller machines. For long-running workflows with timers and human steps, a workflow engine such as Camunda or Flowable, or a durable execution platform such as Temporal, is often a better fit.
What to remember
In this answer, we traced the concurrency bugs to two in-memory machines acting on the same claim and fixed them by serializing events per claim with optimistic locking or a partitioned queue, with side effects behind an outbox. We reviewed atomic persistence of state and variables, replaced in-memory timers with durable ones, questioned whether 30 states belong in one machine, and compared Spring Statemachine with Camunda, Flowable, Temporal and plain domain methods, recommending quick fixes now and a pilot before choosing a long-term engine.
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.
The concurrency bugs come from two events for the same claim being processed at once, usually on different instances, each with its own in-memory machine; events per claim must be serialized, for example with optimistic locking on the claim row or a per-claim queue. I would also check that state and extended variables are saved atomically with domain changes, that timers survive restarts, and whether 30 states with nested regions should be several smaller machines. For long-running workflows with timers and human steps, a workflow engine such as Camunda or Flowable, or a durable execution platform such as Temporal, is often a better fit.
Background to this question
Claim workflows run for weeks, involve people and deadlines, and must never lose a step, which is exactly where in-memory state machines start to hurt. A senior engineer is asked this to see whether they can diagnose concurrency and durability problems and judge when a library has been stretched beyond what it is good at. In this answer, we will reproduce the race, fix concurrency, review persistence and timers, look at the model's complexity, compare alternatives, and decide how to proceed.
1Reproducing the Race
Picture claim 4711 in state UNDER_REVIEW. At the same moment, an assessor approves it and a fraud system flags it. The two events arrive at two instances behind a load balancer. Each instance loads the claim, rebuilds a machine in UNDER_REVIEW, applies its event and saves.
The reason it breaks is that each machine checked its transition against a state that was already out of date. Both transitions were valid from UNDER_REVIEW, so both were accepted, and the last write wins. The claim might end APPROVED with a fraud flag silently lost, or with a payment action already sent while the stored state says FRAUD_CHECK.
6 messages in order. Play them, or step through them one at a time.
A machine per instance protects nothing across instances.
2Serializing Events per Claim
Events for one claim must be handled one at a time, while different claims still run in parallel. The simplest tool is optimistic locking on the claim row: a version column that every update checks. The second writer's update matches zero rows, it gets an OptimisticLockException, reloads the claim and replays its event against the new state, where FLAG_FRAUD from APPROVED may be refused or lead somewhere else, as the rules decide.
The other option is a per-key queue: publish events to a Kafka topic keyed by claim id, so all events for one claim land in one partition and one consumer processes them in order. That removes the race by design and suits high event volumes. Either way, side effects such as payments must happen after the state change commits, for example through an outbox table, so a lost race never leaves an external action behind.
3Persistence and Timers
The next review point is atomicity. The machine's state and its extended variables must be saved in the same database transaction as the domain changes. If the claim's assessment is saved in one transaction and the machine context later, through a separate persister call, a crash between them leaves the two disagreeing, and nobody notices until a customer calls.
Timers are the bigger risk. Spring Statemachine timers, such as timerOnce on a state, run in the memory of the instance that created the machine. If the claim waits 14 days for documents and the instance restarts on day 3, the timer is gone and the claim waits forever. Durable timers need a store: a due_at column with a scheduled job that sends the timeout event, protected by a lock such as ShedLock so only one instance fires it, or an engine that persists timers for us.
4Is the Model Too Big?
Thirty states with nested regions is hard to read, hard to test and hard to upgrade. A useful question is whether it is really one lifecycle. Often it is several: intake, assessment, payment and recovery, each with five to eight states, linked by events. Several small machines are easier to understand and change independently.
Sometimes the honest answer is that the domain needs explicit methods more than a machine: claim.approve(assessor) that checks the current enum state and throws if not allowed. That puts rules in plain Java, which every developer can debug, and leaves the machine abstraction only where it truly helps.
5Alternatives for Long-Running Workflows
Claims have timers, human tasks and steps that last weeks, which is what workflow engines are built for. We compare the realistic options on durability, visibility and team skills.
| Option | Durability and timers | Visibility | Cost to the team |
|---|---|---|---|
| Spring Statemachine, fixed | We build locking, outbox, durable timers | Our own dashboards | Low change, keeps upgrade pain |
| Camunda or Flowable (BPMN) | Persisted state, timers, human tasks | Process diagrams, cockpit | Learn BPMN and engine operations |
| Temporal (durable execution) | Workflow code replayed from history | Workflow history UI | New programming model, run a cluster or use the cloud |
| Domain methods + enum | Our locking and scheduler | Our own dashboards | Simple, but timers stay our problem |
Notice that the engines give two things the current design lacks: durable timers and an operational view of where each claim is stuck. That second point matters to the business as much as the bugs do.
6What I Would Recommend
First, stop the bleeding without a rewrite: add optimistic locking and retries, move side effects behind an outbox, and replace in-memory timers with persisted due dates. These changes take weeks, not months, and fix the production bugs.
Then decide the long-term home with a pilot. Model one claim type end to end in the strongest candidate, Camunda or Flowable if the business wants BPMN diagrams and human task lists, Temporal if the team prefers writing workflows as code, and compare effort, operability and how a change to the rules is deployed. Also check Spring Statemachine's release activity and its compatibility with the next Spring Boot major version, because if it cannot follow the platform, staying on it is a decision with an end date.
7Follow-up questions
How would you migrate in-flight claims to a new engine?
Usually we do not move them mid-flight at all. New claims start in the new engine from a cutover date, and existing claims finish in the old system, which shrinks to nothing as claims close. If some claims last too long for that, we migrate them only at stable waiting states, such as waiting for documents, by starting a process instance in the new engine at the matching step with the claim's data and remaining timer, and we mark the old machine as migrated so it cannot act again. A mapping table from old states to new steps, rehearsed on a copy of production data, is the core of the plan.
Why not use a distributed lock instead of optimistic locking?
A lock in Redis can work, but it adds a dependency and can expire while work is still running. Optimistic locking uses the database we already write to and fails safely: the loser simply retries.
How do you test concurrency fixes?
Fire the two events at the same claim from two threads in an integration test against a real database, many times, and assert the final state is one the rules allow and the side effects ran exactly once.
8Conclusion
In this answer, we traced the concurrency bugs to two in-memory machines acting on the same claim and fixed them by serializing events per claim with optimistic locking or a partitioned queue, with side effects behind an outbox. We reviewed atomic persistence of state and variables, replaced in-memory timers with durable ones, questioned whether 30 states belong in one machine, and compared Spring Statemachine with Camunda, Flowable, Temporal and plain domain methods, recommending quick fixes now and a pilot before choosing a long-term engine.
Open with
I would start with the concurrency bug. Each instance rebuilds its own in-memory machine, so two events for the same claim can both be accepted from the same old state and the last save wins.
Then, if they want more
Events for one claim must be serialized: I would add a version column for optimistic locking, so the second writer fails, reloads and resends its event against the new state, or route events through a Kafka topic keyed by claim id so one consumer handles them in order. Side effects like payments should only happen after commit, through an outbox.
Next, persistence: the machine state and extended variables must be saved in the same transaction as the domain changes. Timers are a big risk, because Spring Statemachine timers live in memory and vanish on restart, so I would store due dates and fire them from a locked scheduled job.
Then complexity: 30 states with regions probably hide several lifecycles that could be smaller machines or plain domain methods with enum states. For the long term, claims are long-running with timers and human steps, which is what Camunda or Flowable, or Temporal for workflows as code, are built for.
I would run a pilot on one claim type, compare durability, visibility and team skills, and check whether Spring Statemachine still keeps up with our Spring Boot version before investing more in 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 "A team built an insurance claim workflow on Spring Statemachine with 30 states, nested...": https://scrivio-interview-library.netlify.app/spring-boot/spring-projects/1f86913557/