Serdes and Avro
A serde (serializer + deserializer) tells StoatFlow how to turn your keys and values into bytes on the wire and back. Every source, sink, grouping, and state store needs a key serde and a value serde. StoatFlow uses the standard Kafka org.apache.kafka.common.serialization.Serde<T> type, so any serde you already use with Kafka Streams works unchanged.
This page covers where serdes are resolved (defaults vs. per-operator), the built-in serdes, writing your own, serializing windowed keys, and the Avro + Schema Registry integration.
How serdes are resolved
StoatFlow resolves the serde for each operator in a fixed order:
- An explicit serde on the operator's config object —
Consumed,Produced,Grouped, orMaterialized. This always wins. - The default key/value serde configured on the runtime — used whenever the operator config leaves the serde unset (
null).
If neither is set, serialization for that operator fails at startup. The default serde is the convenient base case when most of your topology uses the same types (e.g. String keys); reach for per-operator serdes wherever an individual topic or store differs.
A few operators look further before falling back to the default:
mergekeeps the serdes the two sides' declarations allow: a side with no serde takes the other side's, the same serde carries through, and only two different declared serdes leave the merged stream on the default. Kafka Streams always drops both sides' serdes onmerge.- An aggregation with both
GroupedandMaterializedserdes stores with theMaterializedones — windowed and session aggregations included, as in Kafka Streams. groupByKey(Grouped)with no serdes (for exampleGrouped.as(name)) keeps the key and value serdes the stream already declared, asgroupByKey()does. Kafka Streams uses the defaults here.- A stream-stream join without
StreamJoinedserdes stores each side with its own stream's value serde, and keys both stores by the key serde the two sides agree on (a side with none takes the other side's). Kafka Streams uses the defaults. - A
KTablefilter,mapValuesortransformValueswith aMaterializedthat sets no serde writes its store with the parent table's serdes. Kafka Streams writes it with the defaults. cogroup(...).aggregate(...)— plain, windowed or session — takes theMaterializedkey serde, else the key serde its legs agree on (from theirGroupedor upstream declarations), else the default. Kafka Streams goes straight from theMaterializedto the default.table(topic, Consumed, Materialized)follows Kafka Streams: theConsumedserdes (or the default, where one is unset) serve both the source and the store, and theMaterializedserdes are ignored. Onlytable(topic, Materialized), with noConsumed, deserializes the source with theMaterializedserdes.
These rules matter only where the configured default would write different bytes than the serde declared upstream. Where the default cannot serialize the value at all, Kafka Streams fails and StoatFlow works; where both serdes write the same bytes, nothing differs. Where both work but write different bytes — say a JSON default over Consumed.with(Serdes.String(), …) keys — StoatFlow's stores, changelogs and outputs follow the upstream serde. For a port from Kafka Streams that carries its state over, set topology.validation.declared-serde-over-default=error (default off): the build then lists every such site, and declaring the serde you want there keeps the bytes Kafka Streams wrote.
Boundary key serdes (lane assignment)
There is one more place StoatFlow needs a key serde that Kafka Streams does not: a sub-topology boundary. Where a re-keyed record reaches an operator that needs the new key's lane affinity — an aggregation, a join, toTable(), or an explicit repartition() — records are re-dispatched to key-affinity lanes, and the new key is serialized to bytes for the lane hash, in memory, with no repartition topic.
Since 1.0.0 there are fewer of these boundaries than there used to be: a re-key alone no longer opens one, and neither does a Processor API node (process() / processValues()), matching Kafka Streams — see Lanes and parallelism and, for what that costs after a many-to-one re-key, Key affinity after a re-key. A topology whose re-key feeds only stateless or Processor API operators now needs no boundary key serde at all.
StoatFlow resolves the boundary key serde from whatever your topology declares for that key, searching upstream first and then downstream, always within the region where the key keeps its type:
- Upstream: the nearest ancestor declaration — the source's
Consumedkey serde, or arepartition(Repartitioned.withKeySerde(...))the key flowed through. - Downstream: the nearest declaration for the new key — an explicit
Repartitioned, aGrouped, a sink'sProduced, or an aggregation'sMaterialized.withKeySerde(...). - Last resort: if you never configured a default key serde, the nearest declaration from before the re-key — inherited across it. See Last-resort inheritance below.
- The configured default key serde otherwise.
Two rules keep steps 1 and 2 predictable:
- A declaration only counts for the key it actually describes. After a re-key, upstream serdes no longer apply; a
Groupedserde describes the grouping key, never the key arriving at thegroupBy; and two adjacent re-keys find nothing in either step — no declaration sits between them to describe the intermediate key, so that boundary falls through to the last two steps. - If two different serde classes are reachable for the same key, StoatFlow declines to guess.
Joins and cogroups hash every input with one serde. A table-table join, a stream-stream join and a cogroup decide or
aggregate each key on one lane, so a key must hash alike from every input. StoatFlow resolves every input of such an
operator to one key serde — a table-table join's from its left table, a stream-stream join's or a cogroup's from the
serde its inputs agree on; when the inputs declare different serdes it falls back to the default for all of them, which
the unresolved-boundary-key-serde rule reports. (Kafka Streams has no such check: keys whose bytes differ there land in
different partitions and the join silently misses them.) The stateful-merge-key-serdes rule (default error, warn
or off to opt out) refuses a topology where an input of such an operator would hash a key with a different serde. That
happens where an input comes straight from a source topic, which keeps the key serde its Consumed declares: a
stream-stream join or a cogroup of sources whose key serdes differ, once it runs in a sub-topology of its own — for
example because one of the sources also feeds another operator. The start then fails, naming the operator and both
serdes; declare the same key serde on every input. Inputs agree when their key serializers are of one class:
Serdes.serdeFrom(new StringSerializer(), …) and Serdes.String() do. Two serializer classes count as different even
when they write the same bytes; for such a pair, set the rule to warn.
Last-resort inheritance
Steps 1 and 2 are strict: they only ever return a serde that was declared for this key. When both come up empty, StoatFlow walks upstream a second time, through the re-key, and takes the nearest declaration from the previous key region.
That is safe because a boundary key serde is used for lane hashing only. The bytes feed a MurmurHash3 lane assignment and nothing else — they are never stored, never sent to Kafka, and never deserialized back. So the serde only has to encode your key deterministically; it does not have to be the right serde for it in any other sense. (A Schema Registry serde still registers the key's schema, under the boundary's own subject — see Subjects StoatFlow registers.) (This is exactly why the same leniency is not applied to keyed timers, whose key bytes are persisted and read back.)
It works when the re-key preserves the declared key type — the common case for a re-key that reshapes a String id. When the type changes, the inherited serde cannot encode the new key and the boundary still fails on the first record, with the inherited serde named in the message.
A warning always names the edge ('parent' → 'child') and the inherited serde class, because the topology now starts where it previously died — and if the types happen not to line up, you want to know before the first record. It is raised when the topology compiles, so you see it from your own tests as well as at startup. Silence it, or make it fatal, with the topology.validation rule inherited-boundary-key-serde.
When nothing resolves
If you have configured a default key serde, that default wins outright — step 3 never runs, because your explicit choice is the answer for this key. This is the escape hatch for a genuinely byte[]-keyed boundary: set default.key.serde to Serdes.ByteArray() explicitly and inheritance stays out of the way. Without that, StoatFlow cannot tell a deliberate byte-array key from an unconfigured default — they are the same value. It is the presence of the setting that decides, not its value, so any route that sets one closes inheritance: the StreamsConfig constructor, .copy(), the builder, fromMap / fromProperties, or the YAML key. Having answered, you are also out of the unresolved-boundary-key-serde rule below at every severity — including error — because there is nothing left to report.
A Processor API source added with a key deserializer — Topology.addSource(name, keyDeserializer, …), or the matching addGlobalStore / addReadOnlyStateStore overloads — declares no key serde: a deserializer can read a key but not write one. A boundary whose key comes from such a source — directly, or through other Processor API processors — has nothing to resolve and lands here too, reported by the unresolved-boundary-key-serde rule like any other undeclared boundary. Declare the serde instead with 🆕 addSource(name, Consumed.with(keySerde, valueSerde), topics) (the store overloads have the same Consumed twin); it maps exactly as StreamsBuilder.stream(topic, consumed) does. Setting a default.key.serde that serializes the deserializer's type (for a StringDeserializer source, Serdes.String()) also works. The same choice keys such a processor's timers (see Timer key serde).
Otherwise the boundary falls back to the raw ByteArray default and a warning names the edge when the topology compiles. If your keys are not raw bytes, that boundary will fail on the first record with LaneKeySerializationException — declare a serde at any of the places above, or set default.key.serde. The warning stays a warning by default because a genuinely byte-array-keyed boundary is valid and indistinguishable at build time; the unresolved-boundary-key-serde rule below lets you make it fatal anyway.
Validation severities
Both boundary conditions above are rules in the topology.validation family, and each can be turned off or promoted to a build failure:
stoatflow:
topology:
validation:
unresolved-boundary-key-serde: error # off | warn | error (default: warn)
inherited-boundary-key-serde: off
error fails topology compilation with a single message naming every offending boundary at once, so a topology with several takes one build-run cycle to clean up rather than several.
All three severities are decided at that one point, when the topology compiles — not at engine start — so TopologyTestDriver reaches them as well. At the default warn that means the per-boundary line appears in your own test suite's output, with no broker; at error the driver refuses to build; at off neither happens, in tests or in production.
See the configuration reference for the full rule list.
Default serdes
Set the application-wide default key and value serdes through streamsConfigOverrides when you build the runtime. These apply to every operator that doesn't specify its own serde.
import io.stoatflow.runtime.StoatFlowRuntime
import org.apache.kafka.common.serialization.Serdes
val runtime = StoatFlowRuntime.fromConfig(
topologyBuilder = { buildTopology(it) },
configure = {
streamsConfigOverrides {
defaultKeySerde(Serdes.String())
defaultValueSerde(Serdes.String())
}
},
)
import io.stoatflow.runtime.StoatFlowRuntime;
import org.apache.kafka.common.serialization.Serdes;
var runtime = StoatFlowRuntime.fromConfig(
Main::buildTopology,
builder -> builder.streamsConfigOverrides(cfg -> {
cfg.defaultKeySerde(Serdes.String());
cfg.defaultValueSerde(Serdes.String());
})
);
You can also set the defaults declaratively in application.yaml using the fully-qualified serde class name. The class must have a no-argument constructor; for parameterised serdes (e.g. Avro), set the instance in code instead.
stoatflow:
default-key-serde: org.apache.kafka.common.serialization.Serdes$StringSerde
# default-value-serde: ...
The runtime StreamsConfig is the only place defaults come from. As in Kafka Streams, a StreamsBuilder(TopologyConfig(config)) carries no serde defaults into the topology: the defaults the running application is configured with always apply.
streamsConfigOverrides take precedence over the YAML class-name form — use the YAML keys for serdes with a no-arg constructor, and the code form whenever you need to pass a configured serde instance.Per-operator serdes
Each operator that touches Kafka or a state store takes a config object that carries serdes. All four follow the same shape: a with(keySerde, valueSerde) factory plus chained .withKeySerde(...) / .withValueSerde(...) builders.
| Config | Operators | Factory | Builders |
|---|---|---|---|
Consumed<K,V> | stream, table, globalTable | Consumed.with(k, v) | .withKeySerde, .withValueSerde |
Produced<K,V> | to | Produced.with(k, v) | .withKeySerde, .withValueSerde |
Repartitioned<K,V> | repartition | Repartitioned.with(k, v) | .withKeySerde, .withValueSerde |
Grouped<K,V> | groupByKey, groupBy | Grouped.with(k, v) | .withKeySerde, .withValueSerde |
Materialized<K,V,S> | count, reduce, aggregate, joins | Materialized.with(k, v) | .withKeySerde, .withValueSerde |
Source and sink
Consumed controls how a source topic is deserialized; Produced controls how a sink topic is serialized.
import io.stoatflow.core.topology.Consumed
import io.stoatflow.core.topology.Produced
import org.apache.kafka.common.serialization.Serdes
// Source: String keys, Long values
val numbers = builder.stream(
"input",
Consumed.with(Serdes.String(), Serdes.Long()),
)
// Sink: String keys, Long values
numbers.to(
"output",
Produced.with(Serdes.String(), Serdes.Long()),
)
import io.stoatflow.core.topology.Consumed;
import io.stoatflow.core.topology.KStream;
import io.stoatflow.core.topology.Produced;
import org.apache.kafka.common.serialization.Serdes;
// Source: String keys, Long values
KStream<String, Long> numbers = builder.stream(
"input",
Consumed.with(Serdes.String(), Serdes.Long()));
// Sink: String keys, Long values
numbers.to(
"output",
Produced.with(Serdes.String(), Serdes.Long()));
When you only need to override one of the two serdes (the other coming from the defaults), use the named factory or the builder form. Consumed.as(...) / Produced.as(...) set only the operator name and leave both serdes on the defaults — that is the pattern the first app uses, where the sink overrides just the value serde because counts are Long:
.to(
"word-counts",
Produced.`as`<String, Long>("sink")
.withKeySerde(Serdes.String())
.withValueSerde(Serdes.Long()),
)
.to(
"word-counts",
Produced.<String, Long>as("sink")
.withKeySerde(Serdes.String())
.withValueSerde(Serdes.Long()));
Grouping and aggregation
A groupBy / groupByKey re-serializes records as it re-keys them, so the grouping serdes must match the post-grouping key and value types. Grouped carries those serdes; if a downstream aggregation materializes state, Materialized carries the store's serdes (which may differ — the aggregate value type is often not the input value type).
import io.stoatflow.core.state.StateStore
import io.stoatflow.core.topology.Grouped
import io.stoatflow.core.topology.Materialized
import org.apache.kafka.common.serialization.Serdes
stream
.groupBy(
{ _, value -> value.category },
Grouped.with(Serdes.String(), productSerde),
)
.count(
Materialized.`as`<String, Long, StateStore>("counts-by-category")
.withKeySerde(Serdes.String())
.withValueSerde(Serdes.Long()),
)
import io.stoatflow.core.state.StateStore;
import io.stoatflow.core.topology.Grouped;
import io.stoatflow.core.topology.Materialized;
import org.apache.kafka.common.serialization.Serdes;
stream
.groupBy(
(key, value) -> value.category(),
Grouped.with(Serdes.String(), productSerde))
.count(
Materialized.<String, Long, StateStore>as("counts-by-category")
.withKeySerde(Serdes.String())
.withValueSerde(Serdes.Long()));
Materialized carries far more than serdes (store type, changelog, caching, retention). See State stores for the rest; this page covers only its serde configuration.
Built-in serdes
The standard Kafka serdes cover the common primitive and byte types. They're factory methods on org.apache.kafka.common.serialization.Serdes:
| Type | Factory |
|---|---|
String | Serdes.String() |
Long | Serdes.Long() |
Integer | Serdes.Integer() |
Short | Serdes.Short() |
Float | Serdes.Float() |
Double | Serdes.Double() |
byte[] | Serdes.ByteArray() |
ByteBuffer | Serdes.ByteBuffer() |
Bytes | Serdes.Bytes() |
UUID | Serdes.UUID() |
Void | Serdes.Void() |
These are plain Kafka client types — the same ones you'd reach for in a Kafka Streams app. The full list is whatever your kafka-clients version exposes on Serdes.
Custom serdes
For your own value types — JSON, Protobuf, a hand-rolled binary format — implement org.apache.kafka.common.serialization.Serde<T>, or build one from a Serializer<T> / Deserializer<T> pair with Serdes.serdeFrom(serializer, deserializer).
A deserializer or serializer instance you pass to the Processor API (Topology.addSource(name, keyDeserializer, …), addSink(name, topic, keySerializer, …)) is called by several threads at once — the dispatcher, parallel deserialization and every lane — so it must be thread-safe. (Kafka Streams also shares one instance across its tasks, but with its default single stream thread never calls it concurrently, so code that relied on that needs a thread-safe (de)serializer here.) A serde — one you declare (Consumed, Produced, …) or the configured default — is asked for its deserializer() / serializer() on every call, so a serde that hands out a fresh instance per call stays isolated.
A serde is just a serializer and a deserializer bundled together:
import com.fasterxml.jackson.databind.ObjectMapper
import org.apache.kafka.common.serialization.Deserializer
import org.apache.kafka.common.serialization.Serde
import org.apache.kafka.common.serialization.Serdes
import org.apache.kafka.common.serialization.Serializer
data class Order(val id: String, val amount: Double)
class JsonOrderSerializer : Serializer<Order> {
private val mapper = ObjectMapper()
override fun serialize(topic: String?, data: Order?): ByteArray? =
data?.let { mapper.writeValueAsBytes(it) }
}
class JsonOrderDeserializer : Deserializer<Order> {
private val mapper = ObjectMapper()
override fun deserialize(topic: String?, data: ByteArray?): Order? =
data?.let { mapper.readValue(it, Order::class.java) }
}
val orderSerde: Serde<Order> =
Serdes.serdeFrom(JsonOrderSerializer(), JsonOrderDeserializer())
import com.fasterxml.jackson.databind.ObjectMapper;
import org.apache.kafka.common.serialization.Deserializer;
import org.apache.kafka.common.serialization.Serde;
import org.apache.kafka.common.serialization.Serdes;
import org.apache.kafka.common.serialization.Serializer;
record Order(String id, double amount) {}
class JsonOrderSerializer implements Serializer<Order> {
private final ObjectMapper mapper = new ObjectMapper();
@Override
public byte[] serialize(String topic, Order data) {
if (data == null) return null;
try {
return mapper.writeValueAsBytes(data);
} catch (Exception e) {
throw new RuntimeException(e);
}
}
}
class JsonOrderDeserializer implements Deserializer<Order> {
private final ObjectMapper mapper = new ObjectMapper();
@Override
public Order deserialize(String topic, byte[] data) {
if (data == null) return null;
try {
return mapper.readValue(data, Order.class);
} catch (Exception e) {
throw new RuntimeException(e);
}
}
}
Serde<Order> orderSerde =
Serdes.serdeFrom(new JsonOrderSerializer(), new JsonOrderDeserializer());
Use orderSerde exactly like a built-in — pass it to Consumed.with(...), Produced.with(...), Materialized.withValueSerde(...), or set it as the default value serde.
null cleanly — return null for null input. KTable changelogs and tombstones rely on null values to signal deletion, so a serde that throws on null will break stateful operators.Header-aware deserializers
At the source, StoatFlow calls the record-headers overload deserialize(topic, headers, data), as Kafka Streams does. A deserializer that reads a header, such as a schema id or a content-type, gets the incoming record's headers. Deserializer's default 3-argument method delegates to the 2-argument one, so a deserializer that ignores headers behaves exactly as before. TopologyTestDriver passes the piped record's headers the same way.
Header-aware serializers
The other direction matches Kafka Streams too. Wherever StoatFlow serializes a key or value for a record, it calls serialize(topic, headers, data) with the record's headers: the sink key and value, the key bytes that pick a lane on an in-memory repartition(), and queryMetadataForKey (KIP-1321). A serializer that ignores headers produces the same bytes either way. As in Kafka Streams, the headers are the record's own object, shared by every sink the record reaches: a header a serializer adds — a schema id, say — is also visible to a sibling sink and to processors later in the same traversal. Each produced record keeps its own copy from the moment it is sent. A record that crosses an in-memory sub-topology boundary — a repartition(), or a re-key followed by an operator that needs key affinity (an aggregation, a join or toTable()) — also gets its own copy there, as a Kafka Streams repartition topic gives each record its own headers: what a processor or sink adds after the boundary is not visible before it, nor on the other branches. The one exception follows Kafka Streams too: the boundary key is serialized with the record's own headers before the copy, so a header a key serializer writes there (Confluent's header mode, for example) is visible upstream as well.
At sinks, this is what makes Confluent's schema id in headers (key.schema.id.serializer / value.schema.id.serializer = HeaderSchemaIdSerializer) work: that serializer writes the id into a header and needs the headers to write to.
Header-aware key serializers are meant for sinks. Before an in-memory repartition() — and the grouped aggregation or join it feeds — avoid a key serde whose bytes depend on the contents of the headers: the same key arriving with different headers would be spread over several lanes of one shared store, and its timers may run on another. A serde that only adds a header, like Confluent's header mode, keeps one lane per key. A header-mode serde can key or value a state store only if the store keeps headers — a store that persists them, or the internal stores of joins — see serdes in state stores. It can key a Processor API timer: the schema id it writes is stored with the timer and decodes the key when the timer fires (timer headers). A key serde whose bytes depend on header contents cannot: a timer key is serialized with no headers of its own, so its timers would pick another lane than its records, and the lane-affinity guard refuses them.
Serdes in state stores
State stores call the header-aware serde methods too, but what they pass depends on whether the store keeps headers.
A store that does not persist headers — the default for every store — calls its serdes with fresh, empty headers: on every write, read, range bound and iterator step, whatever record is being processed. Reads therefore decode the same way everywhere: in process(), in a punctuator, on a window-close emission, in an interactive query, during restore. A serde that ignores headers, and Confluent's default id-in-payload mode, write exactly the bytes they always did.
A serde that keeps its schema id in the headers (Confluent's header mode) cannot work on such a store: the store discards the headers, so nothing it reads back carries the id. Its reads fail — or, rarely, when the payload happens to begin with the registry's magic byte, decode against the wrong schema. The same goes for a serde that switches to header mode whenever it is handed headers, such as Apicurio's with apicurio.registry.headers.enabled: the store hands it empty headers, never null (Kafka Streams 4.3+ hands it the record's, also never null). StoatFlow logs one warning per store when a serializer writes headers the store will drop, naming the headers and the fix — materialize the store with headers, Materialized.withRecordHeaders(), or set stoatflow.dsl.store-format: HEADERS for the whole topology; for a Processor API store, build it with a Stores.…WithHeadersBuilder; or configure the serde to keep the schema id in the payload. Any header triggers the warning; one the deserializer does not need — a tracing id, a JSON serde's type id — is harmless, and the application keeps running either way.
A store that persists headers — Materialized.withRecordHeaders(), stoatflow.dsl.store-format: HEADERS, or a Stores.*WithHeaders builder — passes them the way Kafka Streams 4.4 does. A write serializes the key and the value with the headers being stored, and the store keeps what the serializers add, so a header-mode schema id is stored with the value and read back with it. A lookup — get, a range bound, prefixScan — serializes the key with the headers of the record being processed (none in an interactive query). An iterator decodes each key and value with that entry's stored headers. With a header-mode serde this works for keys as well as values.
A header a store serializer appends replaces an older one of the same name in the headers being stored. So a write that carries an entry's stored headers forward — a compute that copies the old value, say — keeps one schema id per header name instead of adding another on every update, and an input record's schema id gives way to the one a header-mode store serializer writes. Kafka Streams keeps every copy.
__value_schema_id when the input was written in header mode. A store serde in prefix mode (Confluent's default) adds no header to replace it, so its deserializer, which reads the header first, decodes the stored bytes against the input topic's schema and fails. Kafka Streams behaves the same. Configure the store's serdes in header mode too, or drop the schema-id headers before the stateful step (for example in a processValues that removes them from the record's headers).The internal store of a foreign-key join keeps the headers its serializers write, separately for the foreign key and the primary key, and decodes with them — on a restart and on a hot standby. A serde that keeps its schema id in the headers works there with no configuration, even when both keys write __key_schema_id. Kafka Streams decodes the subscription with the triggering record's headers, so such a serde fails there.
A stream-stream join — inner, left or outer, a self-join included — keeps the headers its serializers write as well. Each side's buffer stores what the key and value serializers added with the buffered value and decodes the value with them, and an interactive query on a buffer decodes its keys the same way; the buffer of a left or outer join's unmatched records does the same. So a header-mode serde works in every stream-stream join with no configuration: on every store supplier, after a restart or a restore, on a hot standby, and whatever stoatflow.dsl.store-format is. A buffer keeps only what its serializers write, never the record's own headers, so header-mode input read by a join whose serdes are in prefix mode decodes too. A join's key and value serdes must write different header names, as Confluent's (__key_schema_id, __value_schema_id) and Apicurio's do: a pair that wrote the same name would decode a buffered key in an interactive query against the value's schema id.
Kafka Streams 4.3+ buffers each side in a headers store that follows dsl.store.format. With HEADERS, or in-memory join stores, a buffer keeps the record's headers plus what the serializers add and decodes with them; under the default the RocksDB buffer drops them locally, so a header-mode serde fails at the first match. Its buffer of unmatched records decodes with the triggering record's headers whatever the format.
Store serializers always work on a copy of the headers. The record's own headers never gain a store's schema id, so a sink or a repartition topic downstream sees the record's headers plus only what its own serializer adds. Kafka Streams appends the store's schema id to the record itself.
This is a deliberate difference from Kafka Streams 4.3+, which hands a plain store's serdes the current record's headers: a value written under one record is then read back with whatever record happens to be current, which is right only when both carry the same schema id. A Kafka Streams application that uses header-mode serdes on plain stores needs headers stores when it moves to StoatFlow.
Windowed keys
Windowed aggregations (windowedBy(...).count(), session windows, sliding windows) produce keys of type Windowed<K> — the original key plus the window's start and end. To write those keys to a topic (or read them back), you need a serde that encodes the window bounds alongside the key bytes. WindowedSerdes builds those for you from the inner key serde.
Like Kafka Streams' own, these serdes hand the record's headers to the inner key serde, and their plain serialize(topic, key) / deserialize(topic, bytes) forms hand it fresh, empty headers.
The state store doesn't need one: windowed Materialized takes the plain key serde (Serdes.String()) — StoatFlow wraps it into a Serde<Windowed<K>> internally, exactly like Kafka Streams. So the windowed serde belongs on the output (Produced), not the Materialized:
import io.stoatflow.core.state.StateStore
import io.stoatflow.core.topology.Materialized
import io.stoatflow.core.topology.Produced
import io.stoatflow.core.topology.WindowedSerdes
import org.apache.kafka.common.serialization.Serdes
// Encodes Windowed<String> keys for a topic — used on the output, not the store.
val windowedKeySerde = WindowedSerdes.timeWindowedSerdeFrom(Serdes.String())
stream
.groupByKey()
.windowedBy(/* window definition */)
.count(
// Plain key serde — StoatFlow wraps it into Serde<Windowed<String>> for the result table.
Materialized.`as`<String, Long, StateStore>("windowed-counts")
.withKeySerde(Serdes.String())
.withValueSerde(Serdes.Long()),
).toStream()
.to("windowed-counts", Produced.with(windowedKeySerde, Serdes.Long()))
import io.stoatflow.core.state.StateStore;
import io.stoatflow.core.topology.Materialized;
import io.stoatflow.core.topology.Produced;
import io.stoatflow.core.topology.WindowedSerdes;
import io.stoatflow.core.topology.Windowed;
import org.apache.kafka.common.serialization.Serde;
import org.apache.kafka.common.serialization.Serdes;
// Encodes Windowed<String> keys for a topic — used on the output, not the store.
Serde<Windowed<String>> windowedKeySerde =
WindowedSerdes.timeWindowedSerdeFrom(Serdes.String());
stream
.groupByKey()
.windowedBy(/* window definition */)
.count(
// Plain key serde — StoatFlow wraps it into Serde<Windowed<String>> for the result table.
Materialized.<String, Long, StateStore>as("windowed-counts")
.withKeySerde(Serdes.String())
.withValueSerde(Serdes.Long()))
.toStream()
.to("windowed-counts", Produced.with(windowedKeySerde, Serdes.Long()));
Use sessionWindowedSerdeFrom(inner) instead for session windows. Both factories also exist as constructors — WindowedSerdes.TimeWindowedSerde(inner) and WindowedSerdes.SessionWindowedSerde(inner) — and expose the unwrapped inner serde via keySerde().
Serde<T> for the inner key (not a Class<T> as in Kafka Streams), and there is no window-size argument — the window bounds are encoded directly in the serialized bytes. See How StoatFlow differs from Kafka Streams.Avro with Schema Registry
Avro is the most common production serialization format with Kafka. StoatFlow has no Avro serde of its own — you use the Confluent SpecificAvroSerde (or the generic/reflection variants), the same one you'd use with Kafka Streams. StoatFlow's only role is to hand Schema Registry's URL to your default serdes; serdes you build yourself, you configure yourself.
Build setup
Generate Java classes from your .avsc schemas (the com.github.davidmc24.gradle.plugin.avro plugin against org.apache.avro:avro is one common choice), then depend on the Confluent serde from the Confluent Maven repository:
// build.gradle.kts
repositories {
mavenCentral()
maven("https://packages.confluent.io/maven/")
}
dependencies {
implementation("io.stoatflow:stoatflow-runtime:<stoatflow-version>")
implementation("io.confluent:kafka-streams-avro-serde:8.4.1")
}
kafka-clients:8.4.1-ccs, but StoatFlow targets the Apache kafka-clients 4.x APIs. Pin the Apache version so Confluent's -ccs build doesn't win dependency resolution:configurations.all {
resolutionStrategy.eachDependency {
if (requested.group == "org.apache.kafka" && requested.name == "kafka-clients") {
useVersion("4.4.0") // your Apache kafka-clients version
}
}
}
Pointing serdes at Schema Registry
Set the registry URL once in application.yaml. StoatFlow propagates it to the default key and value serdes at startup, so an Avro serde set as the default value serde is configured for you:
stoatflow:
application-id: news-article-publish-processor
bootstrap-servers: ${KAFKA_BOOTSTRAP_SERVERS:-localhost:9092}
schema-registry-url: ${SCHEMA_REGISTRY_URL:-http://localhost:8081}
default-key-serde: org.apache.kafka.common.serialization.Serdes$StringSerde
import io.confluent.kafka.streams.serdes.avro.SpecificAvroSerde
import io.stoatflow.runtime.StoatFlowRuntime
import org.apache.kafka.common.serialization.Serdes
val runtime = StoatFlowRuntime.fromConfig(
topologyBuilder = { buildTopology(it) },
configure = {
streamsConfigOverrides {
defaultKeySerde(Serdes.String())
// Configured automatically with schema-registry-url from YAML
defaultValueSerde(SpecificAvroSerde<Article>())
}
},
)
import io.confluent.kafka.streams.serdes.avro.SpecificAvroSerde;
import io.stoatflow.runtime.StoatFlowRuntime;
import org.apache.kafka.common.serialization.Serdes;
var runtime = StoatFlowRuntime.fromConfig(
Main::buildTopology,
builder -> builder.streamsConfigOverrides(cfg -> {
cfg.defaultKeySerde(Serdes.String());
// Configured automatically with schema-registry-url from YAML
cfg.defaultValueSerde(new SpecificAvroSerde<>());
}));
Consumed, Produced, or Materialized must be configured by hand — StoatFlow never touches it.Per-operator Avro serdes
When different topics carry different Avro types — or you want the value serde on a specific source rather than as the global default — build each serde and call configure(...) with the registry URL before handing it to the operator. The boolean second argument is isKey (true for key serdes, false for value serdes):
import io.confluent.kafka.streams.serdes.avro.SpecificAvroSerde
import org.apache.kafka.common.serialization.Serde
fun createStockTickSerde(schemaRegistryUrl: String): Serde<StockTick> {
val serde = SpecificAvroSerde<StockTick>()
serde.configure(mapOf("schema.registry.url" to schemaRegistryUrl), false)
return serde
}
import io.confluent.kafka.streams.serdes.avro.SpecificAvroSerde;
import org.apache.kafka.common.serialization.Serde;
import java.util.Map;
static Serde<StockTick> createStockTickSerde(String schemaRegistryUrl) {
SpecificAvroSerde<StockTick> serde = new SpecificAvroSerde<>();
serde.configure(Map.of("schema.registry.url", schemaRegistryUrl), false);
return serde;
}
Then wire the configured serde into the source or sink exactly like any other serde:
import io.stoatflow.core.topology.Consumed
import io.stoatflow.core.topology.Produced
import org.apache.kafka.common.serialization.Serdes
val stockTickSerde = createStockTickSerde(schemaRegistryUrl)
builder
.stream("stock-ticks", Consumed.with(Serdes.String(), stockTickSerde))
// ... processing ...
.to(
"filtered-stock-ticks",
Produced.with(Serdes.String(), stockTickSerde),
)
import io.stoatflow.core.topology.Consumed;
import io.stoatflow.core.topology.Produced;
import org.apache.kafka.common.serialization.Serde;
import org.apache.kafka.common.serialization.Serdes;
Serde<StockTick> stockTickSerde = createStockTickSerde(schemaRegistryUrl);
builder
.stream("stock-ticks", Consumed.with(Serdes.String(), stockTickSerde))
// ... processing ...
.to(
"filtered-stock-ticks",
Produced.with(Serdes.String(), stockTickSerde));
Subjects StoatFlow registers
A Schema Registry serde with Confluent's default TopicNameStrategy takes its subject from the topic it serializes for — <topic>-key or <topic>-value — and, with auto.register.schemas=true, registers the schema there. Besides your own topics, StoatFlow serializes where Kafka Streams writes an internal topic, and in a few places where it writes none. Each place serializes under a topic name of its own, prefixed with your application.id, so a subject holds one type and no two applications share it:
| Where | Topic the subject is derived from |
|---|---|
| A state store | the source topic that backs it — a globalTable(), an addGlobalStore() store, a read-only store, and a table() that reuses its source topic (the default, auto-reuse-ktable-source-topics) — else <application.id>-<store>-changelog, logged or not — as in Kafka Streams |
| An in-memory boundary | <application.id>-<name>-repartition |
| A Processor API timer | <application.id>-<processor>-timer |
| A scheduled source's key | <application.id>-<source>-scheduled |
| A foreign-key join | <application.id>-<join>-subscription-registration-topic-fk for the foreign key, …-pk for the primary key, and <application.id>-<join>-subscription-response-topic where results cross to the primary key's lane |
| A LEFT or OUTER stream-stream join's buffered records | the outer store's topic for the key and the left values, <application.id>-<outer store>-changelog-right for the right values — so an OUTER join over two value types works on a BACKWARD registry (in Kafka Streams both sides share one subject) |
A boundary is named by the key stream it carries — the re-key, repartition(), join or merge the key comes from — and by the input it feeds. <name> is the name Kafka Streams gives its repartition topic wherever you named it and Kafka Streams creates that topic — after a re-key or a repartition():
- the
Repartitionedname of therepartition()the key comes from; - the
Groupedname, ongroupByand ongroupByKey, else theMaterializedstore name of the aggregation — for a cogroup, each leg's own grouping; - the
Joinedname of a stream–table join; - the
StreamJoinedname of a stream–stream join, as<name>-leftand<name>-right; - the
Namedname of atoTable.
Otherwise, into a merge(), join or cogroup — an unnamed input, or a join side or cogroup leg whose key was not changed, which has no Kafka Streams repartition topic — <name> is that node's name, shared by those inputs; else it is the name of the node the key comes from. A Processor API node with several parents takes the second rule for each input: its parents may emit different key types, so they never share a subject. Both are names StoatFlow generates (selectKey-0), which never match Kafka Streams' generated ones.
Name your repartitions — Kafka Streams' ensure.explicit.internal.resource.naming — and a port keeps its repartition and foreign-key subjects. A store's subject follows its store name: a stream–stream join's stores are <store>-left-store, <store>-right-store and <store>-outer-store (Kafka Streams: <store>-this-join-store, <store>-other-join-store and <store>-outer-shared-join-store, with -outer- variants for outer joins), and the outer join's right values have a subject of their own. Pre-register those under StoatFlow's names.
One Grouped-named grouping feeding several aggregations shares one subject, as Kafka Streams shares the repartition topic; aggregations named only by their Materialized store name each get their own <store>-repartition subject, as in Kafka Streams. Two unrelated key streams that would get the same name — two groupings given one Grouped name, say — are refused when the topology compiles, all of them in one error, as Kafka Streams refuses a duplicate internal topic.
With auto.register.schemas=false, pre-register these subjects as you would Kafka Streams' internal ones.
A boundary key, a timer key and a scheduled-source key are hashed to pick a lane, and the same key serialized under two subjects must hash alike — a timer's key and its records' boundary key, a source topic's key and a boundary's, or the named inputs of one join or cogroup (-left and -right), or the inputs of a Processor API node with several parents. That holds when your registry gives one schema the same id in every subject, as Confluent Schema Registry and Apicurio's default content-id mode do; Kafka Streams' co-partitioning relies on the same property. The unnamed inputs of one merge, join or cogroup share a subject, so they agree on any registry. On a registry that numbers schemas per subject, a timer could land on another lane than its records, and StoatFlow refuses it (timer.lane-affinity.guard).
A timer's key must still decode when the timer fires — possibly days later, after a deployment. A key serde that changes in between — a schema change, a switch between prefix and header schema-id mode — can leave stored timers whose keys no longer decode. Each such fire goes to the processing exception handler with the raw key bytes: with CONTINUE it is written to the dead-letter queue and dropped; with FAIL the application stops and keeps the timer, which then fails again on every start until the application runs once with CONTINUE. Give a timer-owning processor whose key serde may change a CONTINUE handler and a dead-letter queue.
Keep StoatFlow's key serde in the input topic's schema-id mode. In the first sub-topology a record is laned by its key bytes as consumed, a timer by its key as StoatFlow serializes it. Input written in header mode read with a prefix-mode serde (Confluent's default) — or the reverse, or a key schema whose id drifted — gives the two different bytes, so most of those timers would run on another lane than their records: the lane-affinity guard refuses them (timer.lane-affinity.guard). Behind an in-memory boundary both are serialized by StoatFlow and agree.
A store a source topic backs serializes under that topic's subject, so it reads the schema your producer registered, and the key bytes it restores from the topic equal the ones it writes itself. With auto.register.schemas=true and a store schema that differs from the producer's, the application registers a version under the producer's subject — your registry's compatibility rules decide whether it is accepted.
Under sub-topology-split: eager a boundary opens right after each re-key, so with an operator between the re-key and a named grouping or join (a filter, say) the boundary is named by the re-key, not by your Grouped or Joined name. Pre-register the subjects of the split mode you run. A boundary out of a windowed or session cogroup — whose key is Windowed<K>, not the K of its inputs — is named <application.id>-<cogroup>-windowed-repartition.
Testing Avro topologies
For unit tests against the in-memory test driver, configure your Avro serdes against Confluent's mock Schema Registry (mock://...) instead of a live one — it keeps schemas in-process, so no broker or registry is needed:
import io.confluent.kafka.serializers.AbstractKafkaSchemaSerDeConfig
import io.confluent.kafka.streams.serdes.avro.SpecificAvroSerde
fun stockTickSerde(): SpecificAvroSerde<StockTick> {
val serde = SpecificAvroSerde<StockTick>()
serde.configure(
mapOf(AbstractKafkaSchemaSerDeConfig.SCHEMA_REGISTRY_URL_CONFIG to "mock://schema-registry"),
false,
)
return serde
}
import io.confluent.kafka.serializers.AbstractKafkaSchemaSerDeConfig;
import io.confluent.kafka.streams.serdes.avro.SpecificAvroSerde;
import java.util.Map;
static SpecificAvroSerde<StockTick> stockTickSerde() {
SpecificAvroSerde<StockTick> serde = new SpecificAvroSerde<>();
serde.configure(
Map.of(AbstractKafkaSchemaSerDeConfig.SCHEMA_REGISTRY_URL_CONFIG, "mock://schema-registry"),
false);
return serde;
}
When schema-registry-url is configured, the runtime also exposes a Schema Registry health check on /health/ready that auto-enables itself — no extra wiring required.
Next steps
- State stores — the rest of
Materialized: store types, changelog, caching, retention. - Windowing — where
Windowed<K>keys come from and how windows close. - Error handling and DLQ — what happens when a record fails to deserialize.
- Testing — drive a topology end-to-end in-memory with the test driver.
Scheduled sources
Generate records on an interval or a cron schedule with StreamsBuilder.scheduled() — a topology source that emits without consuming from Kafka.
How configuration works
How the StoatFlow runtime loads configuration — application.yaml on the classpath, overlay files, -D system properties, environment variables and secrets from files — and the layered precedence order.