Skip to content

Inputs can arrive twice

A worker's input topics are at least once since WorkManager 0.38.0: the Runtime saves a cursor per worker and topic at most every 250 ms and, after a move, catches the worker up from it. After a hard crash the events the worker finished in that last window are delivered again, to its next activation; its consumers, still running, then see what it publishes for them a second time (Inputs across a move).

  • What the devkit does for you. BehaviorWorkerBase (and the domain bases on it) drops an input whose source and id the instance already took (SeenEvents: the last 100 000 within 10 minutes, in memory) and derives each output's id from the worker, the input and the output (DerivedEventId), so a republished event carries its first id and the consumer drops it (Inputs can arrive twice).
  • A custom worker (WorkerBase, WorkerCore, or Python): call SeenEvents.TryAdd(input) first and return nothing for a duplicate; set output ids with DerivedEventId.For(Context.WorkerId, input, type, topic, subject, n); and make every fold idempotent by the data's own identity where it has one: a trade id, a candle's window start, an update-id bracket, a fill id. A side effect outside the event stream (an HTTP call, an exchange order, a database write) needs a key derived from the input and a lookup before it acts again: LiveExecutor places each order under a client order id derived from the order id and looks it up first.
  • Choose input_replay OFF (the CreateWorkerRequest field input_replay = INPUT_REPLAY_OFF, a platform setting, never a config key) when the worker resynchronizes itself after a gap, e.g. it numbers its inputs and rebuilds from a snapshot, as the order-book workers do: the inputs then start at the tail after a move (nothing twice, but what was published during the move is lost). Give the worker's workers.toml entry settings = { input_replay = "off" }.

To test it, deliver the same event twice through the worker's entry point (BehaviorTestHost.SendAsync(CloudEvent), ideally decoded from the wire) and assert that the second delivery publishes nothing and changes no state.