Status
Current state: Under Discussion
Discussion thread: TBD
JIRA: KAFKA-8770
Please keep the discussion on the mailing list rather than commenting on the wiki (wiki discussions get unwieldy fast).
Motivation
Currently, in Kafka Streams, we support two modes of processing:
- emit on update
- emit on window close
The first one has been present for the majority of Kafka Stream's inception, when results are forwarded regardless . The second option has been added by implementing the so-called "suppression" operator which collects results throughout the window, and extending into the grace period. However, one mode of processing should've been implemented long ago, and that is emit on change. As mentioned in the JIRA, in many cases, the user would process a prior result to yield a new one, yet, this new result is forwarded to a downstream processor even though the operation performed had been effectively a no-op. This is problematic in certain circumstances where there is a high number of such operations, leading to a number of useless results being forwarded down the topology.
Therefore, with this KIP, we should consider a few things:
- We should decide whether or not emit on change should be considered Kafka's default behavior (i.e. we completely forgo the current emit on update design and migrate to emit on change).
- The second option would be to add configuration to disable emit on change, but this could potentially add undesired complexity to our current API. We will need to add this config if it has been determined by a performance benchmark that emit on change severely impacts processing throughput / latency (the main latency incurred here would probably be loading prior results, if we are going to follow through with this approach).
Implementation
Details on Core Improvement
Emit on change, in many aspects, is basically an improved version of emit on update, except the crucial improvement is that we check if the new result had been changed by a given operation. And for doing such a check, if we want to be thorough, load the previous result and compare it with the most recent one. However, this would not be doable in some cases without incurring extra performance hits. Therefore, rather than load the entire result, we should compare the previous result's hash code with the current result's hash code. Admittedly, there might be a small chance of collisions where the new result with changed values would still yield the same hash code. Yet, it could be safe to say that a good hash function would never let such a thing happen.
However, the main constraint for this approach, if we were to follow through, is that the hash code must reflect any changes in the individual component fields of the result. The key building block for this entire optimization is that the user provides a good hash code override of the given value type. Of course, this isn't always the case. After all, it isn't safe to assume that the user has provided a safe hash code function for us to take advantage of. In conclusion, we can probably come up with the following choices for the user:
- Completely disregard the hash code and load the prior result and compare it to the new one.
- Compare only the hash codes of the given objects instead.
The second option quite obviously is the recommended one. While it is uncertain if we will need to give the user the ability to choose between these two options, it would probably be necessary. In the latter option, it should be emphasized that the hash function must be chosen carefully. In many cases, Kafka already loads the prior result anyway. So for instances where we already load the previous result, we will just use that instead of the hash code (if we are to implement option 2).
Discourse on other areas
The timestamps of the results processed would also present a serious design challenge for this KIP. The requirements of emit on change would entail the following behavior:
- Let's say we receive record 1 with timestamp 0 which leads to a change in the result processed.
- Afterwards, we receive another record with timestamp 100 which would lead to a no-op.
It is required that the timestamp be 0.
To resolve this situation, the current best bet is probably to load the timestamp along with the hash code / full prior result.
Added methods
TBD. Discussion required first.
Proposed Changes
Describe the new thing you want to do in appropriate detail. This may be fairly extensive and have large subsections of its own. Or it may be a few sentences. Use judgement based on the scope of the change.
Compatibility, Deprecation, and Migration Plan
- What impact (if any) will there be on existing users?
- If we are changing behavior how will we phase out the older behavior?
- If we need special migration tools, describe them here.
- When will we remove the existing behavior?
Rejected Alternatives
If there are alternative ways of accomplishing the same thing, what were they? The purpose of this section is to motivate why the design is the way it is and not some other way.