Quick revision
The short answer
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.
What to remember
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.
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 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.
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.
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.
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.
- Official documentation
- Spring Framework reference, Spring WebFlux, Project Reactor reference guide
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/