Flow sharing and hot streams
Flow Sharing and Hot Streams Deep Dive¶
Overview¶
stateIn and shareIn convert cold flows into shared hot streams,
helping avoid duplicated upstream work across collectors.
Core Concepts¶
- cold source vs shared hot stream
- replay behavior and memory tradeoffs
SharingStartedpolicies (WhileSubscribed, eager, lazy)- state streams vs event streams
Internal Implementation¶
stateIn maintains a latest value cache and a sharing job.
shareIn uses a shared upstream collector and multicast emissions to downstream
collectors, with configurable replay/buffer behavior.
Threading Model¶
Sharing scope defines lifetime and dispatcher context. Choosing too-wide scope creates long-lived producers and stale work.
Coroutine / Flow Behavior¶
Hot streams continue based on sharing policy, not individual collectors.
Using WhileSubscribed can reduce wasted work in UI-driven contexts.
Code Examples¶
val uiState: StateFlow<UiState> = repository.observeItems()
.map { items -> UiState(items = items) }
.stateIn(
scope = viewModelScope,
started = SharingStarted.WhileSubscribed(5_000),
initialValue = UiState.Loading
)
Common Interview Questions¶
- Q: When should you use
stateInvsshareIn? A: Answer with correctness first and throughput second: cancellation model, dispatcher choice, bounded parallelism, and contention or latency measurements. - Q: What replay value should be used for one-off events? A: Answer with correctness first and throughput second: cancellation model, dispatcher choice, bounded parallelism, and contention or latency measurements.
- Q: How can sharing leak work in long-lived scopes? A: Answer with correctness first and throughput second: cancellation model, dispatcher choice, bounded parallelism, and contention or latency measurements.
- Q: Why can
WhileSubscribedimprove battery/network usage? A: Answer with correctness first and throughput second: cancellation model, dispatcher choice, bounded parallelism, and contention or latency measurements.
Production Considerations¶
- choose replay deliberately
- avoid event duplication with accidental replay > 0
- align sharing scope with lifecycle ownership
- measure upstream subscription churn
Performance Insights¶
Sharing can cut repeated network/db work significantly, but over-sharing can increase memory retention and stale background activity.
Senior-Level Insights¶
Senior design answers should include explicit lifecycle ownership for each hot stream and policies for when producers should stop.