Quick revision
The short answer
I would make the status an enum that can only change through methods on the Order entity, such as markPaid(), ship() and cancel(), and remove the public setStatus. Each method checks the current state against one table of allowed transitions, for example an EnumMap from each state to its allowed next states, and throws if the move is not allowed. A @Version column makes concurrent updates fail instead of silently overwriting each other, and every transition is recorded in a history table for audit and debugging.
What to remember
In this answer, we saw how a public setStatus spread order rules across many callers and allowed SHIPPED to move back to PAID. We replaced it with event methods on the entity that check the current state, kept all allowed transitions in one EnumMap next to the enum, added @Version so concurrent updates cannot both win, and recorded every transition in a history table for audit and debugging.
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.
I would make the status an enum that can only change through methods on the Order entity, such as markPaid(), ship() and cancel(), and remove the public setStatus. Each method checks the current state against one table of allowed transitions, for example an EnumMap from each state to its allowed next states, and throws if the move is not allowed. A @Version column makes concurrent updates fail instead of silently overwriting each other, and every transition is recorded in a history table for audit and debugging.
Why this question is asked
Order status bugs are some of the most expensive bugs in e-commerce: a SHIPPED order moving back to PAID can trigger a second shipment, a duplicate refund, or confusing emails to the customer. They usually come from a design where status is just a field that any code can set. Interviewers ask this question to see whether we can move a business rule from scattered callers into the model, where it cannot be bypassed. In this answer, we will reproduce the problem, give the entity methods that express each transition, keep the rules in one table, protect against concurrent updates with @Version, and record every transition for audit.
1Reproducing the Problem
A state machine is a set of states plus the rules for moving between them. An order has one: it is created, then paid, then shipped, then delivered, and it can be cancelled only before shipping. The problem is that the code does not know this. Status is a plain field with a public setter, and any service, listener or batch job can write any value.
Each caller looked correct in isolation. The payment webhook handler set PAID because a payment succeeded; it did not check what had happened since. With 15 places calling setStatus, nobody can see the full set of rules, and a new caller can break them without any test noticing.
2Transitions as Methods on the Entity
The fix is to remove the setter and give the entity one method per business event. The method names describe what happened, markPaid, ship, cancel, deliver, and each method decides whether that event is allowed in the current state. Callers can no longer choose a status; they can only report an event.
Now the duplicate webhook from the example throws IllegalStateTransition instead of corrupting the order. The webhook handler can catch it and treat a payment for an already-shipped order as a duplicate it can ignore, log it, or raise it for review, but the order itself stays correct. Notice also that JPA still needs to read and write the field, which it does through field access, so removing the public setter does not break persistence.
3Keeping the Allowed Transitions in One Place
The rules belong in one table that anyone can read. An EnumMap from each state to the set of states it may move to is compact and fast, and it lives right next to the enum.
| From | Allowed next states | Event method |
|---|---|---|
| CREATED | PAID, CANCELLED | markPaid(), cancel() |
| PAID | SHIPPED, CANCELLED | ship(), cancel() |
| SHIPPED | DELIVERED | deliver() |
| DELIVERED | none | - |
| CANCELLED | none | - |
markPaid() on a shipped order throws.A unit test can now check every pair of states against the table in a few lines, and a product owner can review the rules by reading them. For complex workflows with guards and actions, a library such as Spring Statemachine exists, but for most order lifecycles an enum and a map are clearer and easier to debug.
4Protecting Against Concurrent Updates
The checks above are only correct if two threads do not act on the same order at the same time. Imagine the cancel request and the shipping listener both load an order in PAID. Both checks pass, PAID can move to CANCELLED and PAID can move to SHIPPED, and the last commit wins. The order might end up SHIPPED although the customer was told it was cancelled.
Adding a version field fixes this. With @Version, Hibernate adds the version to the UPDATE's WHERE clause and increments it. The second commit updates zero rows, because the version changed, and Hibernate throws an optimistic locking exception (in Spring, ObjectOptimisticLockingFailureException). The losing operation can reload the order and try again; this time its transition check sees the new state and fails correctly.
5Recording Every Transition
Finally, when something does go wrong, we want to know what happened. Each transition writes a row to an order_status_history table: order id, from state, to state, the event, who or what triggered it (user id, service name, message id) and a timestamp. The simplest way is to add the history entry inside moveTo(), as a child entity saved with the order in the same transaction, so the history can never disagree with the status. This table answers support questions such as "why was this order cancelled?" in seconds and makes bugs like the original one easy to trace.
6Follow-up questions
Why not just validate the status in a service method instead of the entity?
Because a service-level check is easy to bypass: any other service, batch job or listener that loads the entity can still call setStatus. Putting the rule inside the entity, with no public setter, means there is no way to change the status without the check. It is the same principle as keeping invariants inside the object that owns the data.
Should a duplicate event throw, or be ignored silently?
The entity should throw, because it cannot know whether the call is a harmless duplicate or a real bug. The caller, which knows the context, decides. A payment webhook handler can recognise "already paid or later" as a duplicate and acknowledge it, while unexpected transitions get logged and alerted. Making the decision explicit at the caller is better than having the entity quietly accept anything.
7Conclusion
In this answer, we saw how a public setStatus spread order rules across many callers and allowed SHIPPED to move back to PAID. We replaced it with event methods on the entity that check the current state, kept all allowed transitions in one EnumMap next to the enum, added @Version so concurrent updates cannot both win, and recorded every transition in a history table for audit and debugging.
Open with
The problem is that status is just a field with a public setter, so every caller decides the next status on its own, and nothing knows the overall rules.
Then, if they want more
I would model the order lifecycle as a state machine inside the entity. Status stays an enum, but I remove setStatus and add one method per business event: markPaid, ship, deliver, cancel.
Each of those calls a private moveTo method that checks whether the transition is allowed from the current state and throws an exception if not, so a late payment webhook calling markPaid on a shipped order fails instead of moving it backwards.
The allowed transitions live in one place, an EnumMap on the enum from each state to the set of states it can move to, for example paid can go to shipped or cancelled, shipped can only go to delivered. That makes the rules easy to read, review and unit test exhaustively.
Then concurrency: two threads could both load a paid order and one cancels while the other ships, and both checks pass. Adding a @Version column makes Hibernate include the version in the update, so the second commit fails with an optimistic locking exception, and on retry its check sees the new state.
Finally, every transition writes a history row with from, to, the event and who triggered it, in the same transaction, which makes these issues easy to investigate.
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 official documentation for the version you use. What the checks cover
- Where the question comes from
- Written by Scrivio for practice. It is not taken from an interview.
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 "In an order service built with Spring Boot and JPA, an Order has a status field and many...": https://scrivio-interview-library.netlify.app/architecture/backend-system-design/4be562c652/