skip to content

If one chain carries two switches that each relocate the producing work, which one decides where the source runs?

level: middleimportance: nice to knowfreq 30%

answer

  1. trace the pass, skip the rule
  2. subscription starts at the subscriber
  3. nearest the source is crossed last
  4. surplus ones still cost a hop
  5. settled once per subscription

basics

~20 s

The one the subscription reaches last, which is the one nearest the source. The others only relocate the remaining part of the upward walk; they never change where the source produces or where values are delivered.

solid answer

~50 s

Trace the subscription. It starts at the subscriber and walks upward, so the switch written nearest the subscriber is met **first**: it hands the rest of the walk to its worker. That walk continues upward and meets the next such switch, which hands the remainder to *its* worker — and so on, until the source is finally attached from the worker of the switch nearest it. That last one is the one that matters, because the source produces on the worker its subscription arrived from. The surplus switches are not inert: each one moves part of the attaching work and costs a hop while the subscription is being set up. What they do not do is change where values are produced or delivered. And all of this is settled once, per subscription: a new subscription re-runs the walk, but an existing one cannot be re-pointed.

code

pseudocode · 11 lines
pseudocode
chain =
    source: read_large_file(path)
    run_producing_on(worker_A)   // nearer the source
    then:   decode(bytes)
    run_producing_on(worker_B)  // nearer the subscriber
subscribe(chain, on_value = paint)

// upward walk: subscriber -> B (hands the rest to worker_B)
//                        -> decode -> A (hands the rest to worker_A)
//                        -> source, attached on worker_A
// so the read runs on worker_A; B only moved the rest of the walk

go deeper

for a junior

Remember the direction before the rule: the subscription starts at the subscriber and walks up to the source, so the switch closest to the source is the last one it crosses.

for a middle

Trace a two-switch chain out loud and state exactly what each switch moved, including the fact that the surplus one still relocates part of the attaching walk and costs a hop.

for a senior

Recognise the composition case in review — a published chain that already pins its worker makes a caller's switch look ignored — and stop the reflex to add a third.

for a principal

Decide whether chains crossing a team boundary may pin a worker at all, since doing so silently overrides every caller's later attempt and hides the decision from the call site.

## Trace the pass, do not memorise a rule This question is only hard if you try to recall a rule. Walk the subscription instead, because everything follows from its direction. A chain, written top to bottom, is: source, then stages, with the subscriber at the bottom. The subscription starts at the **bottom** and walks **up**. So: 1. The producing-side switch written nearest the subscriber is encountered first. It hands the remaining upward walk to its worker and returns. 2. On that worker, the walk continues upward and meets the next such switch, which hands what is still left to its own worker. 3. Whichever such switch sits nearest the source is therefore the last one crossed, and the source is attached from its worker. 4. The source produces on the worker its subscription arrived from — so the nearest-to-source switch is the one that decides where the producing work runs. ## What the surplus switches actually do Calling the extra ones "ignored" is the usual shorthand and it is slightly too strong. Each of them really does relocate something: the part of the attaching walk that is still ahead of it. That matters in two narrow ways and no others. - **It costs.** Every one of them is a handoff during subscription setup — small, but paid on each subscription, which is visible if subscriptions are frequent and short-lived. - **It can change where attaching side effects happen.** If a stage does work as it is being attached rather than as values flow, that work happens on the worker the walk is on at that moment. What they do **not** do: change where the source produces, and change where values are delivered. Delivery is governed by the last forward-only boundary, which is a different pass entirely. | | switch nearest the source | the surplus ones further down | |---|---|---| | Decides where the source produces | yes | no | | Relocates part of the attaching walk | yes | yes | | Costs a handoff at subscription time | yes | yes | | Affects where values are delivered | no | no | ## Why two ever appear in one chain Rarely on purpose. The common origin is composition: a chain published by one layer already relocates its own producing work, and a caller wraps it and relocates again. The caller's switch is written further from the source — it is on the outside — so the inner one wins and the caller's choice quietly does not apply to the source. That is worth knowing before writing the switch, because the outcome looks like the switch was ignored, and the next move is often to add a third. ## Settled once, per subscription Where producing work runs is decided while the subscription is being established, and then it is fixed for that subscription. Two consequences: - You cannot re-point a running subscription onto a different worker by changing anything downstream. The way to run the producing work elsewhere is to subscribe again with a different arrangement. - Each new subscription re-runs the walk independently. Two subscribers to the same assembled chain each get their own decision, and nothing one of them did constrains the other. ## How to answer this in an interview Say the direction first, then derive it — that is the answer that shows understanding rather than recall: 1. "The subscription travels upward from the subscriber." 2. "So the one nearest the source is crossed last, and the source is attached from its worker." 3. "The others move only the remaining attaching work, and they each cost a hop." If you find yourself reasoning from the written order instead, you will get it backwards: top to bottom suggests the first one written should win, which is right by accident only when there is exactly one.

  • Is it fair to say the surplus switches are simply ignored?
    Not quite. Each one relocates the remaining attaching walk and costs a handoff while the subscription is set up, and any work a stage does while being attached happens on that worker. What they never change is where the source produces or where values are delivered.
  • A caller wraps a published chain and relocates its producing work, but nothing moves. Why?
    Because the published chain already relocated its own, and that inner switch is nearer the source, so the subscription crosses it last. The caller's switch is on the outside and only moves the walk up to it.
  • Can a running subscription be moved onto a different worker later?
    No. The producing-side choice is made while the subscription is being established and stays fixed for its lifetime. Running the producing work elsewhere means subscribing again with a different arrangement.

saying these in an interview costs you the question

  • Says the switch written first in the text always wins
  • Believes the surplus switches are literally free
  • Thinks a producing-side switch also decides where values are delivered
  • Expects a running subscription to follow a switch added later
  • Assumes two such switches split the source's work between two workers
  • Reasons from written order instead of the subscription's direction