Java Reactive Programming — Cold Publisher vs Hot Publisher
Overview:
As part of our Reactive Programming series, in this article, we are going to discuss the difference between the Cold Vs Hot Publishers. If you have not read the previous articles, please read them first.
- Reactive Programming — A Simple Introduction
- Reactive Programming — Creating Sequences — Flux vs Mono
Publisher Types:
We had already discussed that we have 2 different types of implementations for the Publisher interface — They are Flux vs Mono. Both emit elements asynchronously. While Flux can emit 0 . . . N elements, Mono can emit 0 or 1 element. Based on the their emission behavior, We can categorize the publishers into 2 types. Cold vs Hot publishers.
Cold Publisher:
Imagine Netflix / YouTube. Netflix does not stream movie on its own when nobody is watching. If you login to the Netflix site and wanted to watch your favorite movie, you can start watching it anytime. You and me are 2 different Netflix subscribers. We might watch the same movie from different locations. It is 2 different streaming. I could have been watching the movie for an hour and you could have just started watching the movie. Netflix is like a Cold publisher here.
Publishers by default do not produce any value unless at least 1 observer subscribes to it. Publishers create new data producers for each new subscription.
Lets take a look at this code. We have a simple method which returns an integer.
Lets assume that we would be emitting the element using Mono as shown here.
Mono.fromSupplier(() -> getDataToBePublished());
Now what does the above code do? The above code would not print any value as there is nobody to observe it. We need at least 1 observer. The below code produces the below output as we have 1 observer.
Mono.fromSupplier(() -> getDataToBePublished())
.subscribe(i -> System.out.println("Observer-1 :: " + i));
Output:
getDataToBePublished was called
Observer-1 :: 1
Lets consider this method which is going to stream a movie for us.
Out NetFlux app implementation is as shown here.
Output:
Got the movie streaming request
You are watching scene 1
You are watching scene 2
Got the movie streaming request
You are watching scene 3
Vinsguru is watching scene 1
You are watching scene 4
Vinsguru is watching scene 2
You are watching scene 5
Vinsguru is watching scene 3
Vinsguru is watching scene 4
Vinsguru is watching scene 5
Here from the output, we can understand that each new subscription triggered the getMovie request. That is why we see the ‘Got the movie streaming request‘ twice in the output. You started your movie request first. Even before I start watching the movie, you had already finished 2 scenes in that movie. You and me watch the same movie in parallel — but different scenes. I did not lose any scene/element just because I joined late. Our NetFlux server accepted my request and started streaming the scenes/elements freshly just for me.
Hot Publisher:
Image a Movie theater / Radio station. It does not matter if people are really listening to the radio. They will be always streaming songs/news. Listeners can observe anytime they want. But all listeners get they same info at any given moment. They get the news/songs whatever broadcast in that moment. Observers lose the information if they joined late. Hot Publishers are like this Radio Station.
Hot Publishers do not create new data producer for each new subscription (as the Cold Publisher does). Instead there will be only one data producer and all the observers listen to the data produced by the single data producer. So all the observers get the same data.
Check the below code. Here we just added a ‘share‘ method in our Flux to make the Netflix server into a Movie theater. That is Cold to Hot publisher. Share method triggers the source immediately and make the late subscribers to see the later elements. (There is also a publish method which makes the Cold stream to a Hot stream).
Output:
Got the movie streaming request
You are watching scene 1
You are watching scene 2
You are watching scene 3
Vinsguru is watching scene 3
You are watching scene 4
Vinsguru is watching scene 4
You are watching scene 5
Vinsguru is watching scene 5
From the output, I lost the first 2 scenes in that movie as I joined late. However I am able to watch latest scene played in the theater along with others.
When To Use What:
Well it depends! For ex: Hot Publisher does not create new data producer for each new subscription. We will just have 1 data producer for all the observers. It is a good choice where we do not want to create unnecessary resources. Lets say we are creating an application which will give live updates on the stock prices. We can just have 1 data producer. But we can have multiple observers. All the observers will want to know the latest price updates and they can start listening anytime they want. They all get same updates irrespective of the time they joined. The data produced is not user specific.
On the other hand, Lets assume we are developing an application for placing an order for Pizza. Once you place the order, we want to get the updates on its state. Order placed, Pizza is being cooked, Packed, Ready for delivery etc. Here we can not have the Hot observable. We need a dedicated data producer just to give the order update just for the user who placed the order. Not to share that with others. Here Cold Publisher will be helpful.
Summary:
Hopefully this article gave you an idea about the difference between the Cold vs Hot Publishers, converting a Cold stream into a Hot stream etc with some samples. Each publisher type has its own advantages and we also discussed when to use what.
Happy learning 🙂