Where work runs
A reactive scheduler controls which thread executes each part of a pipeline. By default everything runs on the subscribing thread, but you often want IO work and CPU work on different pools.
Two distinct operators
- Subscribe on sets the thread where the source starts emitting and affects the whole chain upstream of it.
- Observe on switches the thread for everything downstream of that point.
The asymmetry trips people up. Subscribe on placement does not matter much because only the first one takes effect, while observe on can be inserted multiple times to hop threads at each stage.
Common scheduler kinds
- An IO scheduler uses an elastic pool sized for blocking calls.
- A computation scheduler is bounded near the core count for CPU work.
- A single scheduler serializes work onto one thread for ordering.
A caution
Hopping threads has a cost: each handoff is a context switch and a queue insertion. Hop only at real boundaries, such as moving off an IO thread before heavy parsing.
Key idea
Subscribe on picks the source thread once, observe on switches downstream threads repeatedly, and each hop costs a context switch.