Explain backpressure in Kotlin Flows and its management operators

This tests your understanding of Flow's pull-based nature and performance tuning. A great answer defines suspension as the default backpressure, then details how buffer, conflate, and collectLatest optimize it.
What's really being asked
This question probes your deep understanding of Flow's execution model. Interviewers want to confirm you know Flows are pull-based (cold streams) and that their default backpressure mechanism is the suspension of the producer. The real test is whether you can articulate when and why you'd deviate from this simple, effective default by using specific buffering operators for performance optimization in real-world scenarios.
The full answer
First, define the default behavior. Flows are pull-based, meaning the collector requests a value, and the producer's coroutine suspends until the collector is ready to process the next one. This is the built-in backpressure mechanism; no values are dropped or buffered by default.
Second, explain buffer(). This operator introduces a channel between the producer and collector. The producer can emit values into the buffer (e.g., up to the default capacity of 64) without waiting for the collector. This allows the producer and collector to run concurrently, which is useful for smoothing out processing when one has a spiky workload.
Third, describe conflate(). This is a specialized buffer of size one. If the producer emits a new value while the collector is busy, the new value overwrites the previous unprocessed one. The collector only ever gets the most recent value emitted since its last collection. This is ideal when you only care about the latest state and can skip intermediate updates.
Fourth, detail collectLatest(). This is a terminal operator on the collector side. When the producer emits a new value, the current collection block is cancelled and restarted with the new value. This is for expensive, long-running collection logic that should be abandoned in favor of processing a newer item. It's crucial to contrast this with conflate(), which drops values on the producer side without cancelling the collector.
The mistakes people make
Stating that Flows "have a backpressure problem." This is fundamentally incorrect. Their default suspension model is a valid, if simple, backpressure strategy. The operators are for optimization, not fixing a flaw.
Confusing conflate() and collectLatest(). A candidate might say conflate() cancels the collector, or collectLatest() drops producer values. The key difference is what gets interrupted: conflate() drops data from the producer, while collectLatest() cancels work on the collector.
Describing backpressure using RxJava terms (e.g., BackpressureStrategy.DROP). While analogous, a senior answer must use precise Flow terminology and mechanics: suspension, channels, and coroutine cancellation.
What usually comes next
"When would you choose buffer() over conflate()?" (Answer: When you must process every single event, not just the latest, and want to smooth out processing time between a fast producer and a spiky consumer).
"What is the default buffer size for the buffer() operator?" (Answer: 64, and it can be changed via buffer(capacity = N)).
"Can you implement conflate() using buffer()?" (Answer: Yes, it is equivalent to buffer(capacity = 1, onBufferOverflow = BufferOverflow.DROP_OLDEST)).
A concrete example
Imagine a UI displaying a user's location from a Flow emitting coordinates every 100ms, but the UI takes 300ms to render a map update. With the default behavior, the UI would lag, processing an update every 300ms while the producer is suspended 2/3 of the time. Using conflate(), the producer emits freely. The collector, after its 300ms render, would pick up only the very latest coordinate, skipping the two that arrived while it was busy. This keeps the UI responsive with the most current data. Using collectLatest(), if a new coordinate arrives 100ms into a 300ms render, the render is cancelled and restarted with the new coordinate.
Interview question
A Flow emits frequent updates, but the collector's work is slow. To ensure resources are only spent on the latest item by cancelling work on stale ones, which approach is best?
- a.Use `buffer()` to allow the producer to emit freely into a channel without waiting for the slow collector.
- b.Use no operator; the default suspension behavior will slow down the producer to match the collector, which is the most efficient backpressure.
- c.Use `collectLatest()` to cancel the collector's current work block as soon as a new item is emitted from the producer.Correct
- d.Use `conflate()` to drop intermediate items, ensuring the collector only starts work on the most recent item when it becomes available.
Why? this is the answer
`collectLatest` is correct because it cancels the collector's ongoing work when a new value arrives, preventing wasted resources on stale data. `conflate` is a common misconception; it drops values on the producer side but does not cancel any ongoing work in the collector.
Just read this? Test yourself on what you have been reading.
Read the original → kotlinlang.org
- #kotlin
- #coroutines
- #flow
- #backpressure
- #android
You just looked this up. Could you explain it out loud?
That is the part interviews actually test. Tezvyn takes questions like this one and gives you what the interviewer is really checking, the answer that lands, and the mistake that ends the conversation, in the four minutes before your next meeting.
The iPhone app is on the way
We are building it. Until it lands, nothing here is held back from you: every interview card, your saved cards, streaks and the job board all work in Safari, plus hundreds of free practice quizzes of thirty questions each. Sign in and it all carries over to the app the day it arrives.
Want it as an icon? Tap Share at the bottom of Safari, then Add to Home Screen. It opens full screen and the cards you have read stay available offline.
We are hiring for this. Open roles that interview on kotlin — each one lists the topics its interview covers.
See open roles