Asynchronous and Event-Driven Programming Questions
The programming model of a single asynchronous runtime: callbacks, promises and futures, async/await, event loops and task/microtask scheduling (including microtask starvation and where rendering fits), reactive streams and back-pressure, and how ordering, cancellation, timeouts, retries and error propagation behave across JavaScript, Node.js, mobile coroutine or async runtimes, and a comparison with Python asyncio and Java futures. Includes the async mechanics of a data fetch such as parallel versus sequential requests, partial failure, stale or out-of-order responses, abort, debounced live search, single-flight token refresh, and bounded concurrency; wrapping callback APIs as promises, streaming work through async consumers, moving heavy work off the main thread and back, and diagnosing unhandled rejections, race conditions and leaked subscriptions. Excludes message-queue and pub/sub system design, server throughput and thread-pool tuning, per-language threading and memory models, rebuilding standard APIs from scratch, and fetch caching or state architecture.
A screen freezes whenever a heavy piece of work runs on the main thread. How would you fix that on the web, Android and iOS, and how does the result get back onto the UI safely on each platform?
Sample Answer
Direct answer
The screen freezes because the main thread (the one thread that handles taps, runs layout and draws frames, called the UI thread on mobile) is busy and cannot do anything else until the work ends. The fix on every platform is the same in two steps: move the heavy work off the main thread, then deliver only the small result back to the main thread, which is the only place allowed to touch the UI. The tools differ: Web Workers (or splitting the work into short slices that yield) on the web, Kotlin coroutines with Dispatchers on Android, Swift concurrency or Grand Central Dispatch (GCD, Apple's queue library) on iOS.
Web
- Why it freezes. JavaScript runs on the same thread as rendering and input. A browser "long task" is any stretch where that thread is busy for 50 ms or longer (Mozilla's MDN web documentation, Long Tasks), and long tasks cause delayed interaction, input latency and janky scrolling. At 60 frames per second a frame lasts 1000 / 60 = 16.7 ms, so one 300 ms loop skips about 18 frames.
- Option A, Web Worker (offload). A worker is a separate thread with its own global scope. It cannot touch the DOM (the page's document tree); it communicates with the page through
postMessage, and the page updates the DOM in itsonmessagehandler. Messages are copied with the structured clone algorithm (the browser's built-in deep copy for values sent between threads), except transferable objects such as anArrayBuffer, which are moved instead of copied. - Option B, slices that yield (split). Process a few milliseconds of work, then give the event loop a turn (
await new Promise(r => setTimeout(r, 0)), orscheduler.yield()where available; MDN lists it as not Baseline, so feature-detect it and fall back tosetTimeout). Timers, input and rendering run in the gaps. - Trade-offs. Offloading buys real parallelism and the main thread stays free, but you pay for copying data across, the extra thread's start-up, and the fact that the code cannot read the DOM. Slicing needs no new thread and can use the DOM and app state directly, but the work takes longer in total (every yield adds overhead), it still competes with rendering for the same thread, and a step that cannot be divided below one frame still causes jank. A pure computation on a large input goes to a worker; a loop that must update the DOM as it goes is sliced.
The harness measures "is the UI still alive" with an interval that increments a counter every 10 ms, then runs 300 ms of work three ways in Chromium, Firefox and WebKit through Playwright. How to read it: a timer callback can only run when the main thread is free, so the number of ticks gained while the work runs counts how often the page could have responded. Free for 300 ms, the counter would gain about 30; blocked for the whole 300 ms, it gains none (the harness tolerates one that was already due). heavy(ms) is a busy loop that spins for that many milliseconds, standing in for real computation. measure waits 50 ms, records the counter, runs the work, and returns the difference. The three cases are the work inline, the same loop inside a worker created from a Blob URL (a worker built from a string, so no extra file is needed), and 30 slices of 10 ms with a setTimeout yield between them. The printout shows only none or some because exact tick counts differ between browsers and runs.
import { chromium, firefox, webkit } from 'playwright';
const pageScript = async () => {
const heavy = (ms) => { const end = performance.now() + ms; let x = 0; while (performance.now() < end) x += Math.sqrt(x + 1); return x; };
let ticks = 0;
const ticker = setInterval(() => ticks++, 10); // stands in for "the UI is still alive"
const measure = async (label, fn) => {
await new Promise((r) => setTimeout(r, 50)); // let the ticker settle
const before = ticks;
await fn();
return { label, ticksDuringWork: ticks - before };
};
const results = [];
// 1. heavy work inline on the main thread
results.push(await measure('inline on main thread', async () => { heavy(300); }));
// 2. same work in a Web Worker; the result comes back with postMessage
const workerSrc = `onmessage = (e) => { const end = performance.now() + e.data; let x = 0; while (performance.now() < end) x += Math.sqrt(x + 1); postMessage('done'); };`;
const worker = new Worker(URL.createObjectURL(new Blob([workerSrc], { type: 'text/javascript' })));
results.push(await measure('Web Worker', () => new Promise((resolve) => { worker.onmessage = () => resolve(); worker.postMessage(300); })));
worker.terminate();
// 3. same work split into 10 ms slices on the main thread, yielding between slices
results.push(await measure('10 ms slices on main thread', async () => {
for (let spent = 0; spent < 300; spent += 10) { heavy(10); await new Promise((r) => setTimeout(r, 0)); }
}));
clearInterval(ticker);
return results;
};
for (const [name, type] of [['chromium', chromium], ['firefox', firefox], ['webkit', webkit]]) {
const browser = await type.launch();
const page = await browser.newPage();
const results = await page.evaluate(`(${pageScript})()`);
console.log(`[${name}]`);
for (const r of results) console.log(` ${r.label.padEnd(30)} ticks during work: ${r.ticksDuringWork > 0 ? 'some' : 'none'}`);
const [inline, worker, sliced] = results;
if (inline.ticksDuringWork > 1) throw new Error('inline work should freeze the ticker');
if (worker.ticksDuringWork < 5 || sliced.ticksDuringWork < 3) throw new Error('offloaded or sliced work should keep the ticker alive');
await browser.close();
}
Run in the Playwright v1.48.2 container (Chromium 130, Firefox 131, WebKit 18.0) as shown in the last section, it prints the block below once per browser, headed [chromium], [firefox] and [webkit], with identical lines (the code throws if the pattern differs; WebKit in Playwright is the engine, not Safari on iOS):
[chromium]
inline on main thread ticks during work: none
Web Worker ticks during work: some
10 ms slices on main thread ticks during work: some
Android
Android raises an ANR (Application Not Responding) dialog when the app does not respond to an input event within 5 seconds (Android developer documentation), and the same documentation says not to do blocking or long-running work on the main thread but to offload it with Kotlin coroutines to background dispatchers such as Dispatchers.IO or Dispatchers.Default. A dispatcher is the object that decides which thread runs a coroutine. Use Dispatchers.Default for CPU-bound work and Dispatchers.IO for blocking I/O. In a ViewModel, viewModelScope starts coroutines on Dispatchers.Main; withContext(Dispatchers.Default) { ... } moves the heavy part away and returns to the main thread automatically when it finishes, so the next line can safely publish the result. StrictMode can flag accidental disk or network access on the main thread during development.
The program below stands in for viewModelScope with a scope bound to a single thread named main-thread, and counts ticks of a ticker running on that thread, the same measurement as on the web. The ticker coroutine can only run while that thread is free. heavy(300) called directly blocks the thread, so the ticker gets no turns (0 ticks). withContext(Dispatchers.Default) suspends the coroutine instead of blocking, so the thread is free for the ticker while the work runs elsewhere, and the coroutine resumes on main-thread afterwards (the check on the thread name verifies that). The thread is a daemon thread so that a failed check ends the process with an error instead of leaving the ticker running and the program hanging:
import kotlinx.coroutines.*
import java.util.concurrent.Executors
fun heavy(ms: Long) { val end = System.nanoTime() + ms * 1_000_000; var x = 0.0; while (System.nanoTime() < end) x += Math.sqrt(x + 1) }
fun main() = runBlocking {
val mainThread = Executors.newSingleThreadExecutor { Thread(it, "main-thread").apply { isDaemon = true } }.asCoroutineDispatcher()
val uiScope = CoroutineScope(mainThread + SupervisorJob()) // on Android: viewModelScope (bound to Dispatchers.Main)
var uiState = "idle"
var ticks = 0
uiScope.launch { while (true) { ticks++; delay(10) } } // stands in for "the UI is still alive"
delay(50)
// Both blocks run on the main thread; the ticker can only run while they are suspended.
var inlineTicks = -1
var offloadTicks = -1
uiScope.launch {
val before = ticks
heavy(300) // blocks the main thread
inlineTicks = ticks - before
}.join()
uiScope.launch {
val before = ticks
val result = withContext(Dispatchers.Default) { heavy(300); "done" } // runs off the main thread
check(Thread.currentThread().name == "main-thread") // back on main to publish
uiState = result
offloadTicks = ticks - before
}.join()
println("ticks while blocking inline: $inlineTicks")
println("ticks while offloaded: ${if (offloadTicks >= 5) "several" else "$offloadTicks"}")
check(inlineTicks == 0) ; check(offloadTicks >= 5); check(uiState == "done")
mainThread.close()
}
Compiled and run as shown in the last section (Kotlin 2.0.21, kotlinx-coroutines 1.9.0, JDK 21), it prints:
ticks while blocking inline: 0
ticks while offloaded: several
iOS
An actor is a Swift type that lets only one piece of code at a time touch its state, and a global actor is a single shared actor that any type or function can be marked with. The main actor is the global actor that corresponds to the main thread (SE-0316, a Swift Evolution proposal, one of the language's design documents). Main actor isolation means that code and state marked @MainActor, such as a view model, run there and other code must await to reach them, so a heavy synchronous call inside it freezes the interface. Two ways out:
- Swift concurrency. Run the work in a
Task.detached(a detached task does not inherit the actor context, SE-0304),awaitits value, and the continuation resumes on the main actor. A non-isolatedasyncfunction also runs off the caller's actor in the Swift language modes through Swift 6 (SE-0338); Swift 6.2 introduced theNonisolatedNonsendingByDefaultfeature (SE-0461) that changes the default to the caller's actor when enabled, and@concurrentmarks a function that always leaves it. Being explicit withTask.detachedor@concurrentavoids depending on that setting. - GCD.
DispatchQueue.global(qos:).async { heavy work; DispatchQueue.main.async { publish result } }. Theqos(quality of service) argument tells the system how urgent the work is;.userInitiatedis for work the user is waiting on. A checked continuation (withCheckedContinuation) turns a callback into something that can be awaited: the awaiting code resumes whencontinuation.resumeis called, and the "checked" part makes the runtime report a continuation resumed zero or two times.
import Foundation
func heavy(milliseconds: Int) { // synchronous CPU work
let end = DispatchTime.now().uptimeNanoseconds + UInt64(milliseconds) * 1_000_000
var x = 0.0
while DispatchTime.now().uptimeNanoseconds < end { x += (x + 1).squareRoot() }
}
@MainActor
final class ScreenModel {
var ticks = 0
var state = "idle"
var inlineTicks = -1, offloadTicks = -1, gcdTicks = -1
func startTicker() { // stands in for "the UI is still alive"
Task { while true { ticks += 1; try? await Task.sleep(nanoseconds: 10_000_000) } }
}
func loadInline() async {
let before = ticks
heavy(milliseconds: 300) // runs on the main actor: everything else waits
inlineTicks = ticks - before
}
func loadOffloaded() async {
let before = ticks
let result = await Task.detached { // leaves the main actor
heavy(milliseconds: 300)
return "done"
}.value
MainActor.preconditionIsolated() // the continuation is back on the main actor
state = result
offloadTicks = ticks - before
}
func loadWithGCD() async {
let before = ticks
let result: String = await withCheckedContinuation { continuation in
DispatchQueue.global(qos: .userInitiated).async { // background queue
heavy(milliseconds: 300)
DispatchQueue.main.async { continuation.resume(returning: "done") } // back on the main queue
}
}
MainActor.preconditionIsolated()
state = result
gcdTicks = ticks - before
}
}
let model = ScreenModel()
model.startTicker()
try? await Task.sleep(nanoseconds: 50_000_000)
await model.loadInline()
await model.loadOffloaded()
await model.loadWithGCD()
print("ticks while blocking inline:", model.inlineTicks)
print("ticks while offloaded with Task.detached:", model.offloadTicks >= 5 ? "several" : String(model.offloadTicks))
print("ticks while offloaded with GCD:", model.gcdTicks >= 5 ? "several" : String(model.gcdTicks))
precondition(model.inlineTicks == 0 && model.offloadTicks >= 5 && model.gcdTicks >= 5 && model.state == "done")
The ticker here is a Task created in the @MainActor model, so it runs on the main actor and only gets a turn when the main actor is free: loadInline holds it for 300 ms (0 ticks), while the Task.detached and GCD versions suspend the main actor at the await (several ticks).
Compiled with Swift 6.0.3 (swiftc -swift-version 5, the swift:6.0 container, aarch64 Linux), it prints:
ticks while blocking inline: 0
ticks while offloaded with Task.detached: several
ticks while offloaded with GCD: several
Pitfalls that apply on all three
- The result must come back to the main thread. Mutating UI state from the worker is a crash or a race on every platform. Workers have no DOM at all; Kotlin and Swift code is structured so the last step runs on
Mainor@MainActor. - Offloading is not free. Copying a large payload between threads can cost as much as the work, so hand over the smallest possible input and result.
- Cancel what the user left. Cancel the coroutine, the
Task, or terminate the worker when the screen goes away, or the work finishes into a dead UI. - Show progress. A responsive thread with no feedback still feels broken; Android's documentation recommends progress indicators for background work.
Running the code
# web: save the browser listing as freeze.mjs (the .mjs extension is needed for import and top-level await on the Node 20 in this image)
docker run --rm -v "$PWD":/w -w /w mcr.microsoft.com/playwright:v1.48.2-jammy sh -c 'npm i playwright@1.48.2 && node freeze.mjs'
# Android: save the Kotlin listing as A.kt; kotlinc 2.0.21 unzipped to ./kotlinc and kotlinx-coroutines-core-jvm-1.9.0.jar (Maven Central) in the folder
docker run --rm -v "$PWD":/w -w /w eclipse-temurin:21-jdk sh -c \
'kotlinc/bin/kotlinc A.kt -cp kotlinx-coroutines-core-jvm-1.9.0.jar -d out && java -cp out:kotlinc/lib/kotlin-stdlib.jar:kotlinx-coroutines-core-jvm-1.9.0.jar AKt'
# iOS: save the Swift listing as main.swift (top-level await needs that file name)
docker run --rm -v "$PWD":/w -w /w swift:6.0 sh -c 'swiftc -swift-version 5 main.swift -o app && ./app'
A phone streams sensor readings at a high rate. You must transform them off the main thread, keep memory bounded when processing falls behind, and hand finished batches to the UI with low latency. Build this as a reactive pipeline in RxSwift, RxKotlin or Flow and explain what you chose at each stage and why.
Sample Answer
Direct answer
The sensor cannot be slowed down, so the pipeline must decide what to do when it is outrun, and it must decide explicitly. My choice: take readings from the platform callback without ever blocking it, put them into a bounded buffer that drops the oldest reading when full (sensor data is perishable: a reading loses its value within moments, so the newest one matters most), transform on a background thread, group results into batches of at most 32 or 50 ms, whichever comes first, and deliver each batch on the UI thread. The pipeline is shown in Kotlin Flow (the option built on coroutines, where work started inside a scope is cancelled when the scope ends), in RxJava 3 Flowable (the library RxKotlin wraps) and in Swift with a built-in AsyncStream; all three were compiled and run. Memory is bounded because every queue in the chain has a fixed size; the batch time limit keeps latency low when the sensor is quiet; cancelling the consumer unregisters the sensor listener.
Stage by stage
Back-pressure means the slow consumer limiting how fast the producer may send. A hardware sensor callback cannot be told to wait, so here "back-pressure" has to be a policy for the surplus: drop, keep the latest, buffer without limit (unsafe), or fail.
| Stage | Flow | RxJava 3 / RxKotlin | Why |
|---|---|---|---|
| Source | callbackFlow { register; trySend; awaitClose { unregister } } | Flowable.create(..., BackpressureStrategy.MISSING) with setCancellable | Wraps the callback. trySend / onNext never suspend, so the sensor thread is never blocked. awaitClose / setCancellable unregister the listener on cancel, so a closed screen stops draining the battery. |
| Overflow policy | .buffer(64, BufferOverflow.DROP_OLDEST) | .onBackpressureBuffer(64, null, DROP_OLDEST) | Bounded memory. Drop oldest because a stale reading is worth less than a fresh one. conflate() is the same with capacity 1 and suits "latest value only" UI. Documented: buffer with a non-SUSPEND overflow never suspends its upstream. |
| Off the main thread | .map(transform).flowOn(Dispatchers.Default) | .observeOn(Schedulers.computation(), false, 16) then .map | flowOn changes the context of everything above it, not below. The observeOn prefetch of 16 bounds its queue. |
| Batching | custom batched(32, 50 ms) | .buffer(50, MILLISECONDS, 32) | Fewer UI updates, but the time limit means a slow sensor still delivers a partial batch. |
| Empty batches | not produced | .filter { it.isNotEmpty() } | RxJava's buffer(time, count) also emits empty lists on quiet ticks; without the filter the UI receives empty batches (the test below fails when the filter is removed). |
| To the UI | collect in the UI dispatcher | .observeOn(ui) | Only the final, small stream touches the UI thread. |
Terms in the table: callbackFlow turns a listener-style API into a Flow; trySend hands one value to that Flow and returns at once instead of waiting for room; awaitClose keeps the Flow open until the collector cancels and then runs its cleanup block. In Rx, Flowable.create does the same job: BackpressureStrategy.MISSING means "apply no policy here" (the next operator supplies it), and setCancellable registers the cleanup that runs on dispose. A prefetch is how many items an operator requests from upstream ahead of need, so observeOn(..., 16) keeps at most 16 queued between the threads. Schedulers.computation() is RxJava's fixed pool of threads sized for CPU-bound work, the counterpart of Dispatchers.Default. DROP_OLDEST discards the longest-waiting item when the buffer is full.
The Flow pipeline builds its batches in the collector's context, so collecting on the UI dispatcher delivers them there. Flow has no built-in "count or time" operator, which is why batched is written out: a channelFlow with a mutex-guarded list, a ticker coroutine that flushes after the wait, a flush when the list reaches the size limit, and a final flush when the stream ends. In order: the collector appends each reading to batch under the lock and, if that makes 32, calls flush() at once. flush() swaps in a fresh list under the lock and sends the full one outside it, so a slow consumer never holds the lock. The ticker coroutine calls the same flush() every 50 ms, which delivers whatever has accumulated (it is not reset per batch, so a flush can come sooner than 50 ms after a batch started). The lock is there because the collector and the ticker are two coroutines that can interleave, or run on different threads under a multi-threaded dispatcher, and both touch batch. When the sensor stops, the ticker is cancelled and the leftover items go out in a last flush.
Which to pick. For new Android code, Flow: cancellation is tied to a coroutine scope, there is no separate disposable to forget, and callbackFlow is built for listener APIs. Choose RxKotlin or RxJava when the app already standardises on Rx and needs its operator set. Both express the same four decisions above. RxSwift's documentation covers observeOn, subscribeOn, MainScheduler and background schedulers for the off-main-thread and UI-delivery stages, but documents no back-pressure mechanism, so on iOS the drop-oldest policy would have to be applied where the sensor callback hands values to Rx (a small bounded buffer of your own) rather than being an operator in the chain. Swift's standard library has such a buffer: AsyncStream with .bufferingNewest(64) keeps at most 64 waiting values and discards the oldest when a new one arrives. A producer thread stands in for the sensor callback (yield never blocks it), and a for await loop is the slow consumer:
import Foundation
struct Reading: Sendable { let seq: Int; let x: Double }
func transform(_ r: Reading) -> Int { // CPU work per reading, slower than the producer
var acc = r.x
for _ in 0..<3_000 { acc = (acc * acc + 1.0).squareRoot() }
return r.seq
}
let total = 100_000
// bufferingNewest(64): when 64 readings are waiting, the oldest is discarded to make room.
let (stream, continuation) = AsyncStream.makeStream(of: Reading.self, bufferingPolicy: .bufferingNewest(64))
// Stand-in for the platform sensor callback: its own thread, never waits for the consumer.
let producer = Thread {
for i in 1...total { continuation.yield(Reading(seq: i, x: Double(i))) } // yield never blocks
continuation.finish()
}
producer.start()
// Consumer: leaves the producer's thread behind and processes one reading at a time.
var seen: [Int] = []
for await reading in stream {
seen.append(transform(reading))
}
func report(_ name: String, _ ok: Bool) { print(ok ? "PASS" : "FAIL", name); precondition(ok) }
report("some readings were dropped", seen.count < total)
report("the newest reading was delivered", seen.last == total)
report("order preserved", zip(seen, seen.dropFirst()).allSatisfy { $0 < $1 })
Compiled with Swift 6.0.3 (swiftc -swift-version 5, the swift:6.0 container, aarch64 Linux), it prints:
PASS some readings were dropped
PASS the newest reading was delivered
PASS order preserved
The values from this stream can be handed to an Rx pipeline (or consumed directly) on the main actor for the UI stage; the same three decisions (never block the callback, drop oldest at a fixed bound, process off the main thread) apply.
The code and its tests
A fake sensor stands in for the platform's sensor manager: it calls listeners on its own thread as fast as the producer loop runs (100,000 readings in a tight loop). The transform does CPU work per reading so it is slower than the producer. The checks assert properties, not exact counts, because how many readings are dropped depends on timing: some are dropped, the newest reading is always delivered, order is preserved, no batch exceeds 32, the transform ran on a background thread, delivery was on the UI thread, a quiet sensor still produces a partial batch, a finished stream flushes its last partial batch, a quiet tick produces no empty batch, and cancelling unregisters the listener. A failed check throws an AssertionError and the program exits with a non-zero status (the UI executor's thread is a daemon thread, so the JVM does not wait for it). A control pipeline with an unbounded buffer must still have most of its input queued when the producer stops, showing what the bound prevents.
import io.reactivex.rxjava3.core.BackpressureOverflowStrategy
import io.reactivex.rxjava3.core.Flowable
import io.reactivex.rxjava3.core.BackpressureStrategy
import io.reactivex.rxjava3.schedulers.Schedulers
import kotlinx.coroutines.*
import kotlinx.coroutines.channels.BufferOverflow
import kotlinx.coroutines.flow.*
import kotlinx.coroutines.channels.awaitClose
import kotlinx.coroutines.sync.Mutex
import kotlinx.coroutines.sync.withLock
import java.util.concurrent.CopyOnWriteArrayList
import java.util.concurrent.Executors
import java.util.concurrent.TimeUnit
import java.util.concurrent.atomic.AtomicInteger
data class Reading(val seq: Int, val x: Double)
data class Sample(val seq: Int, val magnitude: Double, val thread: String)
// A stand-in for SensorManager: the platform calls the listener on ITS thread, whenever it likes, and cannot be slowed down.
class FakeSensor {
private val listeners = CopyOnWriteArrayList<(Reading) -> Unit>()
val listenerCount get() = listeners.size
fun register(l: (Reading) -> Unit) { listeners.add(l) }
fun unregister(l: (Reading) -> Unit) { listeners.remove(l) }
fun emit(r: Reading) { listeners.forEach { it(r) } }
}
fun transform(r: Reading): Sample { // deliberately slower than the producer: CPU work per reading
var acc = r.x
repeat(3_000) { acc = Math.sqrt(acc * acc + 1.0) }
return Sample(r.seq, acc, Thread.currentThread().name)
}
// ---------- Kotlin Flow ----------
fun FakeSensor.readings(): Flow<Reading> = callbackFlow {
val listener: (Reading) -> Unit = { trySend(it) } // never suspends: a sensor callback must not block
register(listener)
awaitClose { unregister(listener) } // runs when the collector is cancelled
}
// Flow has no built-in "size or time, whichever comes first" operator, so this one is written out.
fun <T> Flow<T>.batched(maxSize: Int, maxWaitMs: Long): Flow<List<T>> = channelFlow {
val lock = Mutex()
var batch = ArrayList<T>(maxSize)
suspend fun flush() {
val out: ArrayList<T>? = lock.withLock { if (batch.isEmpty()) null else batch.also { batch = ArrayList(maxSize) } }
if (out != null) send(out)
}
val ticker = launch { while (true) { delay(maxWaitMs); flush() } } // a quiet sensor still delivers what it has
collect { v ->
val full = lock.withLock { batch.add(v); batch.size >= maxSize }
if (full) flush()
}
ticker.cancelAndJoin()
flush() // the final partial batch when the stream ends
}
fun flowPipeline(sensor: FakeSensor): Flow<List<Sample>> =
sensor.readings()
.buffer(capacity = 64, onBufferOverflow = BufferOverflow.DROP_OLDEST) // bounded memory: keep the newest 64
.map(::transform) // heavy work ...
.flowOn(Dispatchers.Default) // ... runs on a background thread; everything above this line
.batched(maxSize = 32, maxWaitMs = 50) // batches are built and delivered in the collector's context
// ---------- RxJava 3 (what RxKotlin wraps) ----------
fun rxPipeline(sensor: FakeSensor, ui: io.reactivex.rxjava3.core.Scheduler): Flowable<List<Sample>> =
Flowable.create<Reading>({ emitter ->
val listener: (Reading) -> Unit = { emitter.onNext(it) }
sensor.register(listener)
emitter.setCancellable { sensor.unregister(listener) }
}, BackpressureStrategy.MISSING) // the sensor cannot be slowed, so choose the policy explicitly next
.onBackpressureBuffer(64, null, BackpressureOverflowStrategy.DROP_OLDEST)
.observeOn(Schedulers.computation(), false, 16) // hand over to a background thread with a small prefetch
.map(::transform)
.buffer(50, TimeUnit.MILLISECONDS, 32) // up to 32 items or 50 ms, whichever first
.filter { it.isNotEmpty() } // buffer(time, count) also emits EMPTY lists on quiet ticks
.observeOn(ui) // deliver on the UI thread
fun check(name: String, ok: Boolean) { println("${if (ok) "PASS" else "FAIL"} $name"); if (!ok) throw AssertionError(name) }
fun main() = runBlocking {
val uiThread = Executors.newSingleThreadExecutor { Thread(it, "ui-thread").apply { isDaemon = true } } // daemon: a failed check must end the JVM, not hang it
val uiDispatcher = uiThread.asCoroutineDispatcher()
val total = 100_000
println("== Flow ==")
val sensor = FakeSensor()
val batches = ArrayList<List<Sample>>()
val collectorThreads = HashSet<String>()
val job = launch(uiDispatcher) {
flowPipeline(sensor).collect { batches.add(it); collectorThreads.add(Thread.currentThread().name) }
}
while (sensor.listenerCount == 0) delay(5)
val producer = Thread { for (i in 1..total) sensor.emit(Reading(i, i.toDouble())) } // hammer: ~no pauses
producer.start(); producer.join()
delay(1_000) // let the pipeline drain; slow tail is dropped data, not backlog
val all = batches.flatten()
check("some readings were dropped because processing fell behind", all.size < total)
check("the newest reading was delivered", all.last().seq == total)
check("order preserved (strictly increasing seq)", all.zipWithNext().all { (a, b) -> a.seq < b.seq })
check("no batch larger than 32", batches.all { it.size <= 32 })
check("transform ran off the main thread", all.all { it.thread.startsWith("DefaultDispatcher-worker") })
check("batches were delivered on the ui thread", collectorThreads == setOf("ui-thread"))
job.cancelAndJoin()
check("cancelling the collector unregistered the sensor listener", sensor.listenerCount == 0)
println("== Flow: slow sensor still delivers a partial batch ==")
val slow = FakeSensor()
val got = CompletableDeferred<List<Sample>>()
val job2 = launch(uiDispatcher) { flowPipeline(slow).collect { got.complete(it) } }
while (slow.listenerCount == 0) delay(5)
repeat(3) { slow.emit(Reading(it + 1, 1.0)) }
val partial = withTimeoutOrNull(2_000) { got.await() }
check("3 readings arrive as one partial batch without waiting for 32", partial?.size == 3)
job2.cancelAndJoin()
println("== Flow: batching edge cases ==")
check("a finished stream flushes its last partial batch", flowOf(1, 2, 3, 4, 5).batched(32, 10_000).toList() == listOf(listOf(1, 2, 3, 4, 5)))
check("quiet ticks produce no empty batch", flow { delay(300); emit(1) }.batched(32, 50).toList() == listOf(listOf(1)))
println("== Control: unbounded buffer keeps everything ==")
val s3 = FakeSensor()
val received = AtomicInteger()
val job3 = launch(uiDispatcher) {
s3.readings().buffer(kotlinx.coroutines.channels.Channel.UNLIMITED).map(::transform).flowOn(Dispatchers.Default).collect { received.incrementAndGet() }
}
while (s3.listenerCount == 0) delay(5)
val p3 = Thread { for (i in 1..total) s3.emit(Reading(i, i.toDouble())) }
p3.start(); p3.join()
val backlogRightAfterProducerStops = total - received.get()
check("unbounded: most of the input is still queued when the producer stops", backlogRightAfterProducerStops > total / 2)
job3.cancelAndJoin()
println("== RxJava 3 ==")
val rxSensor = FakeSensor()
val rxBatches = java.util.Collections.synchronizedList(ArrayList<List<Sample>>())
val rxThreads = java.util.Collections.synchronizedSet(HashSet<String>())
val uiScheduler = Schedulers.from(uiThread)
val disposable = rxPipeline(rxSensor, uiScheduler).subscribe { rxBatches.add(it); rxThreads.add(Thread.currentThread().name) }
val rp = Thread { for (i in 1..total) rxSensor.emit(Reading(i, i.toDouble())) }
rp.start(); rp.join()
delay(1_500)
val rxAll = rxBatches.flatten()
check("rx: some readings were dropped", rxAll.size < total)
check("rx: the newest reading was delivered", rxAll.last().seq == total)
check("rx: no empty batch reached the UI", rxBatches.none { it.isEmpty() })
check("rx: no batch larger than 32", rxBatches.all { it.size <= 32 })
check("rx: transform ran on a computation thread", rxAll.all { it.thread.startsWith("RxComputationThreadPool") })
check("rx: delivered on the ui thread", rxThreads == setOf("ui-thread"))
disposable.dispose()
check("rx: dispose unregistered the listener", rxSensor.listenerCount == 0)
uiThread.shutdown()
}
Run in an eclipse-temurin:21 container (JDK 21, Kotlin 2.0.21, kotlinx-coroutines 1.9.0, RxJava 3.1.9), the program printed the following and exited with status 0 on every repeated run:
== Flow ==
PASS some readings were dropped because processing fell behind
PASS the newest reading was delivered
PASS order preserved (strictly increasing seq)
PASS no batch larger than 32
PASS transform ran off the main thread
PASS batches were delivered on the ui thread
PASS cancelling the collector unregistered the sensor listener
== Flow: slow sensor still delivers a partial batch ==
PASS 3 readings arrive as one partial batch without waiting for 32
== Flow: batching edge cases ==
PASS a finished stream flushes its last partial batch
PASS quiet ticks produce no empty batch
== Control: unbounded buffer keeps everything ==
PASS unbounded: most of the input is still queued when the producer stops
== RxJava 3 ==
PASS rx: some readings were dropped
PASS rx: the newest reading was delivered
PASS rx: no empty batch reached the UI
PASS rx: no batch larger than 32
PASS rx: transform ran on a computation thread
PASS rx: delivered on the ui thread
PASS rx: dispose unregistered the listener
Which check proves which decision: "some readings were dropped" and the unbounded control show the bound is real (the control keeps everything and falls behind); "the newest reading was delivered" shows the policy is drop-OLDEST rather than drop-newest; "order preserved" shows dropping never reorders; "no batch larger than 32" and "3 readings arrive as one partial batch" show the size limit and the 50 ms limit; the two batching edge cases show that the Flow batched operator flushes the leftover items when the stream ends and never emits an empty list (each check fails when the matching line in flush() or the final flush() is removed); "transform ran off the main thread" shows the flowOn / observeOn(computation) stage; "delivered on the ui thread" shows the final hop; "cancelling ... unregistered" and "dispose unregistered" show the awaitClose / setCancellable cleanup; "no empty batch reached the UI" shows the filter.
Trade-offs and pitfalls
- Dropping is a product decision. For a live chart or a gesture it is right. If every sample must be kept (for example a recording), do not drop: write to a file or database in the background and let the UI read a downsampled view.
- Capacity 64 and batch limits 32 / 50 ms are starting points, not rules. Size the buffer to cover the longest pause you expect from the transform (a garbage-collection pause or a slow frame), and size the batch time to the UI's refresh need.
- Unbounded buffers hide the problem until the app runs out of memory.
Channel.UNLIMITEDor RxonBackpressureBuffer()with no capacity is the control in the test. flowOnonly moves what is above it. Putting it after the collector's code does not move the collector.- Never do work in the sensor callback. Do the minimum (
trySend) and return. - Lifecycle: collect only while the screen is visible, so the listener is registered only when someone is looking.
Running the code
mkdir demo && cd demo && cp /path/to/SensorPipeline.kt .
curl -fsSL -O https://github.com/JetBrains/kotlin/releases/download/v2.0.21/kotlin-compiler-2.0.21.zip && jar xf kotlin-compiler-2.0.21.zip
curl -fsSL -o cor.jar https://repo1.maven.org/maven2/org/jetbrains/kotlinx/kotlinx-coroutines-core-jvm/1.9.0/kotlinx-coroutines-core-jvm-1.9.0.jar
curl -fsSL -o rx.jar https://repo1.maven.org/maven2/io/reactivex/rxjava3/rxjava/3.1.9/rxjava-3.1.9.jar
curl -fsSL -o rs.jar https://repo1.maven.org/maven2/org/reactivestreams/reactive-streams/1.0.4/reactive-streams-1.0.4.jar
bash kotlinc/bin/kotlinc SensorPipeline.kt -cp cor.jar:rx.jar:rs.jar -d out
java -cp out:cor.jar:rx.jar:rs.jar:kotlinc/lib/kotlin-stdlib.jar SensorPipelineKt
# Swift: save the Swift listing as sensor_stream.swift, then in the swift:6.0 image run
swiftc -swift-version 5 sensor_stream.swift -o sensor_stream && ./sensor_stream
A mobile search field fires a network request as the user types. Using Swift concurrency or Kotlin coroutines, how do you wait for a pause in typing, make sure an older in-flight search cannot overwrite a newer one, and update the UI only on the main thread? Show the code.
Sample Answer
Direct answer
Three separate guarantees, each with its own mechanism. Wait for a pause: debounce (hold a value back until no newer one arrives for a set time). No stale overwrite: every new keystroke cancels the previous search, and the code that publishes a result checks it is still current, because cancellation is cooperative and a call that ignores it can still return late. Main thread only for the UI write: the search runs off the main thread and only the final assignment to UI state happens on it. In Kotlin that is debounce, mapLatest and flowOn/withContext on a Flow; in Swift it is a stored Task that is cancelled on the next keystroke, Task.sleep as the debounce, and a @MainActor model.
Kotlin: Flow pipeline
A Flow is Kotlin's asynchronous stream of values. The pipeline reads top to bottom as the three requirements:
query.debounce(100)waits for a pause. The Flow documentation defines it as filtering out values that are followed by a newer value within the timeout, and the latest value is always emitted. (The demo uses 100 ms so it runs quickly; a real search box typically waits a few hundred milliseconds, a product choice.)debounceis marked@FlowPreview, so it needs an opt-in annotation..mapLatest { q -> withContext(io) { search(q) } }cancels the running lookup when a new query reaches it.withContext(io)moves the blocking network call to the IO dispatcher (a pool of background threads), and the Kotlin documentation guarantees that if the caller is cancelled by the time the result would be delivered back, the result is discarded and aCancellationExceptionis thrown instead..collect { ... }runs in the collecting scope's context. On Android the scope isviewModelScope, which is documented to useDispatchers.Main, so the UI write happens on the main thread without an explicit hop. The demo uses a single-thread executor namedmain-threadin its place andchecks the thread name.
distinctUntilChanged() stops the same text from searching twice, and filter { it.isNotBlank() } is where an empty box would clear the results instead of searching.
The fake search blocks its thread with Thread.sleep and ignores cancellation on purpose, the worst case. The query "ab" takes 400 ms and "abc" takes 50 ms, so without protection the older answer finishes last. The control at the bottom is the same typing with no cancellation.
Terms in the program: a dispatcher decides which thread or threads a coroutine runs on (Dispatchers.IO is a shared pool meant for blocking input and output; the demo's main-thread dispatcher stands in for the UI thread). MutableStateFlow is a Flow that holds a current value you can assign, which is how the demo simulates a text box. SupervisorJob makes a scope where one failed child does not cancel its siblings. runBlocking makes main wait for the coroutines it starts. asCoroutineDispatcher() turns a plain Java executor into a dispatcher. The @file:OptIn(...) line acknowledges that debounce (@FlowPreview) and mapLatest (ExperimentalCoroutinesApi) are APIs the library marks as possibly changing.
What the typing script does, in milliseconds from the start (illustrative timings of the demo, not a measurement of a device):
| Time | Event | Pipeline result |
|---|---|---|
| 0 | user types a | debounce starts a 100 ms wait |
| 20 | user types ab | a is dropped because a newer value arrived within 100 ms; the wait restarts |
| 120 | 100 ms of silence | ab passes debounce; its search starts and would finish at 520 |
| 200 | user types abc | debounce wait starts |
| 300 | 100 ms of silence | abc reaches mapLatest, which cancels the ab lookup and starts abc; it finishes at about 350 |
Only abc is rendered. After that the script clears the box and types abc again: the empty text is removed by filter and the repeated abc by distinctUntilChanged, so neither reaches the network, which searches started confirms. In the control there is no mapLatest, so the ab search keeps running and delivers at about 520, after abc at about 350.
@file:OptIn(FlowPreview::class, ExperimentalCoroutinesApi::class)
import kotlinx.coroutines.*
import kotlinx.coroutines.flow.*
import java.util.concurrent.Executors
// Fake network call. It BLOCKS its thread (like a legacy blocking client) and ignores cancellation.
// "ab" is slow (400 ms), "abc" is fast (50 ms), so the older request would finish last.
val searched = java.util.Collections.synchronizedList(mutableListOf<String>()) // queries that reached the network call
fun blockingSearch(query: String): List<String> {
searched += query
Thread.sleep(if (query == "ab") 400 else 50)
return listOf("result for '$query'")
}
fun threadName() = Thread.currentThread().name
suspend fun typeLikeAUser(query: MutableStateFlow<String>, clearAndRetype: Boolean = false) {
query.value = "a"; delay(20)
query.value = "ab"; delay(180) // pause longer than the 100 ms debounce, so "ab" is sent
query.value = "abc"; delay(1000) // "abc" is sent 100 ms later; "ab" is still in flight
if (clearAndRetype) { // exercises filter and distinctUntilChanged
query.value = ""; delay(250) // an empty box passes debounce but must not search
query.value = "abc"; delay(300) // the same text again passes debounce but must not search twice
}
}
fun main() = runBlocking {
val mainThread = Executors.newSingleThreadExecutor { Thread(it, "main-thread").apply { isDaemon = true } }.asCoroutineDispatcher()
val io = Dispatchers.IO
// ---- correct: debounce, then mapLatest cancels the previous lookup ----
val shown = mutableListOf<String>()
val query = MutableStateFlow("")
val scope = CoroutineScope(mainThread + SupervisorJob())
val job = scope.launch {
query
.debounce(100) // wait for a pause in typing
.filter { it.isNotBlank() }
.distinctUntilChanged()
.mapLatest { q -> withContext(io) { blockingSearch(q) } } // new query cancels the old one
.collect { result -> // collect runs on mainThread
check(threadName() == "main-thread") { "UI update on ${threadName()}" }
shown += result.single()
println("render ${result.single()} on ${threadName()}")
}
}
typeLikeAUser(query, clearAndRetype = true)
delay(600)
job.cancel()
println("shown = $shown")
check(shown == listOf("result for 'abc'")) { "stale or missing result: $shown" }
println("searches started = $searched")
check(searched.toList() == listOf("ab", "abc")) { "debounce let through: $searched" } // "a" never reached the network
// ---- control: the same typing, but every keystroke launches an uncancelled request ----
val naiveShown = mutableListOf<String>()
val naiveScope = CoroutineScope(mainThread + SupervisorJob())
val naiveQuery = MutableStateFlow("")
val naiveJob = naiveScope.launch {
naiveQuery.debounce(100).filter { it.isNotBlank() }.collect { q ->
naiveScope.launch { naiveShown += withContext(io) { blockingSearch(q) }.single() } // no cancel of the older one
}
}
typeLikeAUser(naiveQuery)
delay(600)
naiveJob.cancel()
println("naive shown = $naiveShown (last wins: ${naiveShown.last()})")
check(naiveShown.last() == "result for 'ab'") // the stale answer arrives last and overwrites
mainThread.close()
}
Compiled with Kotlin 2.0.21 and run with kotlinx-coroutines 1.9.0 on JDK 21 (an eclipse-temurin:21-jdk container), it prints:
render result for 'abc' on main-thread
shown = [result for 'abc']
searches started = [ab, abc]
naive shown = [result for 'abc', result for 'ab'] (last wins: result for 'ab')
The pipeline shows only the abc result, and searches started = [ab, abc] shows that a never reached the network: that line is what proves the debounce. (mapLatest alone would also leave only abc on screen, because it cancels every older lookup, so the screen result cannot tell you whether the debounce works.) In the control the stale ab answer arrives last and would be the one left on screen. All three outcomes are enforced with check(...), so the program fails if the debounce, filter, distinctUntilChanged or mapLatest stops working or the control stops reproducing the bug; the main-thread executor uses daemon threads so that a failed check ends the process instead of leaving it hanging.
Swift: stored Task, debounce by sleeping, main-actor model
Swift tasks are cancelled by calling cancel() on the Task; the documented rule (SE-0304, a Swift Evolution proposal, one of the language's design documents) is that cancellation is cooperative (it only sets a flag, and nothing stops unless code checks the flag or calls something that does), and Task.sleep throws CancellationError when its task is cancelled mid-sleep. That gives the whole design:
searchTask?.cancel()on every keystroke.try await Task.sleep(...)as the debounce. If another keystroke cancels this task during the sleep, the sleep throws and the task returns without ever starting a request.- After the awaited search returns,
if Task.isCancelled { return }before touching state. This is the stale-overwrite guard for a search API that does not itself honour cancellation. - The model is
@MainActor(an attribute that ties a type's state to the main actor, the actor that runs on the main thread). Actor isolation means that state can only be touched from its actor's context, so aTask { }created inside the model inherits that isolation (SE-0304) and the finalshown += resultsruns on the main actor;MainActor.preconditionIsolated()asserts it.
import Foundation
// Fake network call. It ignores cancellation (like a client that does not check Task.isCancelled).
// "ab" takes 400 ms, "abc" takes 50 ms, so the older request would finish last.
func search(_ query: String) async -> [String] {
let ms: UInt64 = query == "ab" ? 400 : 50
_ = try? await Task.sleep(nanoseconds: ms * 1_000_000) // swallows CancellationError on purpose
return ["result for '\(query)'"]
}
@MainActor
final class SearchModel {
private(set) var shown: [String] = []
private(set) var searched: [String] = [] // queries that actually reached the network call
private var searchTask: Task<Void, Never>?
private let guardAgainstStale: Bool
init(guardAgainstStale: Bool) { self.guardAgainstStale = guardAgainstStale }
func queryChanged(_ query: String) {
if guardAgainstStale { searchTask?.cancel() } // 1. the newer keystroke cancels the older task
searchTask = Task { // inherits MainActor isolation
if guardAgainstStale {
do { try await Task.sleep(nanoseconds: 100_000_000) } // 2. debounce: throws if cancelled meanwhile
catch { return }
}
searched.append(query)
let results = await search(query) // suspends here; the main actor is free for other work
if guardAgainstStale && Task.isCancelled { return } // 3. an uncooperative call may still return: drop it
MainActor.preconditionIsolated() // UI state is only touched on the main actor
shown += results
}
}
}
func typeLikeAUser(_ model: SearchModel) async {
await model.queryChanged("a"); try? await Task.sleep(nanoseconds: 20_000_000)
await model.queryChanged("ab"); try? await Task.sleep(nanoseconds: 180_000_000)
await model.queryChanged("abc"); try? await Task.sleep(nanoseconds: 800_000_000)
}
let guarded = SearchModel(guardAgainstStale: true)
await typeLikeAUser(guarded)
let guardedShown = guarded.shown
print("guarded shown =", guardedShown)
precondition(guardedShown == ["result for 'abc'"])
let guardedSearched = guarded.searched
print("guarded searches started =", guardedSearched)
precondition(guardedSearched == ["ab", "abc"]) // debounce: "a" never reached the network
let naive = SearchModel(guardAgainstStale: false)
await typeLikeAUser(naive)
let naiveShown = naive.shown
print("naive shown =", naiveShown, "last =", naiveShown.last!)
precondition(naiveShown.last == "result for 'ab'")
Compiled with Swift 6.0.3 (swiftc -swift-version 5, the swift:6.0 container, aarch64 Linux) and run, it prints:
guarded shown = ["result for \'abc\'"]
guarded searches started = ["ab", "abc"]
naive shown = ["result for \'a\'", "result for \'abc\'", "result for \'ab\'"] last = result for 'ab'
(The backslashes come from how Swift prints an array of strings that contain apostrophes.) The guarded model ends with only the abc result, and guarded searches started = ["ab", "abc"] shows that the debounce kept a from ever reaching the network. The unguarded model, with no cancel, no debounce and no check, shows all three and ends on the stale ab. The order a, abc, ab is the order the fake searches finish: with no debounce, a starts at 0 ms and finishes at 50 ms, ab starts at 20 ms and finishes at 420 ms, and abc starts at 200 ms and finishes at 250 ms. In the guarded model a is cancelled during its debounce sleep and never searches. ab passes the sleep at 120 ms and starts its 400 ms search, abc cancels it at 200 ms, and when the uncooperative ab search returns at about 520 ms the Task.isCancelled check drops it. abc (sleep until about 300 ms, search until about 350 ms) is the only result shown. Both outcomes and the list of started searches are enforced with precondition; removing the debounce sleep, or the Task.isCancelled check, makes the program trap.
Trade-offs and pitfalls
- Cancellation is a request, not a kill. The demo APIs ignore it, which is why the result is discarded after the fact. In real code also use an HTTP call that actually cancels the underlying request when its coroutine or task is cancelled, so the connection and battery are released too.
Dispatchers.Mainversus background. A blocking call on the main thread freezes input; Android's guidance is that suspend functions should be main-safe, meaning they switch dispatchers themselves.- Debounce delays the first result. Showing a spinner or a cached result while waiting avoids a dead-feeling box;
mapLatestdoes not replace that user-experience work. - Order of operators matters. Debounce before the network call,
distinctUntilChangedbefore it too, andmapLatestlast;flatMapMergeor a plainlaunchper keystroke would bring the race back. - Test with virtual time. On the JVM, kotlinx-coroutines-test can replace real delays with controllable ones, and in Swift inject the sleep and the search function as the demo does with the fake
search.
Running the code
# Kotlin: kotlinc 2.0.21 unzipped to ./kotlinc and kotlinx-coroutines-core-jvm-1.9.0.jar (Maven Central) in the folder, listing saved as K.kt
docker run --rm -v "$PWD":/w -w /w eclipse-temurin:21-jdk sh -c \
'kotlinc/bin/kotlinc K.kt -cp kotlinx-coroutines-core-jvm-1.9.0.jar -d out && java -cp out:kotlinc/lib/kotlin-stdlib.jar:kotlinx-coroutines-core-jvm-1.9.0.jar KKt'
# Swift: save the listing as main.swift (top-level await needs that file name)
docker run --rm -v "$PWD":/w -w /w swift:6.0 sh -c 'swiftc -swift-version 5 main.swift -o app && ./app'
A mobile app has data sources that produce events faster than the UI can use them. Design how reactive streams (RxSwift/RxJava or Kotlin Flow) move that data: which work runs on background threads, how results reach the main thread, what you do when the producer outruns the consumer, and how errors and long-running operations are handled.
Sample Answer
Direct answer
Start here: the design answers four questions in order. Which thread does the work? Which thread draws the result? What happens when values arrive faster than the screen can use them? What happens when something fails or runs for a long time?
Run the source and all heavy work on background threads, hop to the main thread exactly once at the end (a "hop" is the switch from a background thread to the main thread, where the UI state is written), and decide per stream what happens when the producer is faster than the consumer: UI state keeps only the latest value (Kotlin's conflate and RxJava's onBackpressureLatest both mean "skip the values in between and keep the newest"), commands and user actions go through a bounded buffer (a waiting line with a fixed maximum length) that makes the producer wait, and telemetry-style firehoses are sampled (one value per time slice) or dropped at the source. Errors are handled inside the stream so one bad item does not end it, and every long-running subscription is tied to a lifecycle owner (the screen or view model whose lifetime says how long the subscription should live) that cancels it.
Vocabulary used below
- Reactive stream: a sequence of values over time that you transform with operators (
map,filter,debounce) instead of callbacks. RxJava and RxSwift (ReactiveX libraries) call it an Observable or Flowable; Kotlin calls it a Flow. - Backpressure: the feedback a slow consumer gives a fast producer ("wait" or "skip"). Without it the values pile up in memory or an overflow error occurs.
- Cold vs hot: a cold source does its work per subscriber and only when subscribed (a network request, a database query,
Flowable.range); a hot source emits whether or not anyone listens (taps, sensor callbacks, a socket,PublishProcessor). Each new collector of a cold Flow starts a new execution of it, which is why two screens collecting the same cold Flow fire two requests unless you share it (shareIn,stateIn; RxSwift'sshare(replay: 1)). - Scheduler / dispatcher: the object that decides which thread runs a piece of the chain.
- Observable vs Flowable (RxJava): both are streams, but only
Flowablesupports backpressure; anObservablejust pushes and cannot be told to slow down. - Structured concurrency: Kotlin's rule that every coroutine belongs to a scope, and cancelling the scope cancels everything started in it, so nothing outlives its screen.
- Sealed UI state: a Kotlin type with a fixed set of cases, for example Loading, Content(items) and Error(message), so the screen has to handle each one and a failure becomes a value instead of an exception.
- Main-safe: a function that is safe to call from the main thread because it moves its own blocking work to another thread internally.
- Sample / throttle: emit at most one value per time window (for example the latest value every 100 ms) and ignore the rest.
MissingBackpressureException: the RxJava error raised when aFlowablesource pushes faster than a bounded buffer can hold and no overflow strategy was chosen.
Which work runs where
| Concern | RxJava / RxSwift | Kotlin Flow |
|---|---|---|
| Where the source starts and runs | subscribeOn(Schedulers.io()) / subscribe(on:): "where the Observable starts, no matter where it appears in the chain" (ReactiveX) | flowOn(Dispatchers.IO) or Dispatchers.Default: changes the context of everything upstream of it only |
| Where results are delivered | observeOn(AndroidSchedulers.mainThread()) / observe(on: MainScheduler.instance): affects the chain below where it appears | The collector's own context, for example viewModelScope (documented to use Dispatchers.Main) |
| UI-safe binding | RxSwift Driver: observes on the main scheduler and cannot error out | StateFlow collected under repeatOnLifecycle(STARTED) |
| Cleanup | dispose the subscription in the owner's teardown; RxSwift DisposeBag disposes everything when it is deallocated | cancel the collecting coroutine (structured concurrency does it with the scope) |
Rules that follow (the Kotlin column and the RxJava column are two spellings of the same design, so follow the one for the library your project already uses): parse, decode, sort and database work stay upstream of the hop; only the final assignment to UI state happens on the main thread; and on Android a suspend function that blocks should switch dispatchers itself (Android's guidance: suspend functions should be main-safe).
When the producer outruns the consumer
Take a sensor or socket delivering 1000 events per second to a screen that draws 60 frames per second: one frame lasts about 1000 / 60 = 16.7 ms, so at most 60 of those 1000 events per second can ever be seen. Buffering all 1000 per second only creates latency and memory growth.
A small trace makes the options concrete. Illustrative model: event number k arrives at k ms, the consumer needs 20 ms per item, and an item that arrives at the instant the consumer becomes free counts as already waiting. Events 0 to 44 then play out like this:
| Strategy | Items the consumer receives (event number at time in ms) | Items skipped |
|---|---|---|
| Default (producer suspends) | all 45 in order, but the producer is held back to one event per 20 ms | none |
conflate (keep newest) | 0 at 0, 20 at 20, 40 at 40, 44 at 60 | 41 of 45 |
buffer(3, DROP_OLDEST) (keep the newest 3 waiting) | 0 at 0, 18 at 20, 38 at 40, 42 at 60, 43 at 80, 44 at 100 | 39 of 45 |
At 20 ms the consumer finishes event 0. With conflate it picks up event 20, the newest, and skips 1 to 19. With the 3-slot buffer it picks up the oldest of the three it kept (18, 19, 20) and the other two wait. Over a full second of 1000 events the same model delivers 51 items with conflate (0, 20, 40, and so on up to 980, then 999) and 53 with the 3-slot buffer; a consumer that does 50 items per second cannot do better than about 50, whatever the strategy. Both strategies end on the final event, which is why they suit "current state" data and not "every command" data.
Choose by the meaning of the data:
| Data | Strategy | RxJava Flowable | Kotlin Flow |
|---|---|---|---|
| Current state (position, price, progress) | Keep only the newest | onBackpressureLatest() | conflate(), or StateFlow |
| Must not lose items, bursts are short | Bounded buffer, producer waits when full | onBackpressureBuffer(capacity) with the error strategy, or request a few items at a time | default suspension, or buffer(n) (default overflow is SUSPEND) |
| Old items are worthless once new ones exist | Drop | onBackpressureDrop() | buffer(n, BufferOverflow.DROP_LATEST) or DROP_OLDEST |
| Work per item should be abandoned when a newer item arrives | Cancel in-flight work | switchMap | collectLatest, mapLatest |
| Telemetry | Sample at a fixed rate | sample/throttle | sample (not debounce, which never emits while values keep arriving) |
Two library facts that decide RxJava code. Observable has no backpressure at all and the RxJava 2 notes recommend it for streams of at most about 1000 elements and for GUI events; Flowable has it, and an overflowing bounded buffer terminates the stream with MissingBackpressureException. The usual cause is a source that cannot be slowed down (interval, a PublishProcessor, a hand-written create). An unbounded onBackpressureBuffer() hides the problem by growing without limit, so it is the wrong default on a memory-limited phone. Swift's AsyncStream offers the same choice as a buffering policy: .unbounded (the default), .bufferingOldest(n) or .bufferingNewest(n).
Errors and long-running work
- An uncaught error ends the stream. In Kotlin,
catchhandles only upstream exceptions and then the flow is over. To keep a stream alive across a bad item, convert the failure to a value inside the step that can fail (aResultor a sealed UI state) and let the stream continue. The harness shows both outcomes. - Retry with a policy. Retry transient failures with backoff (
retryWhenin Flow,retryWhenorretryin Rx), not in a tight loop, and never retry what the user cancelled. - Cancel obsolete work.
mapLatest,collectLatestandswitchMapcancel the previous in-flight job when a new item arrives;withTimeout(or Rxtimeout) bounds a single slow call. - Tie subscriptions to a lifecycle. Android's documentation warns that collecting a flow with a bare
launchkeeps processing while the UI is not visible and can crash it; userepeatOnLifecycle(Lifecycle.State.STARTED). For never-ending sequences such as button taps the RxSwift docs state resources are held permanently unless disposed, which is the unsubscribe leak.
Code and run
The Kotlin half uses a fast producer (100 events, one per millisecond, on Dispatchers.Default) and a collector that takes 20 ms per item on a thread named main-thread (a single-threaded executor standing in for the Android main thread). The RxJava half feeds an interval source into a slow observeOn consumer with a 4-item buffer.
@file:OptIn(ExperimentalCoroutinesApi::class)
import io.reactivex.rxjava3.core.Flowable
import io.reactivex.rxjava3.schedulers.Schedulers
import kotlinx.coroutines.*
import kotlinx.coroutines.channels.BufferOverflow
import kotlinx.coroutines.flow.*
import java.util.concurrent.Executors
import java.util.concurrent.TimeUnit
import java.util.concurrent.atomic.AtomicInteger
// A source that outruns its consumer: 100 events, one every millisecond, produced on a background thread.
fun events(): Flow<Int> = flow {
for (i in 0 until 100) {
check(Thread.currentThread().name != "main-thread") // the producer must never run on the UI thread
emit(i); delay(1)
}
}.flowOn(Dispatchers.Default)
fun main() = runBlocking {
val ui = Executors.newSingleThreadExecutor { Thread(it, "main-thread").apply { isDaemon = true } }.asCoroutineDispatcher()
suspend fun consume(label: String, shape: Flow<Int>.() -> Flow<Int>) {
val seen = mutableListOf<Int>()
withContext(ui) {
events().shape().collect { v ->
check(Thread.currentThread().name == "main-thread") // delivery on the UI thread
delay(20) // slow consumer: rendering an item takes 20 ms
seen += v
}
}
val rising = seen.zipWithNext().all { (a, b) -> a < b }
println("%-24s delivered all 100: %-5s last = %d in order: %s".format(label, seen.size == 100, seen.last(), rising))
check(seen.last() == 99 && rising)
if (label == "default (suspend)") check(seen.size == 100) else check(seen.size < 100)
}
consume("default (suspend)") { this } // producer slows to the consumer's pace
consume("buffer(10, DROP_OLDEST)") { buffer(10, BufferOverflow.DROP_OLDEST) } // bounded, keeps the freshest 10
consume("conflate") { conflate() } // keeps only the newest
// collectLatest: the block for an old item is cancelled when a newer item arrives
var started = 0; val finished = mutableListOf<Int>()
withContext(ui) {
events().collectLatest { v -> started++; delay(20); finished += v }
}
println("collectLatest started 100: ${started == 100}, finished all: ${finished.size == 100}, last finished = ${finished.last()}")
check(finished.size < 100 && finished.last() == 99)
// errors: a throw in the middle of the stream ends it, unless the risky step handles its own failure
fun risky(v: Int): Int = if (v == 2) error("bad item") else v
val ended = mutableListOf<Int>()
flowOf(0, 1, 2, 3, 4).map { risky(it) }.catch { emit(-1) }.collect { ended += it }
val survived = mutableListOf<Int>()
flowOf(0, 1, 2, 3, 4).map { runCatching { risky(it) }.getOrDefault(-1) }.collect { survived += it }
println("catch after map: $ended | per-item runCatching: $survived")
check(ended == listOf(0, 1, -1) && survived == listOf(0, 1, -1, 3, 4))
// RxJava 3: Flowable.interval cannot be slowed down, so a slow observeOn consumer overflows its buffer
val rxThread = Executors.newSingleThreadExecutor { Thread(it, "main-thread").apply { isDaemon = true } }
val rxUi = Schedulers.from(rxThread)
fun runRx(label: String, expectOverflow: Boolean, shape: (Flowable<Long>) -> Flowable<Long>) {
val last = AtomicInteger(-1); val error = arrayOfNulls<Throwable>(1)
val d = shape(Flowable.interval(1, TimeUnit.MILLISECONDS).take(100))
.observeOn(rxUi, false, 4) // bufferSize 4 makes the overflow easy to see
.subscribe({ Thread.sleep(20); last.set(it.toInt()) }, { error[0] = it })
Thread.sleep(3000); d.dispose()
println("%-26s error = %s".format(label, error[0]?.javaClass?.simpleName))
if (error[0] == null) println("%-26s last item delivered = %d".format("", last.get()))
if (expectOverflow) check(error[0] is io.reactivex.rxjava3.exceptions.MissingBackpressureException)
else { check(error[0] == null); check(last.get() == 99) }
}
runRx("interval, no strategy", expectOverflow = true) { it }
runRx("onBackpressureLatest", expectOverflow = false) { it.onBackpressureLatest() }
rxThread.shutdown(); Schedulers.shutdown(); ui.close()
}
Compiled with Kotlin 2.0.21 and run with kotlinx-coroutines 1.9.0 and RxJava 3.1.9 on JDK 21 (an eclipse-temurin:21-jdk container), it prints:
default (suspend) delivered all 100: true last = 99 in order: true
buffer(10, DROP_OLDEST) delivered all 100: false last = 99 in order: true
conflate delivered all 100: false last = 99 in order: true
collectLatest started 100: true, finished all: false, last finished = 99
catch after map: [0, 1, -1] | per-item runCatching: [0, 1, -1, 3, 4]
interval, no strategy error = MissingBackpressureException
onBackpressureLatest error = null
last item delivered = 99
How to read the harness. withContext(ui) { ... } runs the collection on a single thread named main-thread, and check(Thread.currentThread().name == "main-thread") fails the program if an item ever arrives anywhere else. The mirror-image check(... != "main-thread") inside the producer fails the program if the source ever runs on the UI thread, which is what happens if .flowOn(Dispatchers.Default) is deleted. delay(20) stands in for 20 ms of rendering. seen.zipWithNext() pairs each item with the next one, so .all { (a, b) -> a < b } verifies the items arrived in increasing order. The shape parameter is the one operator being tried (nothing, buffer, or conflate), so each consume call differs by exactly one line. runCatching { risky(it) }.getOrDefault(-1) turns an exception for one item into the value -1 so the stream keeps going.
What it shows: with the default the producer is slowed to the consumer's pace and nothing is lost; buffer(10, DROP_OLDEST) and conflate lose intermediate items but always deliver the final value (99) in order; collectLatest starts work for every item but abandons the unfinished ones; a failure at item 2 inside map ends the stream when only catch follows, while handling the failure per item keeps items 3 and 4 flowing; and the same interval source fails with MissingBackpressureException until onBackpressureLatest() is added, after which the last item arrives. The exact number of dropped items depends on thread timing and is deliberately not asserted, only that some were dropped and that the last one survived. The program checks each of these claims with check(...), so it throws if one stops being true. The RxJava half states its expectation per case (interval, no strategy must fail with MissingBackpressureException; onBackpressureLatest must finish with no error and the last item 99), so removing onBackpressureLatest() makes the run exit with Check failed.
Trade-offs and pitfalls
- Dropping is a product decision. Conflating a stream of "item added to cart" commands would lose purchases; conflating a stream of scroll positions loses nothing that matters.
debounceis not backpressure. It reduces the rate by waiting for a quiet period, so under a continuous stream it never emits.- Thread hops cost. One
flowOnorobserveOnper stage is cheap; a hop per operator adds latency and context switches. - Share expensive cold sources between collectors, and stop the shared work after the last collector leaves, so a rotated screen does not restart a request.
Running the code
# Maven Central jars: kotlinx-coroutines-core-jvm 1.9.0, rxjava 3.1.9, reactive-streams 1.0.4; Kotlin compiler 2.0.21 unzipped to ./kotlinc
CP=kotlinx-coroutines-core-jvm-1.9.0.jar:rxjava-3.1.9.jar:reactive-streams-1.0.4.jar
docker run --rm -v "$PWD":/w -w /w eclipse-temurin:21-jdk sh -c \
"kotlinc/bin/kotlinc Main.kt -cp $CP -d out && java -cp out:kotlinc/lib/kotlin-stdlib.jar:$CP MainKt"
That is every published Asynchronous and Event-Driven Programming question for Mobile Developer so far. Browse the other topics in this category, or practice this one interactively.