The Processor API
When the declarative DSL operators don't cover what you need, drop down to the Processor API: a record-by-record interface where you control forwarding, state access, periodic work, and timers directly. It mirrors the Kafka Streams Processor API, so code written for KS ports with only an import change. For the engine model behind state access and timer execution, see Architecture and Lanes and parallelism.
Processor vs FixedKeyProcessor
Two interfaces, chosen by whether your logic changes the record key:
| Interface | Attach with | Can change key? | Forward |
|---|---|---|---|
Processor<KIn, VIn, KOut, VOut> | KStream.process(...) | Yes | new key + value |
FixedKeyProcessor<KIn, VIn, VOut> | KStream.processValues(...) | No (key fixed) | value only |
Use FixedKeyProcessor whenever you only transform values — a fixed key keeps the record on the lane that already owns it, so per-key state stays serialised without any re-keying at all. Reach for Processor when you need to emit records under a different key, and read Key affinity after a re-key before you keep per-key state in one.
Both interfaces are in io.stoatflow.core.processor. The convenience base classes ContextualProcessor and ContextualFixedKeyProcessor store the context for you so you don't have to keep a field; context() returns it after init.
A value-only processor
FixedKeyProcessor transforms the value and forwards it under the original key. process(record) runs once per input record; forward zero records to filter the record out, or more than one to fan out.
import io.stoatflow.core.processor.ContextualFixedKeyProcessor
import io.stoatflow.core.processor.FixedKeyRecord
class UppercaseProcessor : ContextualFixedKeyProcessor<String, String, String>() {
override fun process(record: FixedKeyRecord<String, String>) {
val value = record.value
if (value.isNotBlank()) {
// key and timestamp are preserved automatically
context().forward(value.uppercase())
}
}
}
import io.stoatflow.core.processor.ContextualFixedKeyProcessor;
import io.stoatflow.core.processor.FixedKeyRecord;
public class UppercaseProcessor
extends ContextualFixedKeyProcessor<String, String, String> {
@Override
public void process(FixedKeyRecord<String, String> record) {
String value = record.value();
if (value != null && !value.isBlank()) {
// key and timestamp are preserved automatically
context().forward(value.toUpperCase());
}
}
}
A key-changing processor
Processor can emit records under a different key. KStream.process(...) is a key-changing operation, so downstream stateful operators see the new key.
process() and processValues() do not open a sub-topology boundary of their own, even after a re-key and even with connected stores — matching Kafka Streams exactly. Before 1.0.0 StoatFlow was stricter here and always opened one; that was the single remaining place a ported topology's describe() showed an extra sub-topology, and it is gone. Set topology.processor-api-key-affinity: presumed to restore the old boundary. What this costs you after a many-to-one re-key is below. See also Lanes and parallelism.import io.stoatflow.core.processor.ContextualProcessor
import io.stoatflow.core.processor.Record
class RouteByTypeProcessor : ContextualProcessor<String, Event, String, Event>() {
override fun process(record: Record<String, Event>) {
val event = record.value
// re-key on the event type, keeping the original timestamp
context().forward(Record(event.type, event, record.timestamp))
}
}
import io.stoatflow.core.processor.ContextualProcessor;
import io.stoatflow.core.processor.Record;
public class RouteByTypeProcessor
extends ContextualProcessor<String, Event, String, Event> {
@Override
public void process(Record<String, Event> record) {
Event event = record.value();
// re-key on the event type, keeping the original timestamp
context().forward(new Record<>(event.type(), event, record.timestamp()));
}
}
Suppliers
process and processValues take a supplier, not a processor instance — the engine creates one instance per key-affinity lane, calling get() for each:
ProcessorSupplier<KIn, VIn, KOut, VOut>—get()returns aProcessor.FixedKeyProcessorSupplier<KIn, VIn, VOut>—get()returns aFixedKeyProcessor.
get() must return a fresh instance every time (Kafka Streams' contract; a supplier that hands back the same reference twice fails loudly). Both are functional interfaces, so a lambda or method reference (::new) works. They also expose a stores() method (default: empty) for declaring state stores inline with the processor — see Attaching state stores.
process() sets; (2) after a many-to-one re-key, one key's records can arrive on several lanes, so per-key state in an instance field is spread across those lanes' instances — again exactly as in KS, where it is spread across tasks (see Key affinity after a re-key); (3) for state that must be shared across lanes or read by a punctuator, use a state store — StoatFlow's global-state channel — not a plain instance field. See State and thread safety and Attaching state stores.process(), processValues(), transformValues() or the low-level Topology.addProcessor gets a punctuator instance in addition to the per-lane record instances. It is created when the application starts, and its init() is what registers context.schedule(...) punctuators, so a punctuator-only processor with no input records still fires. Expect init()/close() once per instance — twice even with one lane: the punctuator instance at start, each lane's instance on its first record. Records reach only lane instances; punctuators run only on the punctuator instance. Deliberate divergence from Kafka Streams' one instance per task (KSC-160) — see A punctuator runs on its own instance.val routed: KStream<String, Event> =
events.process({ RouteByTypeProcessor() }, Named.`as`("route-by-type"))
val shouted: KStream<String, String> =
lines.processValues({ UppercaseProcessor() }, Named.`as`("uppercase"))
KStream<String, Event> routed =
events.process(RouteByTypeProcessor::new, Named.as("route-by-type"));
KStream<String, String> shouted =
lines.processValues(UppercaseProcessor::new, Named.as("uppercase"));
Named argument is optional but recommended — StoatFlow uses these stable names for the topology graph, metrics, and state-store identity. The same convention as the DSL operators in Your first app.ProcessorContext
init(context) is called once per instance. As in Kafka Streams, a node is initialised when the application starts — before any record arrives, with or without input (in tests: when the TopologyTestDriver is built): that first init() is its punctuator instance's. Each lane's instance is initialised on that lane's first record. In HA active/standby mode init runs when a pod becomes active, never on a standby. Inside init, context.timestamp() is the current wall-clock time (the driver's clock in tests). Store the context (or extend a Contextual* base class and use context()). The context gives you forwarding, record metadata, time access, state stores, punctuator scheduling, and the timer service.
Forwarding
ProcessorContext (key-changing) forwards a full Record or a key/value pair:
| Method | Behaviour |
|---|---|
forward(record) | Forward a Record<KOut, VOut> — the record's timestamp and headers ride to the sink (set them via record.withTimestamp(...) / record.withHeaders(...)) |
forward(key, value) | Forward using the current record's timestamp and headers |
forward(key, value, timestamp) | Forward with an explicit timestamp |
forward(record, childName) / forward(key, value, childName) | Forward to a specific named child node |
FixedKeyProcessorContext forwards values only — the key is fixed: forward(value), forward(value, timestamp), and the FixedKeyRecord / childName variants. key() reads the (immutable) key of the callback it is called in — the record's in process(), the timer's in onTimer, on a context you keep from init() too — and forward(value) forwards under it. init(), close() and a punctuator have no key: there both throw IllegalStateException, so forward a FixedKeyRecord with the key you mean. Kafka Streams' FixedKeyProcessorContext has neither method.
A timestamp can be any Long except Long.MIN_VALUE, which marks a missing event time: a Record or FixedKeyRecord built with it, withTimestamp(Long.MIN_VALUE), and every forward(…, timestamp) with it — from process, onTimer or a punctuator — throw IllegalArgumentException. Negative timestamps are valid and flow on like any other; a sink writes such a record without a timestamp, so the producer or broker stamps the wall-clock time (see Which event times are valid). Kafka Streams' Record rejects every negative timestamp.
A child connected to the same parent twice (topology.addProcessor("x", supplier, "p", "p")) receives a plain forward once per connection and a forward(record, "x") once, as in Kafka Streams.
A node with several parents (addProcessor("x", supplier, "a", "b")) may receive different key types from each. When its parents sit in different sub-topologies, StoatFlow hands each record over to the node's own lanes by its key, so one key reaches one lane whichever parent sent it. The key is hashed with the key serde the parents declare; if they declare different ones, each parent's records are hashed with that parent's own serde, and the build logs a warning naming the node — the same key may then reach it on two lanes. Declare one key serde for every input of such a node when its parents carry the same keys.
Record metadata and time
Both contexts expose the metadata of the record currently being processed and the engine's time notions:
| Method | Returns |
|---|---|
timestamp() | Timestamp of the current record. In onTimer the timer's time, in a punctuator the punctuation's, in init() the wall clock |
headers() | The headers of the record being processed (org.apache.kafka.common.header.Headers) — the same instance as record.headers(), including headers an upstream node set with forward(record.withHeaders(...)). In onTimer the timer's headers — those of the record that armed it (Timer headers); empty in punctuators and in init() |
topic() / partition() / offset() | Source coordinates of the current record. After a repartition() or any other re-key boundary, those of the source record it came from — StoatFlow repartitions in memory, where Kafka Streams reports its repartition topic. "" / -1 / -1 in onTimer, in punctuators and in init(), and for a record no topic delivered — one a timer, a punctuator or a scheduled source forwards, or a window's final result a watermark advance emits. A suppress result reports the record it was buffered with — its timestamp, headers, topic, partition and offset — as in Kafka Streams |
applicationId() | The configured application ID |
currentWatermarkMs() | Global event-time progress (min watermark across non-idle partitions); may be Long.MIN_VALUE before any events |
currentStreamTimeMs() | KS-compatible alias for currentWatermarkMs() |
currentSystemTimeMs() | Wall-clock time (System.currentTimeMillis(); under the TopologyTestDriver, the driver's wall clock) |
getStateStore(name) | A state store by name |
All five follow the callback they are called in, so a context you keep from init() always describes one record: the record in process(), the timer in onTimer, the punctuation in a punctuator, and no record in init() or close() (timestamp() is then the wall clock when you read it). Inside a callback, its own arguments — the Record, or the TimerContext in onTimer — say the same, and a kept context's forward(...) in onTimer reaches the children as the TimerContext's does.
They describe the record the calling thread is processing, so read them in the callback itself. A thread you start — an executor task, a CompletableFuture — sees no record, even one you wait for before process() returns: capture the values you need first, or take them from the Record. The same goes for forward(...): outside process(), onTimer() and a punctuator — in init(), close() or a thread you start — there is nothing to forward downstream of, so the record is dropped, and StoatFlow logs a WARN the first time a processor does it.
ProcessorContext additionally has isLate() — true when the current record arrived late: a record a topic delivered, or one forwarded from it, whose timestamp is below the watermark. An update an operator forwards while it processes an arrival — an aggregation's, a join's — counts as that arrival's record, judged by the timestamp it is forwarded with. What a timer, a punctuator or a scheduled source produces is never late, and neither is a window's final result emitted when the watermark closes the window, a suppress(untilWindowCloses | untilTimeLimit) result, or anything forwarded from them — their time is below the watermark by construction. A Suppressed.untilBarrier() flush is judged against the watermark its update arrived under, so emit.mode = on-barrier does not change isLate() — also further down, where a processor forwards the record with a new timestamp. In timer and punctuator callbacks it returns false. See Event time and watermarks.
process(...), onTimer(...), and punctuators. Reads see your own uncommitted writes; the engine flushes and makes them durable at commit barriers. Under exactly_once a read is as of the reading lane's own commit: a processor, timer or punctuator never sees a value another lane has already written for the next, still-uncommitted commit (except from the two stores that cannot answer that way — see Reading a store from another lane). Under at_least_once, and for interactive queries, reads see the latest state.Threads you start yourself are not supported under exactly_once. Such a thread is not a processing lane: its reads see the latest state rather than its lane's commit, and its writes are not attributed to the lane's commit. See State and thread safety.Attaching state stores
A processor reads and writes a store by name via context.getStateStore(name). The store must be connected to that processor — asking for one that isn't throws StoreNotConnectedException, exactly as Kafka Streams does. There are two ways to connect one, and they union:
Option A — declare stores in the supplier
Override stores() on the supplier to return the StateStoreBuilders the processor needs. The engine creates and connects them automatically. Build store builders with the Stores factory in io.stoatflow.core.state.
import io.stoatflow.core.processor.ProcessorSupplier
import io.stoatflow.core.state.StateStoreBuilder
import io.stoatflow.core.state.Stores
import org.apache.kafka.common.serialization.Serdes
val supplier =
object : ProcessorSupplier<String, Event, String, Event> {
override fun get() = SessionProcessor("sessions")
override fun stores(): Set<StateStoreBuilder<*>> =
setOf(
Stores.keyValueStoreBuilder(
Stores.persistentKeyValueStore("sessions"),
Serdes.String(),
sessionSerde,
),
)
}
val out = events.process(supplier, Named.`as`("sessionize"))
import io.stoatflow.core.processor.Processor;
import io.stoatflow.core.processor.ProcessorSupplier;
import io.stoatflow.core.state.StateStoreBuilder;
import io.stoatflow.core.state.Stores;
import org.apache.kafka.common.serialization.Serdes;
import java.util.Set;
ProcessorSupplier<String, Event, String, Event> supplier =
new ProcessorSupplier<>() {
@Override
public Processor<String, Event, String, Event> get() {
return new SessionProcessor("sessions");
}
@Override
public Set<StateStoreBuilder<?>> stores() {
return Set.of(
Stores.keyValueStoreBuilder(
Stores.persistentKeyValueStore("sessions"),
Serdes.String(),
sessionSerde));
}
};
KStream<String, Event> out = events.process(supplier, Named.as("sessionize"));
Option B — register on the builder
Register the store on the StreamsBuilder with addStateStore(...), then name it in the process(...) call — registering alone does not connect it to any processor. persistentKeyValueStore is the recommended store type for production (RocksDB, changelog-backed); inMemoryKeyValueStore is available for non-durable state. See State stores.
import io.stoatflow.core.state.Stores
import org.apache.kafka.common.serialization.Serdes
builder.addStateStore(
Stores.keyValueStoreBuilder(
Stores.persistentKeyValueStore("sessions"),
Serdes.String(),
sessionSerde,
),
)
// The trailing store name is what connects "sessions" to this processor.
val out = events.process({ SessionProcessor("sessions") }, Named.`as`("sessionize"), "sessions")
import io.stoatflow.core.state.Stores;
import org.apache.kafka.common.serialization.Serdes;
builder.addStateStore(Stores.keyValueStoreBuilder(
Stores.persistentKeyValueStore("sessions"),
Serdes.String(),
sessionSerde));
// The trailing store name is what connects "sessions" to this processor.
KStream<String, Event> out =
events.process(() -> new SessionProcessor("sessions"), Named.as("sessionize"), "sessions");
getStateStore(name) throws StoreNotConnectedException — a fatal, non-recoverable error — if name is not connected to this processor node. The message names the processor, the store, and what the node actually declares. Two categories are exempt and need no declaration: global stores (addGlobalStore(...) and globalTable(...)), which are handed to an undeclaring processor read-only — declare one anyway and you get write access, a StoatFlow extension Kafka Streams rejects outright; and read-only stores (addReadOnlyStateStore(...)).A lookup in init — where it usually lives — fails at startup, as in Kafka Streams: start() throws (in tests, the TopologyTestDriver constructor). A lookup in process() fails on the record that makes it. Either way it is deliberately not routable to processing.exception.handler: a CONTINUE or DLQ handler cannot swallow a topology-configuration error.
process; the engine reads the store when a window closes). Punctuators have full read/write store access. For ephemeral scratch that doesn't need to survive a restart, use an in-memory, logging-disabled store:builder.addStateStore(
Stores
.keyValueStoreBuilder(Stores.inMemoryKeyValueStore("buffer"), Serdes.String(), valueSerde)
.withLoggingDisabled(), // ephemeral — no changelog topic
)
Sharing a store between processors
A processor connected to a store that another part of the topology writes — a table()'s store, say, or another processor's — is placed with that store's writer, as Kafka Streams puts processors that share a store into one sub-topology. With no re-key between them, a key reaches the same lane in both, so their writes to that key are serial. Only writers pull nodes together: a node that merely reads the store, such as a join, is not moved.
A re-key between two processors that connect the same store puts them in different sub-topologies. Their lanes cross commit barriers at different times, so a later write from one can land in an older commit than an earlier write from the other and be hidden by it — a lost update, under either processing guarantee. Kafka Streams forces such processors into one sub-topology (KAFKA-20464); StoatFlow places nodes by key, so it warns instead, with the presumed-cross-sub-topology-write rule (default warn, see the configuration reference). If only one of them writes the store and the others only read it, nothing is lost: set the rule to off. A punctuator counts as a writer: its store work pauses only its own sub-topology, so the other sub-topology's lanes keep writing the store meanwhile. Otherwise connect the store from one sub-topology only, or let one processor own it and forward what the others need. The same shape among DSL writers is refused at build time.
Reading a store from another lane
Stores.lruMap(...) and Stores.inMemoryVersionedKeyValueStore(...) write directly instead of batching writes per commit, so their reads are always live. Under exactly_once a read of one from another lane can see a write that is not committed yet. When a Processor API node may read such a store across lanes — from a punctuator, or a node downstream of one in the same sub-topology, while record lanes write it; or from another sub-topology than the store's writer — the build warns with the presumed-cross-lane-read rule (default warn). A global or read-only store of either kind counts as read by every lane, because any processor may read one without declaring it.
To fix it, materialise the store with a default store supplier or, for versioned state, Stores.persistentVersionedKeyValueStore(...) — both are read as of the lane's commit — or run at_least_once. If the processor never reads the store that way, set the rule to off. The same read by a DSL join is refused at build time (joins under exactly-once), as is a DSL node that reads such a store on the punctuation lane — downstream of a LEFT or OUTER stream–stream join, say — while record lanes write it. A store that only the punctuation lane writes and reads, such as the table an OnWindowClose aggregation feeds, is fine.
Reading and writing the store
Look up the store in init, then use it in process (and onTimer). getStateStore is generic over the store type; in Kotlin pass the type argument, in Java assign to a typed variable. The same connection rule applies in every callback — process, onTimer and punctuators all see the node's declared set.
import io.stoatflow.core.processor.ContextualProcessor
import io.stoatflow.core.processor.ProcessorContext
import io.stoatflow.core.processor.Record
import io.stoatflow.core.state.KeyValueStore
class CountingProcessor : ContextualProcessor<String, String, String, Long>() {
private lateinit var counts: KeyValueStore<String, Long>
override fun init(context: ProcessorContext<String, Long>) {
super.init(context)
counts = context.getStateStore("counts")
}
override fun process(record: Record<String, String>) {
val next = (counts.get(record.key) ?: 0L) + 1
counts.put(record.key, next)
context().forward(record.key, next)
}
}
import io.stoatflow.core.processor.ContextualProcessor;
import io.stoatflow.core.processor.ProcessorContext;
import io.stoatflow.core.processor.Record;
import io.stoatflow.core.state.KeyValueStore;
public class CountingProcessor
extends ContextualProcessor<String, String, String, Long> {
private KeyValueStore<String, Long> counts;
@Override
public void init(ProcessorContext<String, Long> context) {
super.init(context);
this.counts = context.getStateStore("counts");
}
@Override
public void process(Record<String, String> record) {
long current = counts.get(record.key()) == null ? 0L : counts.get(record.key());
long next = current + 1;
counts.put(record.key(), next);
context().forward(record.key(), next);
}
}
Key affinity after a re-key
Normally a key's records all run on one lane, so per-key read-modify-write is serial and you need no locking. A many-to-one re-key immediately upstream of a Processor API node breaks that — and it is the one shape StoatFlow can detect at build time. (The other way to break it is to write a store under a key that is not the record key; see state and thread safety. That one is computed inside your processor at runtime, so no compiler can see it.)
Since 1.0.0 a Processor API node does not, by itself, require key affinity (topology.processor-api-key-affinity: off) — matching Kafka Streams, which never materialises a repartition before process() / processValues() even with connected stores. StoatFlow lanes records by the key they arrived with, so:
| Shape | What happens |
|---|---|
| No re-key upstream | Unchanged — one lane per key. |
Injective re-key (k → "user-$k", 1:1) | Unchanged — distinct new keys still map to distinct lanes. Safe for free. |
Node declares a timer (declaredTimerTypes() non-empty) | Unchanged — the boundary is kept, because the timer service hashes the new key while records lane by the old one. |
| Many-to-one re-key (several old keys → one new key) | Records for that new key arrive on several lanes and are processed concurrently. |
In the last row, per-key state is fragmented across lanes: instance fields stay race-free (one instance per lane), but a given key's state now lives in several of them — which is exactly what happens across tasks in Kafka Streams after the same re-key without a repartition(). State in a store is worse than fragmented, and it is worth being precise about why.
compute() nor a lock closes this.compute(key, fn) and merge(...) are atomic within a commit epoch. Across a barrier rotation they are not, and no user-level lock can make them be: a lane still draining epoch N is structurally forbidden from reading a value another lane wrote into epoch N+1 — allowing it would import uncommitted state into a committed transaction. So both writes can derive from the same base and one is durably lost, with the lock held. A lock makes the safe ordering deterministic when it already exists; it cannot create it.StoatFlow refuses to compile the provable case
When a re-key feeds a store-connected Processor API node with no boundary between them, the topology fails to build — in your own test suite, with no broker, because the check runs at topology-compile time:
Topology validation failed: 1 violation(s) configured as 'error'.
- [papi-key-affinity-presumed] Processor API node 'papi' connects 'counts' and follows the
re-key at 'rekey' with no sub-topology boundary between them, so records reach it on the
lane of their OLD key. ...
Three remedies, in order of preference:
- Insert
repartition()before the node. In StoatFlow this is an in-memory lane hand-off, not a Kafka topic — no broker round-trip, no extra latency budget, no topic to provision. If you are arriving from Kafka Streams, this is much cheaper than it looks there. - Set
topology.processor-api-key-affinity: presumedto restore the boundary for every Processor API node in the application. - Stop keying the store by the post-re-key key — keep it keyed by something that is still lane-stable.
If the re-key really is injective, downgrade the rule with topology.validation.papi-key-affinity-presumed: warn. Read-only stores (KIP-813) need no action — they cannot be written, so they are excluded from the check.
When the re-key is only presumed
process() and Topology.addProcessor() declare a key change unconditionally, because StoatFlow cannot see inside your Processor. So in a chain like process(parse).process(count, "store") — the shape a KSML-generated topology is made of — the "re-key" may not exist at all, and Kafka Streams compiles and runs it without a repartition.
Failing that build would be a refusal on evidence that does not exist, so it gets its own rule, papi-key-affinity-presumed-chain, defaulting to warn:
Topology validation [papi-key-affinity-presumed-chain]: Processor API node 'count' connects
'store' and follows the Processor API node 'parse' with no sub-topology boundary between them.
StoatFlow cannot see whether 'parse' changes the key ... but IF it does, and many-to-one, then ...
Only you can answer it. If parse does not change the key, nothing is wrong — set the rule to off. If it does, the remedies above apply: one key's records can then reach several lanes of one sub-topology, which cross commit barriers at different times, so two writes of that key can land in the wrong commit order and one is lost. Commit ordering protects a key only while it stays on one lane, so this warning is the only guard for that shape. Making the re-key explicit upstream (a selectKey) also moves the finding to the error-default rule, where it belongs.
What the build-time check cannot see
Two things.
Per-lane instance fields. No declaration can express them, so nothing can prove the shape. After a many-to-one re-key with the setting off, per-key state held in a processor's own fields is fragmented across lanes — exactly as it is across tasks in Kafka Streams. State held in a store is the case StoatFlow refuses to build, because that is the only place StoatFlow would be worse than Kafka Streams (KS fragments the value across N stores; StoatFlow can lose an update) rather than merely equal to it.
A store key that is not the record key. store.put(record.value().customerId, …) has the same cross-barrier window with no re-key anywhere, because the key you store under is not the key you are laned by. It is computed at runtime, so the compiler never sees it. State and thread safety covers what a lock does and does not buy you there.
Boundaries elided under off are logged once at startup, at INFO, naming the nodes — so an elided boundary is visible somewhere other than this page.
Consumed.withNumberOfLanes(...) set on the source. Under presumed it would have started its own. If you tuned lanes per sub-topology, re-check them after the flip.Punctuators
A punctuator is a callback that runs periodically — useful for heartbeats, metrics, or flushing buffered state. Schedule one in init via context.schedule(...). It returns a Cancellable you can use to stop it — in close() or from the punctuator itself, never from process() (why).
Because init runs when the application starts, a punctuator scheduled there starts with the application — a catch-up, heartbeat or TTL-cleanup punctuator runs after a restart even if no record arrives, as in Kafka Streams. When it fires is Kafka Streams' rule too — see Fire times.
schedule takes an interval, a TimeNotion and the callback — Kafka Streams' schedule(Duration, PunctuationType, Punctuator); an anchored form takes a start time first. The callback receives the trigger timestamp.
Time notion
TimeNotion is the same type used for timers and StreamsBuilder.scheduled(...). It carries both StoatFlow/Kafka-Streams and Flink naming for the same two concepts:
| Constant | Alias | Fires when | Timestamp passed |
|---|---|---|---|
TimeNotion.STREAM_TIME | EVENT_TIME | the watermark reaches the next point on its grid | the current watermark |
TimeNotion.WALL_CLOCK_TIME | PROCESSING_TIME | system time reaches the next point on its grid | the current system time |
From Java, the PunctuationType object exposes STREAM_TIME / WALL_CLOCK_TIME and TimeDomain exposes EVENT_TIME / PROCESSING_TIME — both resolve to the same TimeNotion constants.
Fire times
Fire times follow Kafka Streams:
- A
STREAM_TIMEpunctuator is aligned to 0: it fires on the first data — the first watermark, even a negative one — and then at every multiple of the interval after it. When the first watermark is at or past 0 this is exactly Kafka Streams' schedule; Kafka Streams never sees a negative one. - A
WALL_CLOCK_TIMEpunctuator first fires one interval after it was scheduled, then every interval on that grid. - An anchored punctuator (
schedule(startTime, interval, …), KIP-1146 — the start time first, as in Kafka Streams) first fires atstartTime— or, ifstartTimeis already behind the current time, at the first grid point after it. AstartTimeofLong.MIN_VALUEthrowsIllegalArgumentException. - When time jumps past several grid points, the punctuator fires once, with the current time, and the next fire is the next grid point. Fire times never drift from the grid.
Inside the callback the context describes the punctuation, not the last record: context.timestamp() is the punctuation time (the value punctuate receives), context.headers() is empty — one instance for the whole punctuation, so a header you add there is carried by its forward(key, value) — and context.topic() / partition() / offset() return "" / -1 / -1. A record built with context.timestamp() in a punctuator therefore carries the punctuation time — as in Kafka Streams.
A punctuator runs on its own instance
Besides its lane instances, every Processor API node has one punctuator instance. context.schedule(...) registers punctuators only there, and they always run on it; process() never does. A punctuator therefore sees the fields its own instance's init() set, and none that process() sets — under the TopologyTestDriver too. On a lane instance, schedule(...) returns a Cancellable that does nothing. In Kafka Streams a punctuator shares its task's instance with process() (KSC-160).
Keep everything a punctuator shares with process() in a state store. Two Kafka Streams patterns compile unchanged and then fail silently:
- Records buffered in a field and flushed from a punctuator. The punctuator instance's buffer stays empty, so the punctuator flushes nothing. Buffer in a key-value store instead: put in
process(), iterate and delete in the punctuator (below). - A
Cancellablekept in a field and cancelled fromprocess(). The field holds the do-nothingCancellablethe lane instance got, so the punctuator keeps firing. Cancel from inside the punctuator — it runs on the instance that scheduled it — or keep the decision in a store the punctuator reads.
import io.stoatflow.core.processor.ContextualProcessor
import io.stoatflow.core.processor.ProcessorContext
import io.stoatflow.core.processor.Record
import io.stoatflow.core.processor.TimeNotion
import io.stoatflow.core.state.KeyValueStore
import java.time.Duration
// "batch-buffer" is connected at the call site: process({ BatchingProcessor() }, named, "batch-buffer")
class BatchingProcessor : ContextualProcessor<String, Event, String, Event>() {
private lateinit var buffer: KeyValueStore<String, Event>
override fun init(context: ProcessorContext<String, Event>) {
super.init(context)
buffer = context.getStateStore("batch-buffer")
// Registered on the punctuator instance; the lane instances get a do-nothing Cancellable.
context.schedule(Duration.ofSeconds(30), TimeNotion.WALL_CLOCK_TIME) { ts ->
val flushed = mutableListOf<String>()
buffer.all().use { entries ->
entries.forEach { entry ->
context.forward(Record(entry.key, entry.value, ts))
flushed += entry.key
}
}
flushed.forEach { buffer.delete(it) }
}
}
override fun process(record: Record<String, Event>) {
// A store, not a field: the punctuator runs on another instance.
buffer.put(record.key, record.value)
}
}
import io.stoatflow.core.processor.ContextualProcessor;
import io.stoatflow.core.processor.ProcessorContext;
import io.stoatflow.core.processor.PunctuationType;
import io.stoatflow.core.processor.Record;
import io.stoatflow.core.state.KeyValue;
import io.stoatflow.core.state.KeyValueIterator;
import io.stoatflow.core.state.KeyValueStore;
import java.time.Duration;
import java.util.ArrayList;
import java.util.List;
// "batch-buffer" is connected at the call site: process(BatchingProcessor::new, named, "batch-buffer")
public class BatchingProcessor extends ContextualProcessor<String, Event, String, Event> {
private KeyValueStore<String, Event> buffer;
@Override
public void init(ProcessorContext<String, Event> context) {
super.init(context);
this.buffer = context.getStateStore("batch-buffer");
// Registered on the punctuator instance; the lane instances get a do-nothing Cancellable.
context.schedule(Duration.ofSeconds(30), PunctuationType.WALL_CLOCK_TIME, ts -> {
List<String> flushed = new ArrayList<>();
try (KeyValueIterator<String, Event> entries = buffer.all()) {
while (entries.hasNext()) {
KeyValue<String, Event> entry = entries.next();
context.forward(new Record<>(entry.key, entry.value, ts));
flushed.add(entry.key);
}
}
flushed.forEach(buffer::delete);
});
}
@Override
public void process(Record<String, Event> record) {
// A store, not a field: the punctuator runs on another instance.
buffer.put(record.key(), record.value());
}
}
How a punctuator runs alongside records
Each processor's punctuators run on a thread of their own: one processor's punctuators one after another, different processors' in parallel, and all of them concurrently with record processing. A punctuator fires on time, whatever the record lanes are doing. A run that is still going when its next fire is due skips that fire.
A run has two parts:
- The free part: everything before its first access to a connected state store, its timer service or a
KeyLockManagerlock — calling an external service, computing, logging, reading a global or read-only store, forwarding. It runs concurrently with everything and pauses nothing. - The shared part: from that first access to the end of the run. It waits for the next commit barrier — StoatFlow creates one at once when none is due — and runs there as exclusive work: the record lanes of the processor's sub-topology stop at the barrier, the run goes on alone, the records it forwarded go downstream, then the lanes continue. Runs that wait for the same barrier run there one after another. What the run writes and forwards commits with that barrier.
A run that touches no state never waits for a barrier, and its forwards are handed downstream when it ends. If they reach a node of the processor's own sub-topology — anything before the next re-key boundary, a sink included — that hand-off also takes a short exclusive window at the next barrier; forwards that only cross into another sub-topology need none.
A state store of your own type — one your own StoreBuilder builds rather than the Stores factory — is handed to your processor as it is, so init() may cast it to its own class, as in Kafka Streams. StoatFlow cannot tell when such a store is first touched, so once a processor's context has handed one out, every run of that processor's punctuators is a shared part from its start: it waits for the next barrier before your punctuator code runs, and the sub-topology's record lanes stay stopped for the whole run.
So no key is processed by a punctuator and a record lane at the same time, and a punctuator's read-modify-write of a key never loses an update process() made, under either processing guarantee.
Keep the shared part short. It waits for the next barrier, and while it runs, its sub-topology takes no input. Under load the pause spreads: the paused sub-topology's lane queues fill within a few hundred records, and since the dispatcher hands a batch out only when every lane it goes to has room, the whole application's input stops until the lanes continue. Do slow work — an external call, a large computation — in the free part, and touch the stores last. The stoatflow.subtopology.exclusive.work timer measures each pause, and one held past 5 seconds logs a WARN naming the punctuators. A shared part still running past stoatflow.commit-stall.threshold-ms (45 s by default) stops the application, as any stalled commit does. Because a run with a shared part lasts at least until its barrier, a schedule whose interval is shorter than that wait skips fires.
Its writes follow processing order, not timestamps. A punctuator's writes land after every record dispatched before its barrier, on every lane. A read-then-write inside the shared part — an increment, an update from the current value — always builds on the newest value. A write computed from something read earlier — an instance field, an external call, a global store read in the free part — replaces whatever a lane wrote to that key earlier in the commit, and a forward with an older timestamp still becomes a plain table's latest value. To keep a newer value, compare the stored ValueAndTimestamp inside the punctuator, use a versioned store (it keeps an older write as history), or use a timer.
Global and read-only stores never pause a punctuator. In the free part they read committed data under exactly_once — a read can be up to one commit behind a lane's, as a Kafka Streams global store holds only committed topic data; in the shared part they read as the lanes do.
Locks. KeyLockManager.withLock / withLocks in a punctuator first waits for the run's exclusive work, then takes the lock — the lanes are stopped by then, so it is free. Any other lock, semaphore or pooled resource that a punctuator shares with process() must not be held across the punctuator's first store, timer or KeyLockManager access: a lane blocked on it in process() never reaches the barrier the punctuator waits for, and the application stops at the commit-stall threshold. See State and thread safety.
Shutdown. A graceful shutdown waits for the punctuator runs in flight, within the time it allows for its final commit, and commits their effects with it. A run still going then is abandoned — nothing of it commits — and the punctuator fires again after the restart. A slow free part can therefore make a graceful shutdown take longer.
After a crash under at-least-once
Under at_least_once no state change is durable before its output — a punctuator's, an onTimer call's, a record's. Each commit first waits until the epoch's output is acknowledged; only then do the cached stores send the epoch's changelog records, and the stores that log as they are written — the timer store, versioned stores and lruMap — release theirs, which they hold until that commit. After a hard crash a punctuation's output and its state change are therefore kept or lost together: if they were lost, the punctuator runs again from the last committed state, and at most repeats output. Records a punctuator forwards into a downstream sub-topology get the same guarantee there. Kafka Streams can lose a punctuator's output in this situation.
The cost: the timer, versioned and lruMap changelogs — and an HA standby — trail the application by up to one commit, and the held writes count toward the uncommitted-state memory limit. One case is not covered: an output whose send fails.
Example
import io.stoatflow.core.processor.Cancellable
import io.stoatflow.core.processor.ContextualProcessor
import io.stoatflow.core.processor.ProcessorContext
import io.stoatflow.core.processor.Record
import io.stoatflow.core.processor.TimeNotion
import java.time.Duration
class HeartbeatProcessor : ContextualProcessor<String, Int, String, Int>() {
private var schedule: Cancellable? = null
override fun init(context: ProcessorContext<String, Int>) {
super.init(context)
// wall-clock heartbeat every 10 seconds
schedule =
context.schedule(Duration.ofSeconds(10), TimeNotion.WALL_CLOCK_TIME) { ts ->
context.forward("heartbeat", ts.toInt())
}
}
override fun process(record: Record<String, Int>) {
context().forward(record.key, record.value)
}
override fun close() {
schedule?.cancel()
}
}
import io.stoatflow.core.processor.Cancellable;
import io.stoatflow.core.processor.ContextualProcessor;
import io.stoatflow.core.processor.ProcessorContext;
import io.stoatflow.core.processor.PunctuationType;
import io.stoatflow.core.processor.Record;
import java.time.Duration;
public class HeartbeatProcessor
extends ContextualProcessor<String, Integer, String, Integer> {
private Cancellable schedule;
@Override
public void init(ProcessorContext<String, Integer> context) {
super.init(context);
// wall-clock heartbeat every 10 seconds
this.schedule = context.schedule(
Duration.ofSeconds(10),
PunctuationType.WALL_CLOCK_TIME,
ts -> context.forward("heartbeat", (int) ts));
}
@Override
public void process(Record<String, Integer> record) {
context().forward(record.key(), record.value());
}
@Override
public void close() {
if (schedule != null) {
schedule.cancel();
}
}
}
context.schedule(...) also has an anchored overload that takes a startTime: Instant, snapping fire times to a fixed grid aligned to that anchor (KIP-1146) for deterministic, cron-like cadence. For topology-level periodic record generation that isn't tied to a processor, see Scheduled sources.Timers
A timer fires once for a specific key at a specific instant, in event time or processing time. Unlike punctuators, timer callbacks run in the same per-key context as records — so onTimer(...) has full read/write state access and forwarding, with the same serialized execution as process(...) for that key. Like Flink's keyed timers — though a StoatFlow timer may be for any key, not only the current one — they are the right tool for per-key timeouts, debounce windows, and scheduled emissions.
Declaring timer types
A processor that uses timers must override declaredTimerTypes() to return the TimeNotions it will register. The default is the empty set. The engine uses the declaration three ways:
- Validation. Registering an undeclared type throws
IllegalStateException. - Hot path. It can skip watermark tracking when no processor declares event-time timers.
- Refusal. Event-time timers fire only on the watermark, so a topology with a processor that declares
EVENT_TIMEis refused (TopologyValidationException, also by theTopologyTestDriver) where the watermark cannot advance: undertimestampCoordinationMode = DISABLED, beside a source read withWatermarkStrategy.noWatermarks(), or when the topology reads no Kafka topic. - The timer store. The timer store and its
<application.id>-timer-store-changelogtopic exist only when some processor declares a timer type — on every timer backend; theMEMORYbackend keeps the store in memory. A Processor API topology whose processors declare no timer type gets neither, even if it never registers a timer.
If you drop every declaration from a topology that had timers, the local timer-store directory is cleaned up as stale (after state.cleanup-delay-ms), and its pending timers stop firing. They stay in the changelog topic, so declaring timers again brings them back — unless the timer backend keeps no changelog (changelog-enabled: false), ran without one at some point, or state restoration is off. Then the directory may hold their only copy, and deleting it loses them: StoatFlow logs a warning first, and state.cleanup-delay-ms: -1 keeps the directory.
A processor.wrapper.class decorator that does not forward declaredTimerTypes() drops the declaration in the same way; StoatFlow logs a warning when it does.
Timer key serde
Timer keys are serialized when a timer is registered, deleted and persisted, and deserialized when it fires. The timer's lane is chosen by hashing those bytes, the same way a record's lane is chosen by hashing its key bytes. A processor's timer key serde is the key serde declared for the key it receives — upstream (a Consumed, Grouped or Repartitioned serde) or downstream before any key change (a Repartitioned or Produced serde) — otherwise default.key.serde. When nothing on the records' path declares it, an upstream DSL store's key serde counts too: behind groupBy(…).count(Materialized.withKeySerde(…)).toStream(), the timer key serde is the Materialized one, the serde its records are laned by. A Processor API store never counts — its keys are whatever your processor writes. Where several branches reach the processor (a merge, or a Processor API node with several parents), a branch that changes the key declares nothing, and branches that declare different serdes leave the choice to default.key.serde. A Processor API node in between (process() / addProcessor()) counts as a key change, so a timer behind one resolves from downstream declarations or the default. Behind a windowed aggregation the timer key is a Windowed<K>, while the records keep the lane of their inner key K (a windowed aggregation is not a key change). Such a timer is laned by the inner key's bytes — through every windowing, for a key windowed more than once — and fires on that lane, so all of a key's windows, records and timers alike, stay on one lane.
A timer key is serialized for the topic <application.id>-<processor>-timer, the same at every operation. A Schema Registry key serde therefore registers the processor's timer key type under a subject of its own, <application.id>-<processor>-timer-key — see Subjects StoatFlow registers.
For a Processor API source, declare the key serde with 🆕 Topology.addSource(name, Consumed.with(keySerde, valueSerde), topics). The Consumed applies exactly as it does for StreamsBuilder.stream(...): serdes, timestamp extractor or watermark strategy, offset reset and lane overrides. A Consumed name must equal the node name. A Pattern overload takes a topic pattern instead of topics.
import io.stoatflow.core.topology.Consumed
import io.stoatflow.core.topology.Topology
import org.apache.kafka.common.serialization.Serdes
val topology = Topology()
.addSource("events", Consumed.with(Serdes.String(), eventSerde), "events")
.addProcessor("sessionize", { SessionProcessor() }, "events")
import io.stoatflow.core.topology.Consumed;
import io.stoatflow.core.topology.Topology;
import org.apache.kafka.common.serialization.Serdes;
Topology topology = new Topology()
.addSource("events", Consumed.with(Serdes.String(), eventSerde), "events")
.addProcessor("sessionize", SessionProcessor::new, "events");
addGlobalStore(...) and addReadOnlyStateStore(...) have the same Consumed-taking twin (storeBuilder, sourceName, consumed, topic, processorName, supplier), so a state-update processor's keys, timers included, use the source's own codec. Those sources always read from the beginning, so a Consumed offset reset other than earliest is refused. All three are StoatFlow extensions; the Kafka Streams-shaped (de)serializer overloads are unchanged.
addSource(name, keyDeserializer, valueDeserializer, topics) declares a deserializer, not a serde. A deserializer can read a key but not write one, so behind it the timer key serde falls back to default.key.serde, which must then serialize the key type to exactly the topic's key bytes. A default that cannot serialize the key type fails at the first timer registration with a ClassCastException naming the processor and the fix. A SerializationException from the serializer is rethrown unchanged, since it may be a transient Schema Registry failure.
The contract. A key's timers run on the same lane as its records, serially with them, whenever the timer-key-lane-affinity validation rule is silent. The one exception is a custom StreamPartitioner on repartition(), which picks a record's lane without hashing the key bytes.
- At build time, the
timer-key-lane-affinityrule (defaultwarn) reports a timer-aware processor whose timer bytes may differ from its records' lane bytes: one behind a source that declares only a key deserializer, or one entered across a sub-topology boundary laned by a different key serde. The fix is to declare the key serde —Consumed.with(Serdes.ByteArray(), …)for a byte-array source, or for a boundary the same key serde on the operator that opens it (Repartitioned/Grouped). Ifdefault.key.serdealready writes exactly the topic's key bytes, set the rule tooff. - At runtime, StoatFlow serializes the timer key and checks which lane those bytes pick against the lane the record was laned to. Bytes that differ but land on the same lane are no split, so they are never reported. There are two checks:
- The guard runs on every timer registered or deleted for the key being processed. A timer key that picks another lane — a default that encodes the key differently, header-mode input, Schema Registry id drift — would split that key's timers from its records. Its verdict is set by
timer.lane-affinity.guard(YAMLstoatflow.processing.timer-lane-affinity-guard):error(default) logs the split at ERROR and throws the fatalTimerLaneAffinityException(io.stoatflow.core.exception, aStreamsException), which bypassesprocessing.exception.handler, so the epoch aborts and its offsets are not committed. The timer is not registered, and catching the exception does not hide it: whether aprocess()catches it around the timer call or an upstream processor catches it aroundcontext.forward, StoatFlow rethrows it once that processor returns, and again when the record's traversal ends — the same holds for every failure that must stop the record, such as a FAIL verdict or a store-connection violation;warnlogs once per processor and lets the timer register;offdoes not check. Every split counts onstoatflow.timer.lane.mismatch.total{processor}. - The sampled check re-serializes the record key before
process(the first 64 records per processor and lane, then 1 in 4,096). Its verdict is set bytimer.lane-affinity.sampled-check(YAMLstoatflow.processing.timer-lane-affinity-sampled-check):warn(default) logs once per processor, counts every split onstoatflow.timer.lane.mismatch.totaland keeps running;errorthrowsTimerLaneAffinityException;offdoes not sample. It defaults towarnbecause it also judges processors that never register a timer for the record's own key, and some wire encodings legitimately differ from a re-serialization (an evolved Schema Registry id, JSON written by another producer).
Neither check runs on sub-topologies with a single lane (no split is possible there), on records whose lane arepartition()StreamPartitionerpicked, or for a registration fromonTimeror a punctuator — only the record being processed is judged. Lanes depend on the sub-topology's lane count, so a key that shares a lane at one lane count may not at another; declaring the key serde removes the question. - The guard runs on every timer registered or deleted for the key being processed. A timer key that picks another lane — a default that encodes the key differently, header-mode input, Schema Registry id drift — would split that key's timers from its records. Its verdict is set by
- In tests, the
TopologyTestDriversamples every piped record and judges a split over the processor's sub-topology lane count — itsnumberOfLanesoverride, elsenum.stream.threads, else a fixed reference count of 16, never the build machine's cores — following the same two settings, and it applies arepartition()partitioner per record as the engine does. Setnum.stream.threadsto your production value to judge over exactly that, andtimer.lane-affinity.sampled-check=errorto make a systematic mismatch fail the unit test before deployment.
addSource(..., Consumed...) to an app that ran on default.key.serde — changes the bytes of every future registration, while timers already stored keep the old bytes. Those fail to decode when they fire, or are never deleted — or, worse, decode successfully as a different key, so onTimer acts on the wrong key's state. Settle the key serde before you rely on persisted timers.Registering and handling timers
Get the timer service in init via context.timerService(). Register timers in process (or onTimer), and handle them by overriding onTimer(timestamp, key, context).
TimerService<K> method | Effect |
|---|---|
registerEventTimeTimer(key, timestamp) | Fire when the watermark reaches timestamp (watermark ≥ timestamp) |
registerProcessingTimeTimer(key, timestamp) | Fire when wall-clock time reaches timestamp |
registerEventTimeTimer(key, timestamp, headers) / registerProcessingTimeTimer(key, timestamp, headers) | The same, with headers added to the ones the timer captures — see Timer headers |
deleteEventTimeTimer(key, timestamp) | Cancel a pending event-time timer |
deleteProcessingTimeTimer(key, timestamp) | Cancel a pending processing-time timer |
registerEventTimeTimer(key, name, timestamp) | A named timer: fire when the watermark reaches timestamp, replacing the name's pending timer in either domain — see Named timers |
registerProcessingTimeTimer(key, name, timestamp) | A named timer that fires when wall-clock time reaches timestamp, replacing the name's pending timer in either domain |
deleteTimer(key, name) | Cancel a named timer, whatever its domain |
deleteAllNamedTimers(key) | Cancel every named timer of the key; its unnamed timers stay |
registerEventTimeTimer(key, name, timestamp, headers) / registerProcessingTimeTimer(key, name, timestamp, headers) | A named timer with headers of its own |
namedTimer(key, name) | The named timer's pending time — a NamedTimer with timeNotion() and timestamp() — or null |
currentWatermark() / currentProcessingTime() | Read the current stream time / wall-clock time (the driver's wall clock under the TopologyTestDriver, so a timer registered at currentProcessingTime() + d fires on advanceWallClockTime(d)) |
Unnamed timers are deduplicated by (key, timestamp) — registering the same pair twice keeps one timer, and it fires once. That holds even when the timer is already due and its fire is queued on its lane, behind records still to be processed: a registration of the same (key, timestamp) from that lane that reaches it before it fires does not make it fire again. Deleting a timer from its lane cancels it, also when its fire is already queued: onTimer does not run. A registration or delete from another lane, or from a punctuator, follows the cross-lane rule below. In a sub-topology behind a re-key, a due timer's onTimer waits until the latest commit boundary has reached its lane, as re-keyed records do: it never runs in an older commit than the writes made before it became due. Named timers are deduplicated by (key, name) instead.
A timer can be for any key, not only the record being processed — any key the timer key serde can serialize, from process, onTimer or a punctuator (including init). A timer for key y, armed while processing key x, fires on y's lane, serially with y's records. This goes beyond Flink, which scopes a timer to the current key, and Kafka Streams, which has no keyed timers. Order across lanes is not guaranteed: registering or deleting y's timer from x's lane or a punctuator is not ordered against what y's own lane does to y's timers at the same moment. The timer set stays consistent, but which write wins depends on timing: a write from a later commit wins over one from an earlier commit, even when the earlier one was made later — the same way while the application runs and after a restart. So a re-registration from another lane that belongs to a later commit than a fire already waiting, or running, on y's lane fires the timer once more, after it; one from the same commit or an earlier one does not. A delete from another lane follows the same order: from the same commit as a waiting fire or an earlier one it cancels the fire, but from a later commit it does not — the fire runs in its own commit, and the delete comes after it. This holds on every timer backend. The TopologyTestDriver has no commits to order by and applies a write from another lane when it happens — a re-registration that meets a waiting fire is taken into it (one fire), and a delete cancels it. A replay after a failure may place the writes in different commits: a re-registration from another lane may then fire the timer once more or collapse into one fire, and a delete from another lane may cancel a fire it came after the first time, or the reverse. When order matters, use the Flink-style pattern: forward a record re-keyed to y and arm the timer while processing it, so each key has one writer. The same holds for moving a named timer: a move from the key's own lane is exact, one from another lane or a punctuator is ordered by commit.
A timer needs a key: every register* and delete* call with a null key throws IllegalArgumentException. As in Flink, a keyed timer belongs to a key, and records with a null key are spread across lanes rather than tied to one.
A timer's timestamp can be any Long except Long.MIN_VALUE: registerEventTimeTimer and registerProcessingTimeTimer throw IllegalArgumentException on it. A negative event-time timer is valid and fires once the watermark reaches it.
A timer that is already due when you register it — at or before the current watermark (event time) or wall-clock time (processing time) — never fires inside the call that registers it. It fires at the next collection pass, which runs on every watermark advance and on the wall-clock tick (processing.wall-clock-advance-interval-ms, 10 ms by default): a few milliseconds after the registering callback returns, with no new input needed. The TopologyTestDriver fires it at its next advance, whether or not that advance moves a clock.
A timer can be re-registered from its own onTimer — through context.timerService() — in event time and in processing time alike, for a timeout that keeps rolling forward. Re-arming the very same (key, timestamp) from its onTimer creates a new timer, already due, that fires once more at the next collection pass; it is not deduplicated against the fire that armed it.
If onTimer throws, or the timer's key cannot be deserialized when it fires, the failure goes to the processing exception handler with a synthetic __timer source topic. The handler gets the timer's key, or the raw key bytes when the key itself could not be decoded, and the timer's headers, so a dead letter carries the arming record's trace headers. On CONTINUE, its dead letters are written and the timer is dropped. On FAIL — or if the handler itself throws, or its dead letters cannot be sent — the instance stops and the timer is kept, so it fires again after the restart: on every timer backend with its changelog and state restoration on (the defaults), and otherwise on HYBRID or PERSISTENT from local state; MEMORY then loses it. A failing punctuator is routed the same way, with a synthetic __punctuator source topic and no headers.
Under at_least_once, the state changes onTimer makes are durable only together with its output, as a punctuator's are (see Punctuators): after a hard crash the timer fires again over the state it first saw, so it can repeat its output but does not lose it.
The onTimer callback receives a TimerContext, which provides forward(...), getStateStore(name) (read/write), timerService() to register follow-up timers, the scheduled timestamp(), timeDomain() to distinguish event-time from processing-time timers, headers() — the timer's headers — and timerName() — the name a named timer was registered with, null for an unnamed one. A FixedKeyProcessor's FixedKeyTimerContext has the same headers() and timerName().
Timer headers
A timer carries the headers of the record that armed it. Every registration reads the record context of the call it is made in:
| Registered from | The timer's headers |
|---|---|
process(record) | The record's headers as they reach the processor — including any an upstream node set with forward(record.withHeaders(...)) |
onTimer(...) | The firing timer's own headers — a re-armed timer keeps them |
init() or a punctuator | None (a punctuator's own context starts empty) |
Pass headers of your own with the overloads that take a Headers argument — registerEventTimeTimer(key, timestamp, headers), registerProcessingTimeTimer(key, timestamp, headers) and the two named forms. They are upserted by key: each key they contain replaces the captured headers of that key, then they are appended in order. A counter you re-arm from onTimer therefore stays one header. Neither the record's headers nor yours are changed; the timer stores a copy.
A timer keeps one header per key — of a key the record carries more than once, the last. A serializer in Confluent's header schema-id mode appends its schema id to the headers it is handed, and a forward from onTimer hands it the timer's own headers (as Kafka Streams hands it the record's), so a timer that forwards and then re-arms itself would otherwise gain one schema id per fire. The engine's stoatflow.epoch stamp (emit.mode = on-barrier) is never stored, from the record or from your headers: it describes the commit the arming record belongs to, not the one the timer fires in.
When the timer fires, context.headers() in onTimer — on the TimerContext, the FixedKeyTimerContext, and a context you kept from init() — returns them as a fresh, mutable copy. forward(key, value) — with or without a timestamp or child name — and a FixedKeyProcessor's forward(value) carry them: they build their record on the call's record context, so a change you make before the forward is on the forwarded record. A forward that reaches a sink hands those headers to the producer, which makes them read-only as it sends — as it does process()'s, and as in Kafka Streams — so to change them for a later forward, change a copy (RecordHeaders(context.headers())). forward(record) sends the record as you built it, with its own headers, as in process() and Kafka Streams: a new Record(key, value, timestamp) has none, so forward one to emit without the timer's headers, or build it with context.headers() (or record.withHeaders(context.headers())) to carry them.
// Async-io style retry: the attempt count rides on the retry timer.
override fun onTimer(timestamp: Long, key: String, context: TimerContext<String, String>) {
val attempt = (context.headers().lastHeader("x-retry")?.value()?.let { ByteBuffer.wrap(it).int } ?: 0) + 1
if (callService(key)) return
context.timerService<String>().registerProcessingTimeTimer(
key,
"retry", // a named timer: one pending retry per key
context.currentSystemTimeMs() + backoffMs(attempt),
RecordHeaders().add("x-retry", ByteBuffer.allocate(4).putInt(attempt).array()), // upserted: one x-retry
)
}
Headers never touch the timer's key. The key is serialized on its own, so a timer has one identity whatever headers address it: a delete, deleteTimer, deleteAllNamedTimers or namedTimer from a punctuator or another key's record finds the timer process() registered, and none of them take headers. What the key serializer writes into its own headers — Confluent's header schema-id mode writes the key's schema id there — is stored with the timer and decodes its key when it fires; it is not among the timer's headers. A key serde in header schema-id mode can therefore key timers (see Serdes).
Cost. The headers are stored with the timer — in the timer store and its changelog — and in memory, with each timer the backend holds in its timer cache, when the headers are 1 KiB or less (larger ones are read back from the store when the timer fires). Tracing headers on every record mean tracing headers on every timer; a timer whose stored headers exceed 1 KiB logs a WARN (at most once a minute per processor). Registering the same timer again from each record — a window-end timer, say — writes it each time the records' headers differ (a per-record trace id), to the timer store and its changelog, where a registration that changes nothing writes nothing. To arm timers without the record's headers, register them where the context has none, or forward the record to the timer's processor with record.withHeaders(RecordHeaders()).
Named timers
A named timer is one timer per (key, name), across both clocks. Registering the name again replaces its pending timer — at another time, or in the other domain — so a deadline that moves with every event needs neither a "last seen" value in your own state nor an exact delete of the previous deadline. Named and unnamed timers are separate: a named and an unnamed timer for the same key and timestamp both fire.
registerEventTimeTimer(key, name, timestamp)andregisterProcessingTimeTimer(key, name, timestamp)create the timer or move it. A registration in the other domain moves it there:registerProcessingTimeTimer(cart, "abandon", t)turns an event-time"abandon"into a processing-time one. Registering it again at the same time changes nothing.deleteTimer(key, name)deletes it, whatever its domain — also when it is due and its fire is waiting on the key's lane, which then does not run.deleteAllNamedTimers(key)deletes every name of the key in both domains; the key's unnamed timers stay.namedTimer(key, name)returns the pendingNamedTimer—timeNotion()(TimeNotion.EVENT_TIMEorPROCESSING_TIME) andtimestamp()— ornull.- A registration carries headers like an unnamed one. Registering a name again at its pending time, in its domain, with the same headers changes nothing; with other headers the timer keeps its time and takes the new headers — so a debounce re-armed from records whose headers differ (a per-record trace id, say) writes the timer each time. A move takes the moving call's headers, never the old timer's.
- In
onTimer,context.timerName()says which named timer fired. The fired timer is removed whenonTimerreturns normally (a failure follows the handler's verdict, below), and inside that call it already reads as absent:namedTimer(key, name)returnsnullfor it — through the timer context'stimerService()and the processor context's alike — until the call registers it again. Soif (timers.namedTimer(key, name) == null) timers.registerEventTimeTimer(key, name, next)re-arms it. Re-arming at the same time fires it once more.
The rules for every timer apply: the domain must be declared in declaredTimerTypes() (registering an undeclared one throws IllegalStateException; deletes and the query are not gated), the key must not be null, and the timestamp can be any Long but Long.MIN_VALUE. A name is non-empty and at most 255 bytes in UTF-8, otherwise IllegalArgumentException; a null name from Java throws NullPointerException (Kotlin's parameter check — the convention Kafka Streams' own APIs follow for a null argument). A failure in onTimer goes to the processing exception handler as for an unnamed timer: CONTINUE removes the named timer, while FAIL keeps it — name and time — so it fires again after the restart, as an unnamed timer does.
The key comes first. With K = String the key and the name are both strings, and swapping them compiles: registerEventTimeTimer("abandon", cartId, ts) arms a timer for the key "abandon", named after the cart.
Moves from another lane. A move from the key's own lane — process() for that key, or the key's own onTimer — is exact, also when the old deadline is already due and its fire is waiting. A move or delete from another lane, or from a punctuator, is ordered by commit, not by event time, as for unnamed timers: within about one commit interval around the old deadline, the old deadline may fire as well (the new one still fires, later), and two moves of one name from different lanes within about one commit interval are ordered by their commits. A deadline moved minutes, hours or days before it is due is replaced exactly. When exactness matters inside that window, forward a record re-keyed to the key and move its named timers while processing it, so each key's timers have one writer.
A session timeout is the typical case: every event moves the session's one timer, so only the latest deadline fires and onTimer needs no "is this still the current deadline?" check.
import io.stoatflow.core.processor.ContextualProcessor
import io.stoatflow.core.processor.ProcessorContext
import io.stoatflow.core.processor.Record
import io.stoatflow.core.processor.TimeNotion
import io.stoatflow.core.processor.TimerContext
import io.stoatflow.core.processor.TimerService
import io.stoatflow.core.state.KeyValueStore
private const val SESSION_TIMEOUT_MS = 30_000L
class SessionProcessor :
ContextualProcessor<String, Event, String, Session>() {
private lateinit var sessions: KeyValueStore<String, Session>
private lateinit var timers: TimerService<String>
override fun declaredTimerTypes(): Set<TimeNotion> = setOf(TimeNotion.EVENT_TIME)
override fun init(context: ProcessorContext<String, Session>) {
super.init(context)
sessions = context.getStateStore("sessions")
timers = context.timerService()
}
override fun process(record: Record<String, Event>) {
val session = sessions.get(record.key)?.add(record.value)
?: Session.start(record.value)
sessions.put(record.key, session)
// one timeout per session: each event moves it (an out-of-order event does not move it back)
timers.registerEventTimeTimer(record.key, "timeout", session.lastActivity + SESSION_TIMEOUT_MS)
}
override fun onTimer(
timestamp: Long,
key: String,
context: TimerContext<String, Session>,
) {
// only the latest deadline fires: no lastActivity check needed
val store = context.getStateStore<KeyValueStore<String, Session>>("sessions")
val session = store.get(key) ?: return
context.forward(key, session.complete())
store.put(key, null) // tombstone; we already read the value above
}
}
import io.stoatflow.core.processor.ContextualProcessor;
import io.stoatflow.core.processor.ProcessorContext;
import io.stoatflow.core.processor.Record;
import io.stoatflow.core.processor.TimeNotion;
import io.stoatflow.core.processor.TimerContext;
import io.stoatflow.core.processor.TimerService;
import io.stoatflow.core.state.KeyValueStore;
import org.jspecify.annotations.NonNull;
import java.util.Set;
public class SessionProcessor
extends ContextualProcessor<String, Event, String, Session> {
private static final long SESSION_TIMEOUT_MS = 30_000L;
private KeyValueStore<String, Session> sessions;
private TimerService<String> timers;
@Override
public @NonNull Set<TimeNotion> declaredTimerTypes() {
return Set.of(TimeNotion.EVENT_TIME);
}
@Override
public void init(ProcessorContext<String, Session> context) {
super.init(context);
this.sessions = context.getStateStore("sessions");
this.timers = context.timerService();
}
@Override
public void process(Record<String, Event> record) {
Session existing = sessions.get(record.key());
Session session = existing == null
? Session.start(record.value())
: existing.add(record.value());
sessions.put(record.key(), session);
// one timeout per session: each event moves it (an out-of-order event does not move it back)
timers.registerEventTimeTimer(record.key(), "timeout", session.lastActivity() + SESSION_TIMEOUT_MS);
}
@Override
public void onTimer(long timestamp, String key, @NonNull TimerContext<String, Session> context) {
// only the latest deadline fires: no lastActivity check needed
KeyValueStore<String, Session> store = context.getStateStore("sessions");
Session session = store.get(key);
if (session == null) {
return;
}
context.forward(key, session.complete());
store.put(key, null); // tombstone; we already read the value above
}
}
HYBRID and PERSISTENT keep them in RocksDB, and MEMORY restores them from the timer changelog at every start (with changelog-enabled and state restoration on, the defaults).Timers and event time
Every timer backend holds at most max-cached-timers timers per time domain (100 000 by default) in its timer cache, loaded in timestamp order — the ones it can fire. Pick the backend with stoatflow.timer-backend in YAML, or in code with StreamsConfig.Builder.timerBackend(...) (Core config → Timer backend). Where the rest live differs:
HYBRID(the default) andPERSISTENTkeep every timer in RocksDB. Whatever the lag, the number of timers due at one instant, a restart or an HA failover, memory stays at about twicemax-cached-timersentries — each a timer's key, plus its headers when they are 1 KiB or less.MEMORYkeeps every timer in memory, with no bound on their total, and loads no RocksDB — a topology whose only state is its timers runs without the native library. It restores its timers from the timer changelog at every start and at every in-place restart (a replaced thread, a poison replay, an HA demotion), so start-up time grows with the number of pending timers: preferHYBRIDfor large timer volumes. Withchangelog-enabled: false, or with state restoration off, its timers are lost at every restart, as an in-memory state store's are.
A timer not loaded into the cache cannot fire, so event time waits for it. When more event-time timers are due than fit in memory — a catch-up after downtime, a burst of timers at one timestamp — the effective watermark stops at the first timer not yet loaded. The loaded timers fire, the next ones load, and the watermark moves on. Meanwhile the records that would move event time past that timer wait in their partition buffers; records that would not keep flowing.
What this means for your code:
- No timer fires late, and nothing passes an unfired timer. No window closes, no record is judged late and no stream-time punctuator fires past an event-time timer that has not fired yet. Results equal those of an application with every timer in memory.
currentWatermarkMs()follows the held watermark, inprocess()and inonTimeralike: during a hold it reads the frontier, not the newest record's time.- Catch-up is stepwise. A backlog of
Bdue timers drains in aboutB / max-cached-timerssteps, each waiting foronTimerto finish the loaded ones — so a slowonTimer(one doing I/O, say) holds every event-time effect in the application, not only its own processor's. Raisemax-cached-timerswhen holds are frequent; thestoatflow.watermark.timer.heldgauge shows them, and a hold longer than a minute logs a WARN naming the loaded count, the bound and the frontier. - Processing-time timers have their own budget, so a processing-time backlog never holds event time.
- A custom
WatermarkGeneratoris never held back record by record — StoatFlow cannot predict its watermark — so only the watermark itself waits for the timers. No window closes past an unfired timer, but which late records a window drops can then differ from a run with every timer in memory.
A timer registered for a time already loaded joins the cache even above the bound, so registrations due within the load horizon (cache-horizon-ms for HYBRID, one second for PERSISTENT) can go past it; stoatflow.timer.cache.overshoot.max reports by how much. The TopologyTestDriver keeps every timer in memory and never holds the watermark.
A complete processor app
Putting it together: register the store, attach the processor with process(...), and run it on the runtime. The processor reads its store by name, forwards results, and the runtime wires up Kafka, the HTTP/metrics server, and graceful shutdown — see Your first app for the runtime entry point.
import io.stoatflow.core.state.Stores
import io.stoatflow.core.topology.Consumed
import io.stoatflow.core.topology.Named
import io.stoatflow.core.topology.Produced
import io.stoatflow.core.topology.StreamsBuilder
import io.stoatflow.runtime.StoatFlowRuntime
import org.apache.kafka.common.serialization.Serdes
fun main() {
val runtime = StoatFlowRuntime.fromConfig(
topologyBuilder = { buildTopology(it) },
configure = {
streamsConfigOverrides {
defaultKeySerde(Serdes.String())
defaultValueSerde(Serdes.String())
}
},
)
runtime.start()
runtime.awaitTermination()
}
private fun buildTopology(builder: StreamsBuilder) {
builder.addStateStore(
Stores.keyValueStoreBuilder(
Stores.persistentKeyValueStore("counts"),
Serdes.String(),
Serdes.Long(),
),
)
builder
.stream<String, String>("input", Consumed.`as`("source"))
.process({ CountingProcessor() }, Named.`as`("count"), "counts")
.to("counts-out", Produced.`as`<String, Long>("sink").withValueSerde(Serdes.Long()))
}
import io.stoatflow.core.state.Stores;
import io.stoatflow.core.topology.Consumed;
import io.stoatflow.core.topology.KStream;
import io.stoatflow.core.topology.Named;
import io.stoatflow.core.topology.Produced;
import io.stoatflow.core.topology.StreamsBuilder;
import io.stoatflow.runtime.StoatFlowRuntime;
import org.apache.kafka.common.serialization.Serdes;
public class Main {
public static void main(String[] args) {
var runtime = StoatFlowRuntime.fromConfig(
Main::buildTopology,
builder -> builder.streamsConfigOverrides(cfg -> {
cfg.defaultKeySerde(Serdes.String());
cfg.defaultValueSerde(Serdes.String());
})
);
runtime.start();
runtime.awaitTermination();
}
private static void buildTopology(StreamsBuilder builder) {
builder.addStateStore(Stores.keyValueStoreBuilder(
Stores.persistentKeyValueStore("counts"),
Serdes.String(),
Serdes.Long()));
KStream<String, String> input = builder.stream("input", Consumed.as("source"));
input
.process(CountingProcessor::new, Named.as("count"), "counts")
.to("counts-out", Produced.<String, Long>as("sink").withValueSerde(Serdes.Long()));
}
}
Processor wrapping (KIP-1112)
Sometimes you want one piece of behaviour applied to every processor in a topology — structured logging,
per-node metrics, a tracing span, uniform error handling, or swapping in a mock while testing. Rather than
editing every operator, configure a single ProcessorWrapper and StoatFlow applies it to every node — DSL
operators and Processor-API nodes alike — when the topology is compiled.
public final class LoggingWrapper implements ProcessorWrapper {
@Override
public <KIn, VIn, KOut, VOut> WrappedProcessorSupplier<KIn, VIn, KOut, VOut> wrapProcessorSupplier(
String processorName, ProcessorSupplier<KIn, VIn, KOut, VOut> supplier) {
return ProcessorWrapper.asWrapped(() -> {
Processor<KIn, VIn, KOut, VOut> inner = supplier.get();
return new Processor<>() {
@Override public void init(ProcessorContext<KOut, VOut> ctx) { inner.init(ctx); }
@Override public void process(Record<KIn, VIn> record) {
log.debug("{} <- {}", processorName, record.key());
inner.process(record);
}
@Override public void close() { inner.close(); }
};
});
}
@Override
public <KIn, VIn, VOut> WrappedFixedKeyProcessorSupplier<KIn, VIn, VOut> wrapFixedKeyProcessorSupplier(
String processorName, FixedKeyProcessorSupplier<KIn, VIn, VOut> supplier) {
return ProcessorWrapper.asWrappedFixedKey(supplier); // pass through
}
}
Point processor.wrapper.class at it:
processor.wrapper.class=com.acme.LoggingWrapper
The Kafka Streams route — passing a TopologyConfig to the builder — works and is the portable choice:
StreamsConfig config = StreamsConfig.fromProperties(props).build();
StreamsBuilder builder = new StreamsBuilder(new TopologyConfig(config));
Unlike Kafka Streams, StoatFlow also honours a wrapper set on the runtime config alone — it compiles the
topology inside start(), so KS's "too late, silently ignored" case doesn't exist here. If both are set to
different classes, the TopologyConfig one wins and a warning names the shadowed value.
If your wrapper needs settings, implement configure(Map) — it is called once at instantiation with the config
map, exactly as in Kafka Streams.
ProcessorWrapper.asWrapped(supplier) / asWrappedFixedKey(supplier) to pass a node through unchanged.
The default is NoOpProcessorWrapper, which does exactly that; with no wrapper configured the runtime record
path is untouched, so there is no cost to leaving the key unset.What a wrapper can and cannot do
Your Processor-API nodes (process / processValues / addProcessor) | Full control — decorate, replace the processor outright, or add state stores via the wrapped supplier's stores() |
DSL operator nodes (mapValues, filter, aggregations, joins, windows) | The record path: observe, decorate, or short-circuit. A decorator may rewrite the record before delegating — record.withTimestamp(...) / withHeaders(...) flow through to the operator, downstream nodes, and the sink. Replacing a DSL operator with a different processor is rejected at build time — they are type-erased internally |
init / watermark / suppress-flush callbacks on DSL nodes | Not routed through the wrapper (they aren't on the Processor surface) — in practice: window/session close emissions, the barrier suppress flush, and FK-join/suppress restore. A DSL-node decorator's context also cannot schedule() or use timerService(). Your timers are unaffected: keyed timers only exist on Processor-API nodes, which are wrapped in full |
| Global / read-only store update processors | Not wrapped — Kafka Streams doesn't wrap them either |
Three behavioural notes worth knowing before you write a stateful wrapper:
get()runs once per lane, not once per task — plus once for a Processor API node's punctuator instance. Each decorator instance is pinned to one lane and single- threaded, so per-instance fields are race-free — but they only see that lane's records. Put cross-lane counters on the wrapper object itself (and make it thread-safe), or push to an external sink.- A decorator must not forward from
process()of a node that emits only when a window closes: anOnWindowClosewindowed, sliding, session or cogroup aggregation, anuntilWindowClosessuppress, or anuntilTimeLimitsuppress with a strict buffer. Such a node emits only when the watermark closes a window or a suppression ends, and StoatFlow places the nodes after it on that basis; a forward from itsprocess()would reach them from a record lane. The first such forward fails with an error naming the wrapper and the node, which is not offered to the processing exception handler, and the application stops. Kafka Streams allows it (KSC-161). Decorating, observing or timing these nodes is fine, and so is forwarding from the wrapped operator itself. - Node names are StoatFlow's —
mapValues-0,process-0, or whatever you passed toNamed.as(...)— not Kafka Streams'KSTREAM-MAPVALUES-0000000001. A ported wrapper that switches on node-name patterns compiles cleanly and then matches nothing.
Next steps
- State stores — store types, serdes, changelog backing, and querying.
- Scheduled sources — topology-level periodic record generation (
StreamsBuilder.scheduled()). - Testing — drive processors, advance time, and trigger punctuators with the in-memory test driver.
- State and thread safety — the execution model behind per-key serialized processing and timer callbacks.
- How StoatFlow differs from Kafka Streams — Processor API parity and the StoatFlow-specific extensions (timers, time notions).
Deep dives
- Your timer fired on another threadDeep Dive — StoatFlow runs a key's timers on the same lane as its records, so onTimer never races process() for that key. The lane is chosen by hashing key bytes — and a timer key is serialised again, by its own serde. Behind a Processor API source declared with only a deserializer, that serde was the default, and when the default wrote different bytes than the topic, a key's timer fired on another lane, concurrently with its records, without a word. How it happens, why Kafka Streams never meets it, and how StoatFlow now refuses to run a proven split.
Joins
Combine two streams or tables on a shared key — stream-stream windowed joins, stream-table enrichment lookups, table-table joins, foreign-key joins, and co-grouping.
Scheduled sources
Generate records on an interval or a cron schedule with StreamsBuilder.scheduled() — a topology source that emits without consuming from Kafka.