buffer, conflate and collectLatest

Flow and StateFlow Senior Kotlin 2.0+ API 24+ Reviewed By Best Latest Apps

Predict the output

Values arrive every 100 ms; each takes 250 ms to handle. What does conflate print?

fun main() = runBlocking {
    flow {
        for (i in 1..5) { delay(100); emit(i) }
    }
        .conflate()
        .collect { delay(250); print("$it ") }
}
Options
Hint 1 of 1

Which values arrive while 1 is being handled (100–350 ms)?

Choose an answer, then open Show answer below to check it.

Show answer

Answer: A 1 3 5

Correct. While 1 is handled (100–350 ms), 2 and 3 arrive and only 3 is kept; while 3 is handled (350–600 ms), 4 and 5 arrive and 5 is kept.

Why not the others?
  • B 1 2 3 4 5. That's the default or buffer(): every value is handled.
  • C 5. The first value is handled straight away.
  • D 1 4 5. Conflate keeps the latest value when the collector is free; at 350 ms that is 3, not 4.
Timeline: by default the producer waits for each value to be handled, so all five finish at 1.75 s; with conflate only 1, 3 and 5 are handled, finishing at 0.85 s.0 s0.5 s1 s1.5 sDefault (sequential)handle 1handle 2handle 3handle 4handle 51.75 sconflate()handle 1handle 3handle 50.85 s
Timeline: by default the producer waits for each value to be handled, so all five finish at 1.75 s; with conflate only 1, 3 and 5 are handled, finishing at 0.85 s.
Text version
Timeline as text, elapsed time t in seconds
VersionWhat runs whenFinishes at
Default (sequential)
  • handle 1 from t = 0.1 s to t = 0.35 s
  • handle 2 from t = 0.45 s to t = 0.7 s
  • handle 3 from t = 0.8 s to t = 1.05 s
  • handle 4 from t = 1.15 s to t = 1.4 s
  • handle 5 from t = 1.5 s to t = 1.75 s
t = 1.75 s
conflate()
  • handle 1 from t = 0.1 s to t = 0.35 s
  • handle 3 from t = 0.35 s to t = 0.6 s
  • handle 5 from t = 0.6 s to t = 0.85 s
t = 0.85 s

30-second answer

By default a flow is sequential: the producer waits while the collector works. buffer() lets the producer run ahead into a queue. conflate() keeps only the latest value, dropping ones the collector missed. collectLatest cancels the collector's work for an old value when a new one arrives. Pick based on whether every value matters.

Practise saying it · 35 s. No recording, just a timer

Explain your options for a fast producer.

Say your answer out loud, then check which of these points you covered:

  • Default is sequential
  • buffer queues everything
  • conflate keeps the latest
  • collectLatest cancels old work

Deep dive

Use buffer when every value must be processed, like writing events to disk.

Use conflate for UI rendering or sensor readings where only the latest matters.

Use collectLatest when processing is cancellable and stale work is wasted, like rendering a preview.

StateFlow is already conflated.

Common mistakes

  • Using conflate for analytics events and losing data.
  • Using collectLatest with non-cancellable work, so the cancellation does nothing.
  • Adding buffer(UNLIMITED) and growing memory without limit.

Senior take & follow-ups

Tie each operator to a product case; that shows you choose rather than memorise.

Follow-up questions

Think of your answer first, then open the hint.

  1. What does buffer's onBufferOverflow do?

    Show hint

    Chooses to suspend, drop the oldest or drop the latest when full.

  2. Is collectLatest the same as conflate?

    Show hint

    No: conflate skips values; collectLatest cancels work in progress.

Suggest a fix if something here is wrong or out of date.