Cold Flow vs Hot Flow
Tags: kotlin, android
Published: 2022-12-21 03:53:25

---
title: Cold Flow vs Hot Flow
published: true
description: In this blog, we will learn about Cold Flow vs Hot Flow in Kotlin.
tags: kotlin, android
cover_image: https://dev-to-uploads.s3.amazonaws.com/uploads/articles/ndoyuvzr5elubol9eou6.png
canonical_url: https://amitshekhar.me/blog/cold-flow-vs-hot-flow
---

I am [**Amit Shekhar**](https://amitshekhar.me), a mentor helping developers in getting high-paying tech jobs.

Before we start, I would like to mention that, I have released a video playlist to help you crack the Android Interview: Check out [Android Interview Questions and Answers](https://www.youtube.com/playlist?list=PL_I3TGB7aK6jNBMZkw3FYdJXyf7quHdI8).

**This article was originally published at [amitshekhar.me](https://amitshekhar.me/blog/cold-flow-vs-hot-flow).**

In this blog, we will learn about Cold Flow vs Hot Flow in Kotlin.

This blog is a part of the series I have written on **Flow API in Kotlin**:

- [Mastering Flow API in Kotlin](https://amitshekhar.me/blog/flow-api-in-kotlin)
- [Creating Flow Using Flow Builder in Kotlin](https://amitshekhar.me/blog/creating-flow-using-flow-builder-in-kotlin)
- [Terminal Operators in Kotlin Flow](https://amitshekhar.me/blog/terminal-operators-in-kotlin-flow)
- Cold Flow vs Hot Flow - **YOU ARE HERE**
- [StateFlow and SharedFlow](https://amitshekhar.me/blog/stateflow-and-sharedflow)
- [Long-running tasks in parallel with Kotlin Flow](https://amitshekhar.me/blog/long-running-tasks-in-parallel-with-kotlin-flow)
- [Retry Operator in Kotlin Flow](https://amitshekhar.me/blog/retry-operator-in-kotlin-flow)
- [Retrofit with Kotlin Flow](https://amitshekhar.me/blog/retrofit-with-kotlin-flow)
- [Room Database with Kotlin Flow](https://amitshekhar.me/blog/room-database-with-kotlin-flow)
- [Kotlin Flow Zip Operator for Parallel Multiple Network Calls](https://amitshekhar.me/blog/kotlin-flow-zip-operator-parallel-multiple-network-calls)
- [Instant Search Using Kotlin Flow Operators](https://amitshekhar.me/blog/instant-search-using-kotlin-flow-operators)
- [Exception Handling in Kotlin Flow](https://amitshekhar.me/blog/exception-handling-in-kotlin-flow)
- [Unit Testing ViewModel with Kotlin Flow and StateFlow](https://amitshekhar.me/blog/unit-testing-viewmodel-with-kotlin-flow-and-stateflow)

Let me tabulate the differences between Cold Flow and Hot Flow for your better understanding so that you can decide which one to use based on your use case.

## Cold Flow vs Hot Flow

| Cold Flow                                     | Hot Flow                                       |
| :-------------------------------------------- | :--------------------------------------------- |
| It emits data only when there is a collector. | It emits data even when there is no collector. |
| It does not store data.                       | It can store data.                             |
| It can't have multiple collectors.            | It can have multiple collectors.               |

In Cold Flow, in the case of multiple collectors, the complete flow will begin from the beginning for each one of the collectors, do the task and emit the values to their corresponding collectors. It's like 1-to-1 mapping. 1 Flow for 1 Collector. It means a cold flow can't have multiple collectors as it will create a new flow for each of the collectors.

In Hot Flow, in the case of multiple collectors, the flow will keep on emitting the values, collectors get the values from where they have started collecting. It's like 1-to-N mapping. 1 Flow for N Collectors. It means a hot flow can have multiple collectors.

Let's understand all of those above points from the example code.

**Note:** I have just written the pseudo-code, this is not actual code. This is just written for the sake of understanding this topic in the simplest way.

**Cold Flow example**

Suppose, we have a Cold Flow that emits 1 to 5 at an interval of 1 second.

```kotlin
fun getNumbersColdFlow(): ColdFlow<Int> {
    return someColdflow {
        (1..5).forEach {
            delay(1000)
            emit(it)
        }
    }
}
```

Now, we are collecting:

```kotlin
val numbersColdFlow = getNumbersColdFlow()

numbersColdFlow
    .collect {
        println("1st Collector: $it")
    }

delay(2500)

numbersColdFlow
    .collect {
        println("2nd Collector: $it")
    }
```

The output will be:

```
1st Collector: 1
1st Collector: 2
1st Collector: 3
1st Collector: 4
1st Collector: 5

2nd Collector: 1
2nd Collector: 2
2nd Collector: 3
2nd Collector: 4
2nd Collector: 5
```

Both the collector will get all the values from the beginning. For both collectors, the corresponding Flow starts from the beginning.

**Hot Flow example**

Suppose, we have a Hot Flow that emits 1 to 5 at an interval of 1 second.

```kotlin
fun getNumbersHotFlow(): HotFlow<Int> {
    return someHotflow {
        (1..5).forEach {
            delay(1000)
            emit(it)
        }
    }
}
```

Now, we are collecting:

```kotlin
val numbersHotFlow = getNumbersHotFlow()

numbersHotFlow
    .collect {
        println("1st Collector: $it")
    }

delay(2500)

numbersHotFlow
    .collect {
        println("2nd Collector: $it")
    }
```

The output will be:

```
1st Collector: 1
1st Collector: 2
1st Collector: 3
1st Collector: 4
1st Collector: 5

2nd Collector: 3
2nd Collector: 4
2nd Collector: 5
```

The collectors will get the values from where they have started collecting. Here the 1st collector gets all the values. But the 2nd collector gets only those values that got emitted after 2500 milliseconds as it started collecting after 2500 milliseconds.

Also, we can configure the Hot Flow to store the data. For example, we can configure it to store the last emitted value.

For example, we configured the above example to store only one last emitted value.

```kotlin
fun getNumbersHotFlow(): HotFlow<Int> {
    return someHotflow {
        (1..5).forEach {
            delay(1000)
            emit(it)
        }
    }.store(count = 1)
}
```

Now, if we collect:

```kotlin
val numbersHotFlow = getNumbersHotFlow()

numbersHotFlow
    .collect {
        println("1st Collector: $it")
    }

delay(2500)

numbersHotFlow
    .collect {
        println("2nd Collector: $it")
    }
```

The output will be:

```
1st Collector: 1
1st Collector: 2
1st Collector: 3
1st Collector: 4
1st Collector: 5

2nd Collector: 2
2nd Collector: 3
2nd Collector: 4
2nd Collector: 5
```

The collectors will get an extra value in addition to the values from where they have started collecting. Here the 1st collector gets all the values. But the 2nd collector will also get "2"(as it stores the last emitted value) in addition to those values that got emitted after 2500 milliseconds even though it started collecting after 2500 milliseconds.

This is how Hot Flow can store data.

So, now we have the idea about Cold Flow and Hot Flow.

This was all about the Cold Flow vs Hot Flow in Kotlin.

Both the StateFlow and SharedFlow in Kotlin are Hot Flows. Learn about them: [StateFlow and SharedFlow](https://amitshekhar.me/blog/stateflow-and-sharedflow)

Master Kotlin Coroutines from here: [Mastering Kotlin Coroutines](https://amitshekhar.me/blog/kotlin-coroutines)

That's it for now.

Thanks

[**Amit Shekhar**](https://amitshekhar.me)

You can connect with me on:

- [Twitter](https://twitter.com/amitiitbhu)
- [YouTube](https://www.youtube.com/@amitshekhar)
- [LinkedIn](https://www.linkedin.com/in/amit-shekhar-iitbhu)
- [GitHub](https://github.com/amitshekhariitbhu)

[**Read all of my high-quality blogs here.**](https://amitshekhar.me/blog)