Scrivio Interview library
ConceptFoundations6 min read · updated 12 November 2026

What is the difference between a cold and a hot publisher in Reactor? Give an example of each.

A cold publisher does its work separately for each subscriber: Flux.range counts again and a WebClient Mono sends a new HTTP request every time someone subscribes. A hot publisher produces items whether or not anyone is listening, and a late subscriber only sees what is emitted after it arrives, as with a Sinks.many().multicast() sink or a shared ticker. We turn a cold source into a shared hot one with share() or publish().refCount(), and we let late subscribers see earlier items with cache() or replay().

Why this question is asked

Most Reactor surprises in production come from not knowing whether a publisher is hot or cold. A service calls an API twice because a Mono was subscribed twice; a dashboard misses the first events because it subscribed to a hot stream too late. Interviewers ask this because the distinction decides how many times our side effects run and what each subscriber gets to see. In this answer, we will recap what subscribing means, look at a cold publisher and a hot publisher in code, and then see the operators that convert one into the other.

1A Quick Recap of What Subscribing Does

In Reactor, a Flux or a Mono is a description of work, not the work itself. Building a chain of operators does nothing; the work starts only when someone calls subscribe(). This is often called assembly time versus subscription time.

So the natural question is: when a second subscriber arrives, does the publisher start the work again for it, or does it share the work that is already running? A publisher that starts again for each subscriber is called cold. A publisher whose work runs independently of its subscribers, and which subscribers merely tap into, is called hot.

2A Cold Publisher in Action

Most publishers we create are cold. Flux.range, Flux.fromIterable, Mono.fromCallable, a repository query and a WebClient call all start from the beginning for every subscription.

Java
Mono<String> call = webClient.get().uri("/price/42")
.retrieve().bodyToMono(String.class);
call.subscribe(p -> log.info("A got {}", p)); // HTTP request 1
call.subscribe(p -> log.info("B got {}", p)); // HTTP request 2
Flux<Integer> numbers = Flux.range(1, 3);
numbers.subscribe(n -> log.info("first {}", n)); // first 1, first 2, first 3
numbers.subscribe(n -> log.info("second {}", n)); // second 1, second 2, second 3

Notice that both subscribers of the WebClient Mono cause their own HTTP call, and both subscribers of range get the full sequence from 1. Cold publishers are like a recipe: everyone who uses it cooks their own meal. That is usually what we want, because each subscriber gets complete, independent data, but it also means side effects run once per subscription.

3A Hot Publisher in Action

A hot publisher emits on its own schedule. The clearest example is a sink, an object we push items into from our own code. Sinks.many().multicast() creates one that forwards each item to whoever is subscribed at that moment.

Java
Sinks.Many<String> sink = Sinks.many().multicast().directBestEffort();
Flux<String> events = sink.asFlux();
sink.tryEmitNext("e1"); // nobody subscribed: e1 is lost
events.subscribe(e -> log.info("A {}", e));
sink.tryEmitNext("e2"); // A e2
events.subscribe(e -> log.info("B {}", e));
sink.tryEmitNext("e3"); // A e3, B e3
Cold versus hot at a glance
QuestionColdHot
When does work start?on each subscribe()independently of subscribers
Two subscribers meantwo runs, e.g. two HTTP callsone run, both share it
Late subscriber seeseverything from the startonly items emitted after it joined
ExamplesFlux.range, WebClient, repository querySinks.many().multicast(), shared ticker

B arrived after e2 was emitted, so it never sees e2, and nobody sees e1. Hot publishers are like a radio broadcast: the station plays whether or not we have tuned in, and tuning in late means we missed the start. Typical hot sources are sinks fed from listeners, UI or sensor events, and a ticker that has already been connected and shared.

4Turning Cold Into Hot With share() and publish()

Sometimes we want one run of a cold source shared by many subscribers, for example one database poll feeding several consumers. publish() turns a Flux into a ConnectableFlux, which does not start until connect() is called. refCount() connects it automatically when the first subscriber arrives and cancels the source when the last one leaves. share() is a shorthand for publish().refCount().

Java
Flux<Long> ticker = Flux.interval(Duration.ofSeconds(1)).share();
ticker.subscribe(t -> log.info("A {}", t)); // starts the interval: A 0, A 1, A 2 ...
Thread.sleep(2500);
ticker.subscribe(t -> log.info("B {}", t)); // joins late: B 2, B 3 ... (no B 0, B 1)

Notice that B starts at tick 2. There is only one interval running, so the source is now hot for its subscribers: they share the work, and a late arrival misses the earlier items.

5Letting Late Subscribers Catch Up With cache() and replay()

When late subscribers must see what they missed, we keep a history. replay() returns a ConnectableFlux that records emitted items, and replay(n) keeps only the last n, which we usually combine with autoConnect() or refCount(). cache() is the convenient form: on the first subscription it runs the source once, stores the signals, and replays them to everyone who subscribes later, including the completion or the error.

Java
Mono<Config> config = loadConfig().cache(); // loadConfig() is a cold remote call
config.subscribe(c -> log.info("A {}", c)); // remote call happens once
config.subscribe(c -> log.info("B {}", c)); // B gets the stored value, no second call
Flux<Long> lastTwo = Flux.interval(Duration.ofSeconds(1)).replay(2).autoConnect();

But there is a catch: cache() without a duration keeps the value, or the error, forever. That is ideal for a value computed once per request, and dangerous for a Mono stored in a long-lived field. cache(Duration) adds a time-to-live for those cases.

6Follow-up questions

Is Mono.just() hot or cold?

It behaves like a hot publisher in one specific sense: the value is captured when we build the Mono, at assembly time, so Mono.just(callApi()) calls the API once immediately, even if nobody ever subscribes. Each subscriber then receives that same captured value. If we want the call to be lazy and repeated per subscription, we use Mono.fromCallable(() -> callApi()) or Mono.defer().

What is the difference between share() and cache()?

share() lets subscribers share one running subscription to the source, but keeps no history, so late subscribers miss earlier items, and when everyone leaves the source is cancelled and a new subscriber starts it again. cache() keeps the history and replays it to every later subscriber, so the source runs once and everyone sees everything.

How do I know if a library's publisher is hot or cold?

By its documentation and by testing: subscribe twice with log() and see whether the source is triggered twice. As a rule, anything that represents a request or a query is cold, and anything that represents ongoing events (a sink, a websocket inbound stream, a message listener bridge) is hot.

7Conclusion

In this answer, we saw that a cold publisher runs its work for every subscriber, so two subscriptions to a WebClient Mono mean two HTTP calls and two subscriptions to Flux.range both start from 1. A hot publisher emits independently of who is listening, as a multicast sink or a shared ticker does, and late subscribers miss earlier items. share(), which is publish().refCount(), turns a cold source into a shared hot one, while cache() and replay() keep a history so late subscribers can catch up, with the caveat that cache() keeps its value forever unless we give it a duration.

What to say in the interviewAbout 90 seconds aloud

Open with

The difference is about when the work happens and who shares it. A cold publisher starts its work for each subscriber. Flux.range, a repository query or a WebClient call are cold: if I subscribe twice to a WebClient Mono, I get two HTTP requests, and two subscribers to range each get the numbers from one.

Then, if they want more

That's usually what I want, but it means side effects run once per subscription. A hot publisher emits regardless of subscribers, like a radio station. A Sinks.many().multicast() sink fed from a listener is the typical example: items go to whoever is subscribed at that moment, and a subscriber that arrives late misses everything emitted before it joined.

A ticker that's been shared behaves the same way. Reactor lets me convert between them. share(), which is publish().refCount(), takes a cold source and runs it once for all current subscribers, connecting on the first subscriber and cancelling when the last leaves; late subscribers still miss earlier items.

If late subscribers need the history, I use replay(n) with autoConnect, or cache(), which runs the source once and replays the stored signals, including errors, to everyone later. I'm careful with cache() on long-lived Monos, because without a duration it keeps the first value forever, so for anything that can change I use cache with a TTL.

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 "What is the difference between a cold and a hot publisher in Reactor? Give an example of...": https://scrivio-interview-library.netlify.app/spring-boot/reactive-fundamentals/8d96c142e5/