Is it safe to assume that KGroupedTable#aggregate subtractor is called before adder?

The documentation re KGroupedTable#aggregate
at https://kafka.apache.org/26/documentation/streams/developer-guide/dsl-api.html#streams-developer-guide-dsl-aggregating states:

When subsequent non-null values are received for a key (e.g., UPDATE), then (1) the subtractor is called with the old value as stored in the table and (2) the adder is called with the new value of the input record that was just received. The order of execution for the subtractor and adder is not defined.

The phrase “the order of execution for the subtractor and adder is not defined” seems to warn against making any assumptions re their order of execution.

Viewing the source I find that the order of execution does seem to be defined:

And (cross-post alert) I filed a question here about the Kafka Music Example:

which also seems to rely on the current behavior (subtractor is called first, then adder). Or am I missing something? Would that Kafka Music Example work correctly even if adder were called first, then subtractor? (I don’t see how it would; but perhaps I’m missing something)

I’m curious to hear from fellow users of KGroupedTable#aggregate. Do you rely on an explicit order (subtractor first, etc.)?

You should not rely on the order. It’s of course hard-coded (so you could exploit it), but the order may change between releases, and thus, if you rely on it and upgrade to a newer version of Kafka Streams you code might break.

As similar question was asked on StackOverflow, and you can find a more extended answer with more background there: apache kafka - Clarify "the order of execution for the subtractor and adder is not defined" - Stack Overflow

2 Likes