Quick revision
The short answer
I would include a short set of rules that each prevent a known class of production bug: never call subscribe() in service code, return the publisher instead; wrap side effects and expensive or blocking calls in Mono.defer or fromCallable so they are lazy; always bound flatMap concurrency when calling limited resources; put a timeout on every remote call and handle errors per item where partial success is valid; avoid block() except in tests and at true edges; and add checkpoint() to key pipelines so failures can be traced. Each rule comes with its reason, an example and, where possible, automated enforcement.
What to remember
In this answer, we built operator-level guidelines for reactive code from the bugs they prevent: never subscribe() inside service code but return the publisher, wrap side effects and expensive calls in defer or fromCallable, bound flatMap concurrency for limited resources, put timeouts on every remote call and handle errors per item where partial success is valid, avoid block() outside tests and edges, and use checkpoint in key pipelines. We made them stick with reasons and examples, automated checks such as static analysis and BlockHound, safe defaults and a review checklist.
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 include a short set of rules that each prevent a known class of production bug: never call subscribe() in service code, return the publisher instead; wrap side effects and expensive or blocking calls in Mono.defer or fromCallable so they are lazy; always bound flatMap concurrency when calling limited resources; put a timeout on every remote call and handle errors per item where partial success is valid; avoid block() except in tests and at true edges; and add checkpoint() to key pipelines so failures can be traced. Each rule comes with its reason, an example and, where possible, automated enforcement.
Why this question is asked
Reactive code fails in a small number of recurring ways, and most of them are invisible in unit tests and obvious in production: lost errors, hung requests, connection pools exhausted, event loops blocked. A good guideline document captures those lessons so that each team does not have to learn them the hard way. Interviewers ask this design question to see which bugs we consider most important and whether we can explain the reasoning behind each rule, not just list them. In this answer, we will go through the rules one by one, each with the bug it prevents and the correct pattern, and then look at how to make the guidelines stick.
1Rule 1: Never Subscribe Inside Service Code
In a reactive application, the framework at the edge, such as WebFlux for a controller or the Kafka listener container for a consumer, subscribes to the pipeline. A service that calls subscribe() itself starts a detached pipeline: the caller cannot wait for it, its errors are lost to a dropped-error log, and the Reactor context with security, tracing and transactions does not flow into it. The rule is that a method that does reactive work returns a Mono or Flux, and callers compose it.
2Rule 2: Make Side Effects and Expensive Calls Lazy
Java evaluates arguments eagerly, so Mono.just(loadUser()) or switchIfEmpty(createUser(id)) run their method calls while the pipeline is being assembled, even if the result is never used. That causes duplicate emails, blocking on event loop threads and retries that do not retry. The rule is that any work with side effects or cost goes inside Mono.defer, Mono.fromCallable or Mono.fromSupplier, and methods that return a Mono do nothing observable until it is subscribed. Blocking calls, if they cannot be avoided, are wrapped in fromCallable and moved to Schedulers.boundedElastic() with subscribeOn.
3Rule 3: Always Bound flatMap Concurrency for Limited Resources
flatMap allows 256 concurrent inner subscriptions by default. Against a database pool of 10 connections, a partner API with a rate limit, or a downstream service sized for a few dozen parallel requests, that default causes pool acquire timeouts, 429 responses or cascading overload. The rule is that every flatMap calling a database, remote service or other limited resource passes an explicit concurrency, chosen from that resource's limit, and that bulk writes use batching.
4Rule 4: Timeouts on Every Remote Call, Errors Handled per Item
A reactive pipeline that waits for a remote call that never answers simply waits forever; nothing blocks a thread, so nothing looks wrong until requests pile up. The rule is that every remote call has a timeout, either on the client, such as WebClient's response timeout, or with the timeout() operator. In addition, where partial success is valid, such as enriching a list of items, errors are handled per item, inside the flatMap lambda, with onErrorResume, so that one failure does not cancel the whole stream.
5Rule 5: No block() Outside Tests and Edges, and Make Pipelines Traceable
block() waits on the current thread for a result. On a Netty event loop thread it stalls every connection served by that thread, and recent Reactor versions throw an IllegalStateException when block() is called on a non-blocking thread. The rule is that block() appears only in tests, main methods, and adapters at a true boundary with blocking code, such as a legacy listener, never in services or controllers. BlockHound in integration tests catches blocking calls we did not notice.
Reactive stack traces are hard to read because they show operator internals rather than our code. The rule is to add checkpoint("description") at key points of important pipelines, such as the end of each service method that external callers use. When an error passes through, Reactor adds the checkpoint description to the stack trace, so we can see which pipeline failed. For deeper debugging in development, the Reactor debug agent gives full assembly traces at lower cost than the global debug hook.
| Rule | Bug it prevents | How we check it |
|---|---|---|
No subscribe() in services | lost errors, missing context, early responses | static analysis rule, review |
Lazy side effects (defer, fromCallable) | duplicate side effects, hidden blocking | review, BlockHound |
Bounded flatMap for limited resources | pool timeouts, overload of downstreams | review checklist, load tests |
| Timeouts, per-item error handling | hung requests, one error failing a batch | client defaults, review |
No block() outside tests and edges | stalled event loops | static analysis, BlockHound |
checkpoint() in key pipelines | untraceable failures | review of service entry points |
6Making the Guidelines Stick
Guidelines that live only in a wiki are forgotten. Three things make them effective. First, each rule states the reason and shows the bad and good code side by side, as above, so developers understand rather than memorise. Second, we automate what we can: static analysis or ArchUnit rules that flag subscribe() and block() in service packages, BlockHound enabled in integration tests, and shared WebClient configuration with default timeouts so the safe path is the default. Third, we add the remaining rules, such as bounded concurrency, to the code review checklist for reactive modules, and review the guideline after every incident caused by reactive code, adding a rule only when it prevents a real, repeated failure.
7Follow-up questions
Why not forbid reactive code and use virtual threads instead?
For many services that is a reasonable direction, and the guidelines can say when to prefer Spring MVC on virtual threads. But teams with streaming workloads, gateways or existing reactive code still need rules, so the guide should cover both and explain when each fits.
How do you handle context propagation in these guidelines?
By relying on the frameworks: use Reactor's context with Spring Security and Micrometer tracing integration, enable automatic context propagation where the versions support it, and never start detached subscriptions, which is the most common way context is lost. A short section with an example of reading the security context reactively helps.
Would you include a rule about Schedulers?
Yes, a short one: use boundedElastic only to wrap unavoidable blocking calls, never create schedulers per request, and avoid publishOn or subscribeOn without a reason, because unnecessary thread hops make debugging harder without making code faster.
8Conclusion
In this answer, we built operator-level guidelines for reactive code from the bugs they prevent: never subscribe() inside service code but return the publisher, wrap side effects and expensive calls in defer or fromCallable, bound flatMap concurrency for limited resources, put timeouts on every remote call and handle errors per item where partial success is valid, avoid block() outside tests and edges, and use checkpoint in key pipelines. We made them stick with reasons and examples, automated checks such as static analysis and BlockHound, safe defaults and a review checklist.
Open with
I'd keep the list short and tie every rule to a bug we've actually seen. Rule one: never call subscribe in service code; return the Mono or Flux and let the framework subscribe, because a detached subscription loses errors, loses the security, tracing and transaction context, and lets the response go out before the work finishes.
Then, if they want more
Rule two: anything with side effects or cost goes inside Mono.defer or fromCallable, because Java evaluates arguments eagerly, so things like switchIfEmpty(createUser(id)) send emails for users who exist, and Mono.just(loadUser()) blocks the event loop during assembly.
Rule three: every flatMap that calls a database or remote service has an explicit concurrency, since the default of 256 exhausts a pool of 10 connections or trips rate limits; bulk writes use buffer and batch inserts.
Rule four: every remote call has a timeout, and where partial success is fine, errors are handled per item inside the flatMap with onErrorResume so one failure doesn't kill the batch. Rule five: no block outside tests and true edges, and BlockHound in integration tests to catch blocking calls.
And I'd ask for checkpoint on key pipelines, so stack traces say which pipeline failed. Then I'd make them stick: bad and good examples for each rule, static analysis or ArchUnit checks for subscribe and block in service packages, a shared WebClient with default timeouts, and a review checklist that we revisit after every reactive incident.
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 "You are writing coding guidelines for reactive code in your organisation. Which...": https://scrivio-interview-library.netlify.app/spring-boot/reactive-fundamentals/a64e1988a0/