Flow operators and backpressure
Flow Operators and Backpressure Deep Dive¶
Overview¶
Operator selection controls cancellation semantics, buffering behavior, and user-visible responsiveness in stream pipelines.
Core Concepts¶
collectLatestfor latest-only renderingflatMapLatestfor source switchingbufferandconflatefor producer/consumer decoupling- backpressure policy as product behavior decision
Internal Implementation¶
Operators insert coroutine boundaries and channels in the pipeline.
collectLatest cancels previous collector block on new emissions.
flatMapLatest cancels previous inner flow when outer source updates.
buffer introduces queueing; conflate keeps only newest queued value.
Threading Model¶
Operator chains may hop dispatchers (flowOn) and create additional scheduling
costs. Keep context shifts explicit to avoid hidden overhead.
Coroutine / Flow Behavior¶
Latest operators improve UI freshness but can drop intermediate work. Buffered chains improve throughput but may increase latency under bursts.
Code Examples¶
queryFlow
.debounce(250)
.flatMapLatest { query -> repository.search(query) }
.flowOn(Dispatchers.Default)
.collectLatest { result ->
render(result)
}
Common Interview Questions¶
- Q: Why does
collectLatestcancel in-flight work? A: Start from delivery semantics: use StateFlow for durable state, SharedFlow or Channel for transient events, and lifecycle-aware collection to prevent duplicate work. - Q: When is
flatMapMergebetter thanflatMapLatest? A: Answer with correctness first and throughput second: cancellation model, dispatcher choice, bounded parallelism, and contention or latency measurements. - Q: What tradeoff does
conflatemake? A: Answer with correctness first and throughput second: cancellation model, dispatcher choice, bounded parallelism, and contention or latency measurements. - Q: How do you debug dropped intermediate emissions? A: State load and SLO assumptions first, identify the first bottleneck, choose scaling and consistency strategy, and explain fallback behavior for partial failures.
Production Considerations¶
- choose operator semantics per UX expectation
- cap buffers for bursty sources
- instrument cancellation and processing latency
- avoid accidental heavy work on main dispatcher
Performance Insights¶
Pipeline performance is dominated by work per emission and queue behavior, not operator count alone.
Senior-Level Insights¶
Strong answers connect operator semantics to explicit product tradeoffs: freshness, completeness, and cost.