Scrivio Interview library
CodingIntermediate6 min read · updated 12 November 2026

Review this method:

Java
public Mono<Order> place(Order o) {
repository.save(o).subscribe();
return Mono.just(o);
}

What is wrong and how would you write it?

The method starts the save in a separate, detached subscription and immediately returns the original order, so the caller cannot wait for the save to finish, errors from the save are lost, the returned order may lack generated fields such as the id and version, and the Reactor context carrying security, tracing and transactions does not flow into the detached subscription. The fix is to return the chain itself: return repository.save(o);, or repository.save(o).thenReturn(o) if we really need the original instance after the save completes.

Why this question is asked

Calling subscribe() inside service code is probably the single most common mistake when developers move from imperative to reactive code. It looks natural, because in imperative code calling save() does the save, and in reactive code nothing happens until someone subscribes, so a developer adds subscribe() to make it happen. Interviewers use this snippet to see whether we understand who should subscribe and what is lost when someone else does. In this answer, we will see what the method actually does, go through each problem it causes, and then write it correctly.

1A Quick Recap: Who Subscribes in a Reactive Application

A Mono is lazy: repository.save(o) returns a description of the save, and nothing touches the database until something subscribes. In a Spring WebFlux application, the framework is the subscriber: a controller returns a Mono, and WebFlux subscribes to it, writes the result to the HTTP response when it emits, and returns an error response if it fails. Every service method in between should simply build on the Mono it received and return it, so that the whole request is one chain with one subscriber at the end.

2What the Method Actually Does

Let us follow the code as written.

Java
public Mono<Order> place(Order o) {
repository.save(o).subscribe(); // starts the save now, in its own subscription
return Mono.just(o); // returns at once, unrelated to the save
}
// In the controller:
// return orderService.place(order); -> 200 OK with {"id": null, "status": "NEW"}
// Meanwhile, maybe: R2DBC insert fails with a duplicate key
// Log: Operator called default onErrorDropped ... DuplicateKeyException
Step throughThe save detached from the request
Controllerplace()RepositoryWebFlux
place(order)
save(o).subscribe(): detached
Mono.just(o)
return Mono to WebFlux
200 OK, id null
insert fails later: error dropped

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

The response is sent without waiting for the save, and its failure reaches nobody.

The call to subscribe() starts the save in a separate subscription that nobody else can see. The method then returns Mono.just(o), a Mono that emits the order immediately. The controller's response is written while the insert may still be running, or after it has failed.

3Problem 1: Errors Are Lost and the Caller Cannot Wait

The detached subscription has no error handler, so when the insert fails, Reactor has nowhere to send the error. It reports it through its dropped-error hook, which logs it, and the client has already received 200 OK for an order that does not exist. Retries, timeouts or onErrorResume added by the caller do not apply, because they are attached to Mono.just(o), not to the save.

The caller also cannot wait for completion. A test that calls place() and then reads the order back may find nothing, because the save has not finished. A batch job that places 10,000 orders may finish its loop and shut down while inserts are still in flight. And because there is no backpressure between the detached saves and the caller, a burst of calls can start thousands of concurrent inserts and exhaust the connection pool.

4Problem 2: The Returned Order Lacks Generated Fields

repository.save() returns the saved entity, which is the version with values the database or Spring Data generated: the id from an identity column, a version number for optimistic locking, created timestamps. Spring Data R2DBC may return a new instance rather than modifying ours, especially with immutable entities such as records. Mono.just(o) returns the object as it was before the save, so the client sees id null and version null, and any follow-up call that needs the id fails.

5Problem 3: Context Does Not Flow Into the Detached Subscription

Reactive code cannot use ThreadLocal reliably, because a pipeline moves between threads. Instead, Reactor carries request-scoped data in its Context, which flows from the subscriber up through the chain. Spring Security stores the authenticated user there, Micrometer tracing stores the trace and span, and reactive transactions with @Transactional or TransactionalOperator store the current transaction there too.

A new subscription started inside the method gets an empty context. So the save runs without the security context, which can break auditing fields such as createdBy; without the trace, so it appears as an orphan in the tracing system; and outside the caller's transaction, so if the caller's later steps fail and roll back, the order insert is not part of the rollback, or worse, runs after it.

6Writing It Correctly

The fix is to return the chain so that the caller's subscriber drives the save.

Java
public Mono<Order> place(Order o) {
return repository.save(o); // the saved order, with id and version
}
// If later steps are needed, keep chaining:
public Mono<Order> place(Order o) {
return validator.check(o)
.then(Mono.defer(() -> repository.save(o))) // save only after validation succeeds
.flatMap(saved -> events.publish(new OrderPlaced(saved.id())).thenReturn(saved));
}
// Controller: return orderService.place(order); -> 201 with {"id": 1042, "version": 0}
// A failed insert now becomes an error response, inside the same transaction and trace

If we really need to return the original instance, for example because the entity is mutable and the caller holds a reference to it, repository.save(o).thenReturn(o) waits for the save and then emits o. Usually returning the saved instance is better. And if the save truly should happen in the background, such as an audit record that must not slow the response, the right tool is an explicit, bounded mechanism, such as publishing an event to a queue, not a hidden subscribe() call.

7Follow-up questions

Is subscribe() ever acceptable in application code?

At the edges, yes: in a main method, a scheduled job that owns its pipeline, a test, or a message listener container adapter that has no framework subscriber. Even then, the code should handle errors in the subscribe call and keep the Disposable if cancellation matters. Inside services that return a publisher, never.

Why did the developer get a dropped error log instead of an exception?

Because the detached subscription had no onError consumer. Reactor cannot throw the error to anyone, since the calling method has already returned, so it routes it to the onErrorDropped hook, which logs it by default.

Would .block() instead of .subscribe() fix it?

It would wait for the save, but it blocks the thread, which on a WebFlux event loop throws an IllegalStateException in recent Reactor versions or stalls the server. Returning the publisher is the correct fix.

8Conclusion

In this answer, we saw that calling repository.save(o).subscribe() inside place() detaches the save from the caller's chain: the response is sent without waiting, errors from the save are lost, the returned order lacks generated fields such as the id and version, and the Reactor context with security, tracing and transactions does not reach the detached subscription. The fix is to return the chain, return repository.save(o), or repository.save(o).thenReturn(o) when the original instance is really needed.

What to say in the interviewAbout 90 seconds aloud

Open with

The problem is the subscribe call. It starts the save in a separate subscription that the caller knows nothing about, and the method immediately returns Mono.just of the original order.

Then, if they want more

So WebFlux sends the response straight away, possibly before the insert has even run. If the insert fails, say with a duplicate key, the error has nowhere to go: the detached subscription has no error handler, so Reactor just logs it as a dropped error, and the client has a 200 for an order that doesn't exist.

The caller's retries, timeouts and error handling don't apply, and a test or batch job can't wait for completion. Second, the order we return is the one from before the save, so the generated id and version are missing; with Spring Data R2DBC the saved entity may even be a different instance. Third, context doesn't flow.

Security, tracing and reactive transactions all live in the Reactor context of the caller's subscription, and a new subscription starts with an empty one, so the save runs outside the transaction and outside the trace. The fix is simply to return the chain: return repository.save(o), and if I need more steps I keep chaining with flatMap.

If I really need the original instance, I use save(o).thenReturn(o). The rule is that service code never subscribes; the framework at the edge does.

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.
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 "Review this method:": https://scrivio-interview-library.netlify.app/spring-boot/reactive-fundamentals/95007feee6/