Scrivio Interview library
DesignIntermediate8 min read · updated 12 November 2026

A loan application workflow has 15 states, several approval steps, and regulators require an audit of every transition. Compare the state pattern, an explicit transition table, a state machine library, and a workflow engine.

With 15 states and regulators auditing every transition, the state pattern spreads the rules over 15 classes and becomes hard to review, while an explicit transition table of current state, event, guard, next state and action is easy to review, test and audit. A state machine library adds guards, actions and persistence helpers on top of such a table at the cost of another framework to learn, and a workflow engine fits when the process runs for weeks with timers, human tasks and retries that must survive restarts. Whatever we choose, we store every transition as an audit record and use optimistic locking so two events cannot move the same application at once, and we decide based on duration, number of states and audit needs.

Why this question is asked

Loan applications are a good test case for state management because they combine many states, human decisions, long waits and strict audit rules. A loan might go from SUBMITTED through credit check, underwriting, two levels of approval, document collection and offer to FUNDED or DECLINED, over days or weeks, and a regulator may ask years later who approved application 81723, under which rule, and when. Interviewers ask this comparison because each of the four options is right for some systems, and a senior engineer should be able to explain what each one costs and choose on the basis of the actual requirements. In this answer, we will look at each option in turn, then at the audit and concurrency requirements every option must meet, and finish with how to choose.

1Option 1: The State Pattern

In the state pattern, each state is a class implementing an interface with one method per event, such as submit(), approve() or requestDocuments(). The application delegates to its current state object, which decides what happens and which state comes next. The behaviour of a state lives in its class, which works well with three or four states whose behaviour differs a lot.

With 15 states and perhaps 12 events, the rules are spread over 15 files, and each file must handle or reject every event. To answer a simple review question, such as from which states can an application be declined, a reviewer must open all 15 classes. Auditors and compliance officers, who are not Java developers, cannot review it at all. And the full set of transitions, the thing the regulator cares about, exists nowhere as a single artefact.

2Option 2: An Explicit Transition Table

A transition table lists every allowed move as a row: the current state, the event that triggers it, a guard that must be true, the next state, and an action to perform. The whole lifecycle is visible in one place, reviewable by developers and compliance alike, and any move not in the table is rejected. A small engine of perhaps a hundred lines looks up the row, checks the guard, runs the action, changes the state and writes the audit record.

Java
record Transition(State from, Event event, Predicate<LoanApp> guard, State to, Consumer<LoanApp> action) {}
static final List<Transition> TABLE = List.of(
new Transition(SUBMITTED, CREDIT_OK, a -> true, UNDERWRITING, a -> {}),
new Transition(UNDERWRITING, APPROVE, a -> a.amount() < 50_000, OFFER_PREPARED, LoanActions::prepareOffer),
new Transition(UNDERWRITING, APPROVE, a -> a.amount() >= 50_000, SENIOR_REVIEW, a -> {}),
new Transition(SENIOR_REVIEW, APPROVE, LoanRules::seniorLimitOk, OFFER_PREPARED, LoanActions::prepareOffer),
new Transition(UNDERWRITING, DECLINE, a -> true, DECLINED, LoanActions::notifyDecline)
/* ... about 30 rows for 15 states */
);
// fire(app 81723 in UNDERWRITING, APPROVE) with amount 80_000 -> SENIOR_REVIEW
// fire(app 81723 in DECLINED, APPROVE) -> rejected: no matching row
Part of the loan transition table
FromEventGuardTo
SUBMITTEDCREDIT_OKalwaysUNDERWRITING
SUBMITTEDCREDIT_FAILalwaysDECLINED
UNDERWRITINGAPPROVEamount below 50,000OFFER_PREPARED
UNDERWRITINGAPPROVEamount 50,000 or moreSENIOR_REVIEW
SENIOR_REVIEWAPPROVEapprover is not the underwriterOFFER_PREPARED
OFFER_PREPAREDOFFER_ACCEPTEDdocuments completeFUNDING
FUNDINGFUNDS_SENTalwaysFUNDED

Notice that the guard expresses rules such as the four-eyes principle for large loans directly in the table. Tests are simple: one test per row and a check that every state is reachable and every non-final state has a way out. The table can also be printed as a diagram or a document for the regulator, generated from the same source as the running code, so the two cannot disagree.

3Option 3: A State Machine Library

Libraries such as Spring Statemachine implement the transition table idea with more features: hierarchical states, so a group of states can share transitions such as WITHDRAW, entry and exit actions, guards, listeners for auditing, and helpers to persist and restore a machine's state. For a complex lifecycle these can save code, especially hierarchical states, which reduce the number of rows when many states share the same exits.

The cost is a framework to learn and debug, configuration that is often more verbose than a plain table, and a dependency whose maintenance pace we should check before relying on it. For 15 states and roughly 30 transitions, a home-grown table is often clearer; a library starts to pay off when the machine grows nested states, or many machines of different types exist in the company.

4Option 4: A Workflow Engine

A loan process is not only a state machine; it is also a long-running process. It waits days for documents, sends reminders after 7 days, expires offers after 30, assigns tasks to human underwriters, and calls external credit bureaus that time out. Each of those needs timers that survive restarts, retries, task queues and visibility. A durable workflow engine, whether a BPMN engine such as Camunda or a code-based one such as Temporal, provides all of that, records the history of each instance, and has tooling for operators to find and fix stuck applications.

The price is a platform to run, a model to learn and some lock-in. So the engine is the right choice when the timers, human tasks and retries dominate, and a table or library is enough when the process is mainly a lifecycle of states driven by events from other systems, with timers handled by a simple scheduled job.

Comparing the four options
OptionReviewable by auditorsTimers and human tasksCost
State patternPoor: rules in 15 classesBuild yourselfLow setup, high review cost
Transition tableGood: one table, printableBuild yourselfSmall engine, easy tests
State machine libraryGood if configured as a tablePartly, via actionsFramework to learn
Workflow engineGood: model and historyBuilt inPlatform, lock-in, learning curve

5Audit and Concurrency, Whatever We Choose

Two requirements apply to every option. First, every transition is stored as an audit record in the same database transaction as the state change: application id, from state, to state, event, guard result, the user or system that triggered it, the rule version, and a timestamp. The table is append-only, with no updates or deletes allowed for the application's database user, so the history cannot be quietly rewritten. Because it commits together with the state change, there can be no change without a record and no record without a change.

Second, two events must not move the same application at once. If an underwriter clicks approve while an automated check fires decline, both might read UNDERWRITING and each apply its own transition. Optimistic locking prevents that: the application row has a version column, and the update succeeds only if the version is still the one we read. With JPA, a @Version field does this automatically; the loser gets an OptimisticLockException, reloads the application, and finds it in a state where its event may no longer be allowed.

Java
update loan_application
set state = 'SENIOR_REVIEW', version = version + 1
where id = 81723 and version = 7; -- 1 row: we won
-- the concurrent request with version = 7 updates 0 rows -> retry or reject
insert into loan_transition_audit (app_id, from_state, to_state, event, actor, rule_version, at)
values (81723, 'UNDERWRITING', 'SENIOR_REVIEW', 'APPROVE', 'u.chen', 'v14', now()); -- same transaction
Step throughFiring one event on an application
  1. Load application with its versionState UNDERWRITING, version 7
  2. Find a row for state and eventNo row means the event is rejected
  3. Is the guard true?For example amount and approver rules
    YesRun the actionNoReject and record the attempt
  4. Update state where version = 7Optimistic lock
  5. Was one row updated?Zero rows means another event won
    YesInsert audit row and commitNoRoll back, reload, retry or report

5 steps in order. Play them, or step through them one at a time.

6How to Choose

The choice follows three properties of the process. Duration: if instances live seconds or minutes and are driven by events from other systems, a table or library inside the service is enough; if they wait days with timers and reminders, an engine earns its cost. Number and shape of states: with a handful of states and different behaviour per state, the state pattern is fine; with 15 states and many shared rules, a table or a library with hierarchical states is easier to keep correct. Audit needs: when regulators review the rules, the lifecycle must exist as one readable artefact with a complete history, which rules out the state pattern and favours a table, a library configured as one, or an engine model.

For this loan workflow, with 15 states, human approvals, waits for documents and strict audit, I would choose a workflow engine if the organisation already runs one or the timers and human tasks are substantial, and otherwise an explicit transition table with a scheduled job for timers, in both cases with the audit table and optimistic locking described above.

7Follow-up questions

How do you change the transition rules without breaking applications in progress?

We version the table and record the rule version in each audit row. Applications in progress either finish under the version they started with, or we write a migration that maps their states to the new table, reviewed like any other change. Regulators usually want to know which rule version applied to each decision, which the audit row answers.

Where do the actions with side effects go, such as sending an offer letter?

They should not run inside the database transaction if they call external systems. The transition writes an outbox row in the same transaction, and a worker sends the letter afterwards, idempotently. That keeps the state change and the intent to send atomic.

Is event sourcing an option here?

Yes: storing the events and deriving the state by replaying them gives a natural audit trail. It adds complexity in queries and schema evolution, so an append-only transition audit next to a current-state row gives most of the audit benefit with a simpler model.

8Conclusion

In this answer, we compared four ways to manage a 15-state loan workflow. The state pattern spreads the rules across classes and is hard to review; an explicit transition table of state, event, guard, next state and action is easy to review, test and audit; a state machine library adds features like hierarchical states at the cost of a framework; and a workflow engine suits long-running processes with timers, human tasks and retries. In every case we store each transition as an audit record and use optimistic locking, and we choose by duration, number of states and audit needs.

What to say in the interviewAbout 100 seconds aloud

Open with

I would compare them on reviewability, long-running needs and cost. The state pattern puts each state in its own class. With 15 states and a dozen events that spreads the rules over 15 files, nobody can see the whole lifecycle in one place, and an auditor certainly cannot review it.

Then, if they want more

An explicit transition table, rows of current state, event, guard, next state and action, puts the whole lifecycle in one artefact. Rules like senior approval above 50,000 become guards, every row gets a test, and we can generate the documentation for the regulator from the same table.

A state machine library like Spring Statemachine adds hierarchical states, listeners and persistence helpers, which helps when the machine is large or nested, but it is another framework to learn.

A workflow engine is about time rather than states: if the process waits days for documents, sends reminders, expires offers and assigns tasks to underwriters, durable timers, retries and human task handling justify the platform. Whichever I choose, two things are non-negotiable.

Every transition is written as an append-only audit record in the same transaction as the state change, with actor, event, rule version and time. And the application row has a version column, so if an approval and an automatic decline race, only one wins and the other reloads.

For this loan process, I would pick a workflow engine if we already run one or the timers and human tasks are heavy, and otherwise a transition table with a scheduled job for timers.

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 "A loan application workflow has 15 states, several approval steps, and regulators...": https://scrivio-interview-library.netlify.app/java/behavioral-patterns/79834ef9ed/