Scrivio Interview library
ConceptFoundations6 min read · updated 12 November 2026

What is the difference between publishOn and subscribeOn, and which scheduler should you use for wrapping a blocking call?

subscribeOn decides which thread the subscription and the source run on, wherever we place it in the chain, and if there are several the one closest to the source wins. publishOn switches the thread for the operators that come after it. To wrap a blocking call we use Mono.fromCallable(...).subscribeOn(Schedulers.boundedElastic()), never Schedulers.parallel(), whose few threads are meant for CPU-bound work and must not block.

Why this question is asked

Reactor's threading model surprises people in both directions. Some expect every operator to run asynchronously on its own pool, while others expect nothing ever to change threads. Getting it wrong leads either to blocked event loops in WebFlux or to slow services full of needless thread hops. Interviewers ask this because publishOn and subscribeOn are the two levers we have, and because wrapping blocking code correctly is a daily task in reactive services. In this answer, we will recap Reactor's default threading, see what each operator changes with thread names printed at each step, and then choose the right scheduler for blocking and CPU-bound work.

1A Quick Recap: Reactor Does Not Switch Threads by Default

A Scheduler in Reactor is an abstraction over a pool of threads, similar to an ExecutorService. Reactor provides a few ready-made ones: Schedulers.parallel() with one thread per CPU core for CPU-bound work, Schedulers.boundedElastic() with a larger, capped pool for blocking work, Schedulers.single() with one reusable thread, and Schedulers.immediate(), which runs on the current thread.

The important default is that operators do not use any scheduler. Each operator runs on whatever thread delivered the signal to it. If we subscribe from main, Flux.range emits on main and every map after it runs on main. If a WebClient response arrives on a Netty event-loop thread, everything after it runs on that event-loop thread. Reactor is concurrency-agnostic: it only changes threads where we ask it to, or where an operator is inherently time-based, such as delay or interval, which use parallel() by default.

Java
Flux.range(1, 2)
.map(i -> { log("map"); return i; })
.subscribe(i -> log("subscriber"));
// map on main
// subscriber on main
// map on main
// subscriber on main

2What subscribeOn Changes

subscribeOn(scheduler) makes the act of subscribing happen on a thread of that scheduler. Because subscribing travels up the chain to the source, the source then emits on that thread, and with no other switch everything downstream also runs there. Its position in the chain does not matter for this, since the subscription always reaches the source. If a chain has two subscribeOn calls, the one nearest the source is the one that decides where the source runs; the later one only affects the brief moment of subscription above it.

Java
Flux.range(1, 2)
.map(i -> { log("map"); return i; })
.subscribeOn(Schedulers.boundedElastic()) // placed at the end, still moves the source
.subscribe(i -> log("subscriber"));
// map on boundedElastic-1
// subscriber on boundedElastic-1

Notice that the map before subscribeOn moved too. That is the key difference from publishOn: subscribeOn affects the source side of the chain, not the position after it.

3What publishOn Changes

publishOn(scheduler) affects only what comes after it. Signals arriving from upstream are put into a small queue, and a worker of the given scheduler takes them out and runs the downstream operators. Upstream operators keep running wherever they ran before.

Java
Flux.range(1, 2)
.map(i -> { log("map 1"); return i; })
.publishOn(Schedulers.parallel())
.map(i -> { log("map 2"); return i; })
.subscribe(i -> log("subscriber"));
// map 1 on main
// map 1 on main
// map 2 on parallel-1
// subscriber on parallel-1
// map 2 on parallel-1
// subscriber on parallel-1
publishOn compared with subscribeOn
subscribeOnpublishOn
What movesthe subscription and the sourceoperators after it
Does position matter?no; the one nearest the source winsyes; it is a switch at that point
Mechanismsubscribes from a scheduler threadqueue plus a scheduler worker
Typical userun a blocking or lazy source elsewheremove downstream work to another pool

So publishOn is a switch in the middle of the pipeline, and its position matters. We can use several, each moving the following operators to a different pool. Each one adds a queue and a thread hop, which costs a little latency, so we add them only where a different kind of work begins.

A simple way to remember it: subscribeOn is about where the source works; publishOn is about where the results are processed from this point on.

4Wrapping a Blocking Call Correctly

A blocking call, such as a JDBC query, a legacy client or file I/O, holds its thread until it returns. In a reactive service that thread is often a Netty event loop serving thousands of connections, so we must move the call to a pool built for waiting. The pattern is to make the call lazy with fromCallable, so it runs only on subscription, and then subscribe on boundedElastic.

Java
Mono<Customer> customer =
Mono.fromCallable(() -> legacyClient.findCustomer(id)) // blocking, runs on subscription
.subscribeOn(Schedulers.boundedElastic()); // ...on a thread that may block
// findCustomer on boundedElastic-3; the event loop stays free
// Broken: Mono.just runs the call immediately, on the caller's thread
Mono<Customer> bad = Mono.just(legacyClient.findCustomer(id))
.subscribeOn(Schedulers.boundedElastic());
// findCustomer on reactor-http-nio-2: the event loop is blocked

Notice the broken version. Mono.just evaluates its argument at assembly time, before any scheduler is involved, so subscribeOn has nothing left to move. fromCallable or defer is what makes the blocking work part of the subscription.

We never wrap blocking calls with parallel(). It has as many threads as cores, eight on an eight-core machine, and it exists for CPU-bound work like parsing or computing. Eight blocked threads would stop every timer, delay and CPU task in the application. In WebFlux, Reactor also marks parallel() threads as non-blocking, so calling block() on them throws an error.

5Follow-up questions

If I use subscribeOn, do I also need publishOn after a blocking call?

Usually not. After the blocking call returns, the rest of the chain runs on the boundedElastic thread, which is acceptable for light work. If heavy CPU work follows, a publishOn(Schedulers.parallel()) moves it to the CPU pool and frees the blocking thread sooner. In WebFlux the response writing returns to the event loop on its own.

Which thread does a WebClient response continue on?

On a Reactor Netty event-loop thread, the one that received the response bytes. So any operator after a WebClient call runs on that event loop unless we switch, and blocking code placed there blocks the loop.

What about virtual threads?

With virtual threads, blocking becomes cheap, and recent Reactor versions can back boundedElastic with virtual threads on Java 21 and later through a system property; check your version. The rule to keep blocking work off parallel() and off the event loop stays the same.

6Conclusion

In this answer, we saw that Reactor runs each operator on the thread that delivered the signal and only changes threads where we ask. subscribeOn moves the subscription and the source, its position does not matter, and the one nearest the source wins. publishOn switches the thread for everything after it, so its position matters. To wrap a blocking call we use Mono.fromCallable(...).subscribeOn(Schedulers.boundedElastic()), keeping the call lazy, and we reserve Schedulers.parallel() for CPU-bound work that never blocks.

What to say in the interviewAbout 90 seconds aloud

Open with

First, Reactor doesn't switch threads by default. Each operator runs on the thread that emitted the signal, so if I subscribe from main everything runs on main, and after a WebClient call everything runs on the Netty event loop.

Then, if they want more

The two operators are the ways I change that. subscribeOn changes the thread on which the subscription happens, and since the subscription travels up to the source, it decides where the source runs.

Its position in the chain doesn't matter, and if there are several, the one nearest the source wins. publishOn is a switch in the middle: it queues signals and runs everything downstream of it on the given scheduler, so its position matters, and operators above it are unaffected.

For a blocking call I write Mono.fromCallable with the blocking code inside, then subscribeOn(Schedulers.boundedElastic()). fromCallable matters because it makes the call lazy.

If I wrote Mono.just(blockingCall()), the call runs immediately on the caller's thread, maybe an event loop, and subscribeOn can't move it. boundedElastic is the pool designed for blocking: it grows on demand up to a cap and evicts idle threads. Schedulers.parallel() is the wrong choice because it has one thread per core and is meant for CPU-bound work.

If I block those few threads, timers and CPU tasks stall, and Reactor treats them as non-blocking threads, so block() on them throws.

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 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 "What is the difference between publishOn and subscribeOn, and which scheduler should you...": https://scrivio-interview-library.netlify.app/spring-boot/reactive-fundamentals/9297f32c9f/