In a context carried with a subscription, where must a value be written for a given stage to read it?
answer
- direction matters
- built down, subscribed up
- a write serves what it passes
- stages between write and source
- put the write at the end
basics
~20 sA subscription context is assembled while the subscription travels from the subscriber back toward the source, so a write serves the stages it passes on that journey: those between it and the source, not those after it in the written chain.
solid answer
~40 sA chain is written downward from the source, but nothing runs until something subscribes, and subscription travels the other way — the subscriber asks the stage above it, which asks the stage above that, up to the source. A context carried with the subscription is assembled on that upward journey, so an entry added by one operator is present for everything the subscription reaches afterwards: the stages between that operator and the source. Stages nearer the subscriber were already visited with the smaller context and never see it. The practical rule is therefore to write at the end of the chain, or at the boundary that opens the subscription, so the entire chain can read it. Where the same key is written twice, a stage sees the nearest write on its subscriber side.
code
pseudocode · 5 linesrows(query)
.map(function(row) return scopeTo(row, contextGet("tenant"))) // reads it
.map(function(row) return writeAudit(row, contextGet("tenant"))) // reads it
.withContext("tenant", request.tenant) // written last: subscription passes it first
.subscribe()go deeper
Remember that the context travels with the subscription and not with the values, so where the write sits in the chain decides who can read it.
Explain the assembly order: the subscription is established from the subscriber toward the source, so a write serves the stages it passes on the way up.
Diagnose the real failure — a read that finds nothing because the write sits nearer the source than the reader — and move the write to the boundary that opens the subscription.
Decide whether stages may contribute context at all, or whether one boundary writes it once, and weigh what that rule costs teams composing chains out of shared fragments.
## Two directions inside one pipeline A chain is **written** from the source downward: each operator wraps the one declared before it, and the last thing in the written chain is the subscriber. At that point nothing runs. Work begins when something subscribes, and subscription travels the **other way** — the subscriber asks the operator above it for a subscription, that operator asks the one above it, and so on until the request reaches the source. Only then do values flow back down. A context carried with the subscription is assembled during that upward journey. That single fact answers the whole question: placement in the written chain decides which stages the write reaches, because the write is applied at the moment the subscription passes it. ## What a write contributes, and to whom When the subscription passes an operator that adds an entry, that entry joins the context handed to everything the subscription reaches **afterwards** — everything nearer the **source**. Stages nearer the subscriber were already visited, with the smaller context, and never see it. | the write is placed | stages that can read it | |---|---| | after every stage, at the end of the chain | all of them | | between two stages | the stages between the write and the source | | immediately after the source, at the top | effectively only the source's own work | - A write at the very end of the chain is the safe default: it serves the whole chain. - A write in the middle is a deliberate scoping decision, not a convenience. - A write at the top usually indicates a misunderstanding, because almost nothing reads it. - With two writes of the same key, a stage sees the **nearest** one on its subscriber side, because that is the last write applied before the subscription reached it. - A stage that reads a key nobody wrote gets an absence, not a failure — which is its own defect class. ## The rule that follows Write the context where the subscription is opened: at the boundary that owns the request. That boundary is the one place that genuinely knows the trace identity, the tenant and the caller, and placing the write there — or as the final element of the chain it assembles — makes the entries visible to every stage the chain will ever contain. A library fragment spliced into the middle of someone else's chain should read keys, not write them, precisely because its position decides its audience and it cannot know that position. ## It is a context, not a scratchpad The context is **immutable**: an operator does not mutate it, it derives a new one. Two consequences follow, and both are asked about. 1. A stage cannot use the context to hand a computed value forward to a later stage. The later stage's view was fixed before this stage ever ran, and in the written chain a later stage is *earlier* on the subscription's journey. Data that has to flow forward belongs in the elements — carried alongside the value that produced it. 2. Two subscriptions that share entries cannot interfere with each other. Each derivation produces a distinct value, so a concurrently running subscription on the same worker sees only its own. ## Where ecosystems differ This is the model in which any operator may contribute to the context mid-chain, and it is the model worth being able to reason about, because placement is then a real decision. Other designs fix the context once, where the subscription is opened, and give operators no way to add entries at all; there, placement never arises but the boundary write is mandatory. Both carry the values with the subscription rather than with the worker, which is the property that survives a hop. ## Diagnosing a read that finds nothing When a stage reads a key and finds an absence, work through it in order: 1. Confirm a write for that key exists in the chain at all, rather than in an enclosing scope that never became part of this subscription. 2. Compare positions: is the write nearer the subscriber than the reader? If not, the reader was visited before the entry existed. 3. Check the key itself — an ad-hoc string key spelled differently in two teams' code fails exactly like a missing write. 4. Check what the read is actually reading. A stage reading worker-bound storage rather than the subscription context fails for an entirely different reason and needs a different fix. The interviewer is listening for one sentence: the chain is built downward and subscribed upward, so a context write serves what the subscription reaches after it.
- Can an early stage put a computed value into the context for a later stage to read?No. Each stage's view of the context is fixed when the subscription passes it, and a later stage in the written chain was passed earlier on that journey. A value computed per element must travel in the element itself, or as a pair carried alongside it.
- Two writes in one chain use the same key — which one does a stage between them see?The one below it, nearer the subscriber, because that write was applied before the subscription reached the stage; the write above it has not happened yet from that stage's point of view. A stage above both writes sees the one closer to itself, which was applied last.
- Why should a reusable chain fragment read context keys rather than write them?Its audience is decided by where it is spliced in, and it cannot know that. A fragment that writes a key serves only whatever sits above it in a chain it did not assemble. Writing belongs to the boundary that opens the subscription and knows the whole shape.
saying these in an interview costs you the question
- Thinks a value written at the top of the chain is visible to every stage below it.
- Treats the context as a mutable per-request scratchpad stages hand forward.
- Expects an entry added by one stage to reach the next stage's read.
- Assumes placement never matters because the context belongs to the whole pipeline.
- Believes writing the same key twice merges both values for every stage.