DEV Community

Cover image for The 64-Slot Waiting Room: Why flatMapLatest Still Hands Your UI Yesterday's Data
Android Expert
Android Expert

Posted on Originally published at Medium

The 64-Slot Waiting Room: Why flatMapLatest Still Hands Your UI Yesterday's Data

You switched accounts. The upstream Flow emitted the new user ID. flatMapLatest dutifully cancelled the old inner flow. And yet, for a few hundred milliseconds, your screen rendered the previous account's feed.

If that sounds impossible, you are in good company. The name says “latest”, and the KDoc ends with a short remark that is easy to skim past: the operator is buffered by default. This post unpacks that one sentence, shows how it turns into visible bugs on Android, and walks through fixes that you can defend in code review.

The pipeline under suspicion

Here is the shape of code you will find in many view models:

val feed: Flow<FeedRow> = accountIdFlow()
    .flatMapLatest { id -> repository.observeFeed(id) }
    .map { dto -> mapper.toRow(dto) } // sometimes slow
Enter fullscreen mode Exit fullscreen mode

Compiler note:flatMapLatest, mapLatest, and transformLatest have been annotated @ExperimentalCoroutinesApi in the kotlinx.coroutines releases this post was written against. Opt in with @OptIn(ExperimentalCoroutinesApi::class) on the declaration, or module-wide with the compiler argument -opt-in=kotlinx.coroutines.ExperimentalCoroutinesApi. The snippets below omit the annotation for brevity except where it is shown; check the annotation status in your own version.

The mental model most of us carry: when accountIdFlow() emits a new ID, everything about the old ID disappears. The KDoc backs this up with an example involving a fast producer and a slow collector, where earlier work is cancelled in favour of newer work.

That example is about the producer side. It says nothing strong about a slow collector sitting downstream of the operator, and that is exactly where the pipeline above lives.

What “buffered by default” actually means

flatMapLatest is built on transformLatest, which is implemented as a ChannelFlow. A ChannelFlow decouples its producer coroutine from its collector with a channel, and unless you say otherwise that channel uses the default buffered capacity of 64 (Channel.BUFFERED; it can be tuned with the kotlinx.coroutines.channels.defaultBuffer system property, so treat 64 as the default, not a law of nature).

Now trace the data path:

  1. A new key arrives, and flatMapLatest cancels the previous inner flow. No more values will come from it.
  2. Values that the previous inner flow had already sent are sitting in the channel.
  3. The downstream map { } pulls from that channel at its own pace. If it is slow, it works through every queued value in order.

default-pipeline

Cancellation is a gate on the source. It is not a vacuum cleaner for the queue. “Latest” describes which inner flow is allowed to keep producing, not which values the collector will observe next.

Reproducing it deterministically

Race-condition bugs are miserable to chase on a device. Virtual time makes this one boringly reproducible:

@OptIn(ExperimentalCoroutinesApi::class)
@Test
fun `queued values from a cancelled inner flow still reach map`() = runTest {
    val keys = flow {
        emit(1)
        delay(50)
        emit(2)
    }

    val seen = keys
        .flatMapLatest { id ->
            flow {
                repeat(5) { emit("$id-$it") } // fast producer
                delay(1_000)                  // stays "alive" until replaced
            }
        }
        .map { value ->
            delay(100)                        // slow collector
            value
        }
        .take(10)
        .toList()

    assertEquals(
        listOf("1-0", "1-1", "1-2", "1-3", "1-4", "2-0", "2-1", "2-2", "2-3", "2-4"),
        seen,
    )
}
Enter fullscreen mode Exit fullscreen mode

Key 2 is requested at 50 ms, but because each map step costs 100 ms, the first value derived from key 2 is not processed until 500 ms. Everything before it is data for a key that was already replaced.

stale-timeline

Failure modes you will actually meet

1. The account-switch flash. The user taps a different profile. The header updates immediately (it is driven by a cheap flow) while the list below keeps showing the old account’s rows for a moment. Besides looking janky, this is a privacy defect: one account’s data is visible in another account’s session.

2. Search-as-you-type regression. A queryFlow.flatMapLatest { search(it) } pipeline feeds a result mapper that does diffing or formatting. On slow devices the list visibly "replays" older queries before settling on the current one.

3. The false sense of safety from stateIn. StateFlow conflates, so people assume the problem cannot reach the UI. But the conflation happens at the end of the chain. Everything upstream of stateIn, including a slow map, still processes the queued backlog before the latest result is stored.

4. Test flakiness that hides the bug. Tests that use a fast fake repository and an instant mapper never fill the buffer, so they pass. The defect only appears with real I/O timing or a heavy mapper, which is why it tends to be found in production first.

Fix 1: put a one-slot buffer directly after the operator

conflate() is shorthand for buffer(Channel.CONFLATED). The library documents a conflated channel as one that keeps only the latest element, which is what a DROP_OLDEST overflow policy produces on a single slot: when a new value arrives and the slot is occupied, the old value is overwritten. Behaviourally this is the same as buffer(1, BufferOverflow.DROP_OLDEST). (If you spell the policy out manually, buffer(onBufferOverflow = BufferOverflow.DROP_OLDEST) is documented to give the same single-slot result, so you do not need to hand-tune a capacity.) Prefer plain conflate(); it says what you mean.

val feed: Flow<FeedRow> = accountIdFlow()
    .flatMapLatest { id -> repository.observeFeed(id) }
    .conflate()                       // keep only the newest pending value
    .map { dto -> mapper.toRow(dto) }
Enter fullscreen mode Exit fullscreen mode

Placement matters. Because flatMapLatest is a ChannelFlow, a buffering operator placed immediately after it is fused into the same channel and replaces the default capacity. Put conflate() after map { } and you are only adding a buffer after the slow step; the backlog in front of it is untouched.

before-after-conflate

One honest caveat is shown in the figure. conflate() cannot rescue a value that map has already started processing. In the diagram, 1-0 is still handled, because it was in flight when key 2 arrived. Exact output depends on dispatcher scheduling, so assert on the property you care about, namely that the last item is the newest and that far fewer than ten items were processed, rather than pinning the exact list.

Fix 2: make the slow step cancellable

If the mapping is genuinely expensive, dropping queued values is not enough. You want in-flight work to be abandoned too. mapLatest does that:

val feed: Flow<FeedRow> = accountIdFlow()
    .flatMapLatest { id -> repository.observeFeed(id) }
    .mapLatest { dto -> mapper.toRowSuspending(dto) }
Enter fullscreen mode Exit fullscreen mode

mapLatest collects upstream without waiting for the transform to finish, and cancels the running block whenever a newer value arrives. Two warnings from experience:

  • Cancellation is cooperative. A tight CPU loop with no suspension point will run to completion regardless. Insert ensureActive() or yield() in long loops, or move the work to a dispatcher where a cooperative check is natural.
  • Anything with side effects inside the block (writing to a database, posting analytics) must be idempotent or moved after the point where the value is final, since the block can be cut off mid-way.

Fix 3: when correctness matters, verify identity

For account, tenant, or permission boundaries, “usually the newest value” is not a strong enough guarantee. Tag each value with the key that produced it and refuse anything that no longer matches:

private data class Tagged<T>(val accountId: AccountId, val value: T)

@OptIn(ExperimentalCoroutinesApi::class)
class FeedViewModel(
    private val session: AccountSession,
    repository: FeedRepository,
    mapper: FeedUiMapper,
) : ViewModel() {

    val state: StateFlow<FeedUiState> = session.activeAccountId
        .flatMapLatest { id ->
            repository.observeFeed(id).map { Tagged(id, it) }
        }
        .conflate()
        .filter { it.accountId == session.activeAccountId.value }
        .map { mapper.toUiState(it.value) }
        .stateIn(
            scope = viewModelScope,
            started = SharingStarted.WhileSubscribed(5_000),
            initialValue = FeedUiState.Loading,
        )
}
Enter fullscreen mode Exit fullscreen mode

The filter deliberately compares against the current account at the moment the value is about to be used, rather than against whatever was current when the value was produced. That is the point of the check: StateFlow.value is safe to read from any thread, and a snapshot of "who is active right now" is exactly what you want to compare with. If the account changes twice in quick succession (A to B to C), a value tagged B is rejected once C is current, and one tagged C is accepted.

Be clear about what this does and does not guarantee. It is a cheap gate placed before the expensive mapping, not an atomic guarantee: the active account can still change after the check passes and while mapper.toUiState is running. If the stakes are high, repeat the identity check at the commit point, or make the mapping itself cancellable (see Fix 2) so that a switch abandons the work.

Which one should you pick?

fix-comparison

A rough decision order that has served well:

  1. If the downstream work is trivial, leave the defaults alone. Not every flow needs a fix.
  2. If you only need the screen to catch up quickly, add conflate() right after flatMapLatest.
  3. If the downstream work is slow and suspends, prefer mapLatest so you stop paying for results nobody will see.
  4. If showing the wrong entity is a bug rather than an annoyance, add an identity check on top of either approach.

Testing the guarantee, not the implementation

Write the test so it fails on the default pipeline and passes on the fixed one:

@OptIn(ExperimentalCoroutinesApi::class)
@Test
fun `latest value wins and backlog is bounded`() = runTest {
    val keys = flow { emit(1); delay(50); emit(2) }

    val seen = keys
        .flatMapLatest { id ->
            flow { repeat(5) { emit("$id-$it") }; delay(1_000) }
        }
        .conflate()
        .map { delay(100); it }
        .take(2)
        .toList()

    assertEquals("2-4", seen.last()) // newest key, newest value
    assertTrue(seen.size < 10)       // backlog did not replay
}
Enter fullscreen mode Exit fullscreen mode

If you use Turbine, the same assertions work with awaitItem(); the important part is a mapper slow enough, in virtual time, to make the queue observable.

🔚 Takeaways

  • flatMapLatest cancels the old producer; it does not purge values already queued downstream.
  • The operator is channel-backed with a default capacity of 64, so a slow collector can see up to 64 stale values.
  • conflate() directly after the operator shrinks the queue to the newest value, but cannot recall work already in flight.
  • mapLatest cancels in-flight work, but only at suspension points.
  • When showing the wrong data is a correctness or privacy issue, tag values and verify identity before rendering.

The next time you read “latest” in an operator name, ask a second question: latest where? At the producer, at the queue, or at the screen. Only the last one is what your users see.

📱 Level Up Your Android System Design Game

If you’re prepping for a senior Android role and system design rounds are the part that makes you sweat, I highly recommend “Mastering Android System Design” — a 37-chapter playbook that treats Android like the constrained runtime it actually is, not a small server. It covers Binder IPC, ART’s GC, process death, and background execution limits, then walks through the CRDDS interview framework and 13 full worked case studies (chat, ride-hailing, video streaming, payments, and more) carried end-to-end through API design, offline sync, and the Android-specific edge cases that trip candidates up mid-interview.


Originally published on Medium.

Top comments (0)