Scrivio Interview library
DesignAdvanced6 min read · updated 12 November 2026

You are writing coding guidelines for reactive code in your organisation. Which operator-level rules would you include to prevent the most common production bugs?

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.

Java
// Bad: detached, errors lost, no transaction
repository.save(order).subscribe();
return Mono.just(order);
// Good: one chain from the controller to the database
return repository.save(order);

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.

Java
// Bad: the email is sent even for existing users
findUser(id).switchIfEmpty(createUser(id));
// Good: createUser runs only if findUser is empty
findUser(id).switchIfEmpty(Mono.defer(() -> createUser(id)));

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.

Java
records.flatMap(repo::save); // bad: up to 256 saves for a pool of 10
records.flatMap(repo::save, 8); // good: bounded to the pool
records.buffer(500).flatMap(this::insertBatch, 4); // better for bulk loads

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.

Java
ids.flatMap(id -> client.fetch(id)
.timeout(Duration.ofSeconds(2))
.onErrorResume(e -> Mono.empty()), 10) // one failure drops one item, not the list
.collectList();

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.

Java
return orderRepository.save(order)
.flatMap(this::publishEvent)
.checkpoint("OrderService.place"); // appears in the error's stack trace
The rules and the bugs they prevent
RuleBug it preventsHow we check it
No subscribe() in serviceslost errors, missing context, early responsesstatic analysis rule, review
Lazy side effects (defer, fromCallable)duplicate side effects, hidden blockingreview, BlockHound
Bounded flatMap for limited resourcespool timeouts, overload of downstreamsreview checklist, load tests
Timeouts, per-item error handlinghung requests, one error failing a batchclient defaults, review
No block() outside tests and edgesstalled event loopsstatic analysis, BlockHound
checkpoint() in key pipelinesuntraceable failuresreview of service entry points
Each rule is tied to a failure we have seen, and to a way of noticing when it is broken.

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.

What to say in the interviewAbout 90 seconds aloud

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.
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/