Scrivio Interview library
ScenarioIntermediate6 min read · updated 12 November 2026

A team rewrote a Spring service using Vavr Either and Try everywhere. New hires struggle, stack traces are hard to read and the code mixes with Spring's exception handling. How would you review this?

I would review it by drawing a clear boundary rather than by rewriting it all back. Result types such as Either work well for expected business failures inside the domain layer, but they fight Spring, which expects exceptions to roll back transactions and to reach @ExceptionHandler, so unexpected failures should stay exceptions and results should be converted at the edges. Where possible, sealed interfaces with records give the same benefits without a library. The changes should be made incrementally, guided by a short style guide the whole team agrees on, with readability for new hires as an explicit goal.

Why this question is asked

Functional error handling is attractive: failures become values that the compiler forces us to handle. But a team that applies it everywhere inside a framework built around exceptions often ends up with code that is clever, hard to read and subtly wrong, for example transactions that commit when they should roll back. Interviewers ask this scenario to see whether we can review a style choice fairly, find the real technical problems, and lead a change without a rewrite war. In this answer, we will look at where the style clashes with Spring, separate expected from unexpected failures, compare with modern Java features, and plan the incremental change.

1What the Review Finds

Typical code in such a service looks like this: every method returns Either or Try, controllers fold the results into responses, and repository calls are wrapped in Try.of. New hires find it hard because they must learn Vavr's types and operators before they can read a single endpoint. Stack traces are hard to read because the exception is captured inside a Try far from where it is finally reported, so the trace shows the lambda machinery of map and flatMap, and sometimes the original exception is turned into a string and lost.

Those are readability costs, and they are real. But the most serious finding is usually a correctness problem, so the review starts there.

2The Clash With Spring's Transactions

Spring's @Transactional rolls back when an exception leaves the method; by default a RuntimeException or an Error. If a method catches the failure into a Try or returns Either.left, no exception leaves it, so Spring commits whatever was written before the failure.

Java
@Transactional
public Either<OrderError, Order> placeOrder(Cart cart) {
Order order = orders.save(Order.from(cart)); // row inserted
return Try.of(() -> payments.charge(cart.total())) // charge throws PaymentDeclined
.toEither()
.mapLeft(e -> new OrderError.PaymentFailed(e.getMessage()))
.map(receipt -> order.paid(receipt));
}
// Result: Left(PaymentFailed[...]) returned normally
// Transaction: COMMIT, an unpaid order row stays in the database
Step throughWhy the unpaid order is committed
ControllerTransaction proxyOrderServiceDatabase
placeOrder(cart)
begin
placeOrder(cart)
insert order
charge fails, caught into Try
returns Left normally
no exception seen, so commit

7 messages in order. Play them, or step through them one at a time.

Spring decides on rollback by exceptions; a returned Left looks like success.

Notice that the code looks correct and its signature is honest, yet the data is wrong: the transaction committed because nothing threw. There are fixes, such as calling TransactionAspectSupport.currentTransactionStatus().setRollbackOnly() on the Left path, but every developer must remember them on every method, which is exactly the kind of rule that gets forgotten. The same mismatch affects @ExceptionHandler and @ControllerAdvice, which only see exceptions, and Spring Retry or Resilience4j annotations, which retry on exceptions.

3Drawing the Boundary: Expected Versus Unexpected Failures

The useful rule is to separate two kinds of failure. Expected failures are part of the business: insufficient funds, an item out of stock, a validation error. Callers must handle them, and a result type makes that visible. Unexpected failures are faults: the database is down, a bug threw a NullPointerException, a remote service timed out. Nobody can handle them meaningfully at the call site, and they should travel as exceptions to the framework, which logs them with a full stack trace, rolls back and returns a 500.

So result types belong inside the domain layer for expected outcomes, and infrastructure calls should not be wrapped in Try just to turn every exception into a value. At the edges, we convert explicitly: the application service maps a Left that requires rollback to an exception, or marks the transaction rollback-only, and the controller maps the remaining results to HTTP responses in one place.

Step throughWhere each style belongs
  1. Controllermap results to HTTP; unexpected exceptions to @ControllerAdvice
  2. Application service, @Transactionalconvert failed results that need rollback into exceptions
  3. Domain layerresult types for expected business failures
  4. Repositories and clientsthrow exceptions for faults; no Try wrapping

4 layers, from the top down. Step through them one at a time.

Results describe the business; exceptions carry faults to the framework.

4Preferring JDK Features for Result Types

Since Java 21, we can model results with a sealed interface and records, and handle them with an exhaustive switch. This keeps the benefit the team wanted, failures that callers cannot ignore, without requiring everyone to learn a library.

Java
sealed interface PaymentResult permits Paid, Declined, LimitExceeded {}
record Paid(Receipt receipt) implements PaymentResult {}
record Declined(String reason) implements PaymentResult {}
record LimitExceeded(Money limit) implements PaymentResult {}
String message = switch (payments.charge(cart)) {
case Paid p -> "paid " + p.receipt().id(); // "paid r-123"
case Declined d -> "declined: " + d.reason();
case LimitExceeded l -> "over limit " + l.limit();
}; // the compiler refuses the switch if a case is missing

A new team member reads this as ordinary Java. The result types also carry domain names, PaymentResult rather than Either<PaymentError, Receipt>, which documents the possibilities better. Optional covers the simple case of a missing value. Vavr can stay where it truly helps, for example persistent collections in a calculation-heavy part, but it no longer needs to be in every signature.

5Making the Change Incrementally

A big rewrite would freeze feature work and start a style war. Instead, the team agrees on a one-page style guide: which failures are results and which are exceptions, how results are converted at the service boundary, and that new result types use sealed interfaces. The authors of the current style must be part of writing it; their goal of explicit failures is sound, and the guide keeps it.

Then changes follow normal work. First, fix the correctness problems: find every @Transactional method that can return a failure after writing, and add tests that prove a rollback happens. Second, when a class is touched for a feature, bring it in line with the guide. Third, remove Try wrapping around infrastructure calls so stack traces are reported by the framework again. After a few months, the hot paths follow the guide, and readability is measured simply by asking new hires how long it took them to make their first change.

6Follow-up questions

Is it wrong to use Vavr in a Spring service?

No. The problem is using it everywhere without a boundary. Inside a pure domain calculation, Try or Either can make code clearer. The trouble starts when result types replace the exceptions that Spring relies on for transactions, error handling and retries.

How do you test that the rollback really happens?

With an integration test against a real database, for example with Testcontainers: call the service so that the charge fails, then check in a new transaction that no order row exists. A unit test with mocks cannot show transaction behaviour, because the proxy and the database are not involved.

How do you keep the authors motivated during the change?

By making clear that the goal they cared about, explicit and type-safe failure handling, stays, and only the means change. Involving them in writing the style guide and in choosing the sealed-interface patterns turns them into the people who teach it, rather than the people whose work was rejected.

7Conclusion

In this answer, we reviewed a service that used Vavr's Either and Try everywhere. The critical finding was that returned failures do not roll back Spring transactions, and that @ExceptionHandler and retry annotations also expect exceptions. We drew a boundary: result types for expected business failures inside the domain, exceptions for unexpected faults, explicit conversion at the service edge, and sealed interfaces with records instead of a library where possible. The change happens incrementally, guided by a style guide the team writes together, with readability for the whole team as a goal.

What to say in the interviewAbout 80 seconds aloud

Open with

I would start the review with correctness rather than style. Spring rolls back a transaction when an exception leaves a @Transactional method. If the code catches a failure into a Try or returns Either.left after writing to the database, Spring sees a normal return and commits, so we can end up with things like unpaid orders.

Then, if they want more

The same applies to @ExceptionHandler and retry annotations, which react to exceptions. So my first action is to find those methods and cover them with integration tests that prove rollback. Then I would agree a boundary with the team: result types are good for expected business failures, like a declined payment or a validation error, inside the domain layer.

Unexpected faults, like a database outage, stay exceptions and go to the framework, which also gives us readable stack traces again. At the service edge, failures that need rollback are converted to exceptions.

For new result types I would suggest sealed interfaces with records and an exhaustive switch, which give the same safety in plain Java that new hires already know.

I would not rewrite everything; I would write a short style guide together with the original authors, since their goal of explicit failures is sound, and apply it when classes are touched for normal work.

About this answer

Last updated
12 November 2026
Versions it names
Java 21
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 team rewrote a Spring service using Vavr Either and Try everywhere. New hires...": https://scrivio-interview-library.netlify.app/java/utility-libraries/3ab7c7594f/