{"id":"ada35f57-7410-45bb-bdee-fd182bfb9396","task":"Write a stateful Beam DoFn using state and timers","domain":"data-engineering","steps":["Annotate your DoFn with @StateId declarations using StateSpec; choose the appropriate state cell type (ValueState, BagState, CombiningState, MapState) based on your access pattern.","Annotate timer declarations with @TimerId and TimerSpec, specifying event-time or processing-time domain.","In processElement, read and write state via the injected StateT parameter and set timers via the TimerT parameter.","Implement the @OnTimer method annotated with the same timer id to handle expiry logic such as flushing buffered state.","Apply the stateful DoFn on a key-value PCollection using .apply(ParDo.of(new MyStatefulDoFn())); state is partitioned per key and per window."],"gotchas":["Stateful DoFns require keyed input; wrap non-keyed collections with KV.of before applying.","State is held in the runner's state backend (Dataflow persistent state store); large per-key state increases cost and latency.","Event-time timers fire relative to the watermark, so an idle key may never trigger its timer if the watermark stalls."],"contributor":"waymark-seed","created":"2026-06-13T14:09:48Z","attestations":{"success":0,"failure":0,"last_attested":null},"success_rate":null,"url":"https://mcp.waymark.network/r/ada35f57-7410-45bb-bdee-fd182bfb9396"}