skip to content

In a stream chain, why does the switch that relocates the producing work apply no matter where it is placed?

level: middleimportance: must knowfreq 62%

answer

  1. two signals, two directions
  2. written order is not execution order
  3. subscription walks up to the source
  4. values walk down to the subscriber
  5. caught on the upward pass, wherever written

basics

~20 s

Subscription travels upstream from the subscriber to the source, so that switch is reached wherever it sits and hands the rest of the upward walk to its worker. The source therefore starts on that worker.

solid answer

~50 s

A finished chain carries two signals in opposite directions. The subscription travels **upward**, from the subscriber through every stage to the source; values travel **downward**, from the source to the subscriber. The switch that relocates producing work acts on the upward pass: when the subscription reaches it, it hands the remaining attachment to its own worker, so every stage above it and the source itself are subscribed there. Because the upward pass crosses every stage, that switch is reached whether it was written next to the source or last before subscribing — its position cannot change the outcome. The other switch acts on the downward pass, queueing each arriving value for its worker, so it moves only the stages written after it. In the absence of any later boundary, the producing-side switch also ends up deciding where the downstream stages run, because a value keeps travelling on the worker it arrived on.

code

pseudocode · 11 lines
pseudocode
chain =
    source: read_large_file(path)        // the producing work
    then:   decode(bytes)
    then:   to_progress_percent(decoded)
    run_producing_on(background_worker)  // written LAST of all

// subscribe is called from the window worker
subscribe(chain, on_value = paint_progress_bar)

// subscription walks up: subscriber -> switch -> percent -> decode -> source
// the switch is crossed on the way up, so read_large_file runs on background_worker

go deeper

for a junior

Recall that building a chain runs nothing, and that a subscriber attaching at the bottom is what starts it. That single fact already explains why where you write a switch is not where it acts.

for a middle

Explain the two passes by name and direction, then derive each switch's reach from the pass it acts on. Be able to walk a written chain and say which worker each stage ends up on.

for a senior

Show the judgment: read a chain in review, state the worker per segment, and spot the switch that was placed for the pass the author did not mean and so bought a hop for nothing.

for a principal

Frame the trade-off in a codebase: every boundary costs a handoff and a guarantee nobody wrote down. Decide where switching belongs — at the edges that must be on a given worker, not sprinkled per stage.

## Assembly is not execution Writing a chain of stages builds a **description** of work, not the work itself. Each stage records what it will do and holds a reference to the stage above it. Nothing in the chain you assembled runs until a subscriber attaches at the bottom. Once one does, two different signals travel that structure, and they travel in **opposite directions**. - **Subscription travels upstream.** The attach call passes from the subscriber to the stage above it, then to the stage above that, until it reaches the source. Only then does the source begin producing for that subscription. - **Values travel downstream.** Each produced value is handed to the stage below the source, then to the next one, until it reaches the subscriber. Both switches are ordinary stages sitting in that structure. What separates them is which of the two passes each one interferes with. ## The switch that relocates the producing work This one acts on the **upward** pass. When the subscription reaches it, it does not attach to its own upstream on the spot; it hands that remaining attachment to its worker and returns. Everything still above it — every remaining stage, and finally the source — is therefore subscribed from that worker, so the source's own work runs there. Because the upward pass begins at the subscriber and crosses **every** stage on its way to the source, this switch is reached wherever it was written. Put it immediately after the source, or write it as the very last thing before subscribing: the result is identical. That is what position-independence means here, and it is not magic — it follows from the direction the subscription travels. A second consequence is easy to miss. A value keeps travelling on whatever worker it arrived on until some stage moves it. The source emits on the worker it was subscribed from, so **in the absence of any later boundary** this single switch also decides where each downstream stage runs. ## The switch that relocates what follows This one acts on the **downward** pass. When a value arrives, it queues that value for its worker and returns; the worker then hands it to the next stage. Every stage written after it runs on that worker, and nothing written before it changes at all — those stages already ran, elsewhere, before the value reached the boundary. Its position is its entire meaning: it is a boundary drawn across the chain, and a boundary can only look forward. | | relocates the producing work | relocates what follows | |---|---|---| | Pass it acts on | the upward subscription | the downward value | | Reach | the source, and every stage until some later boundary | only the stages written after it | | Does position matter | no; the upward pass reaches it wherever it sits | yes; the switch *is* the boundary | | Effect of a second one | only the one nearest the source decides where the source runs | each one opens another segment | | Typical use | move a long read off the worker that assembled the chain | bring values back to the worker that must paint them | ## Reading a chain the way an interviewer wants 1. Find the source and ask which worker the subscription reaches it on: the producing-side switch nearest the source, or else whichever worker attached the subscriber. 2. Walk downward from the source carrying a single note, *the current worker*. Change that note only when you step over a switch that relocates what follows. 3. Compare the worker in the final segment against what the receiving code requires. In a window application, painting usually has exactly one legal worker. ## What each hop costs - A handoff is not free: the value is queued, another worker is woken, and latency is added at every boundary. - Ordering along one sequence survives a hop, but many tiny stages split across many segments measurably lose to the same stages inside one segment. - A switch acting on the pass you did not mean buys the hop and moves nothing you cared about. The signature is a chain that still stalls the worker that started it while a snapshot shows an extra worker doing trivial work. ## What neither switch does Neither switch makes one sequence run in parallel: values still travel one at a time, and relocating a stage moves it rather than duplicating it. Neither is retroactive either — where the producing work runs is settled while the subscription is being set up, so a later placement takes effect only on the next subscription. ## Why people get it backwards The written chain reads top to bottom, so it is natural to assume both switches behave alike and affect only what follows. That intuition is correct for exactly one of the two. The other is reached during a pass that runs bottom to top and is invisible in the source text. Say the direction out loud — subscription upward, values downward — and both rules fall out of it instead of having to be memorised.

  • If a chain contains only a switch that relocates producing work, where do the later stages run?
    On that same worker. The source emits on the worker its subscription reached it from, and a value keeps travelling on the worker it arrived on until some stage queues it elsewhere. With no later boundary, one producing-side switch sets the worker for the whole chain.
  • Where do stages written before a switch that relocates what follows run?
    Wherever the values reached them from — that switch has no reach backwards. It only queues arriving values for its own worker, and a stage above it has already produced its result before the value ever reaches the boundary.
  • Why does assembly order mislead people about this?
    Because the text reads top to bottom while one of the two passes runs bottom to top. Assembling the chain runs none of the stages; it only records them. Both rules become obvious once you state which pass each switch interferes with.

Ordering goods travels back up the supply chain to whoever makes them, and where that maker sits decides where the making happens. A transfer depot halfway along changes only who carries the parcel onward from the depot.

saying these in an interview costs you the question

  • Says both switches affect only the stages written after them
  • Believes a switch that relocates what follows can also move the source's own work
  • Thinks assembling the chain already starts work somewhere
  • Insists the producing-side switch must be written first to have any effect
  • Claims subscription and values travel the chain in the same direction
  • Assumes adding a switch makes the sequence process values in parallel