Mohamed Motaz
02/27/2026, 10:37 PMKeyedProcessFunction to use async state v2 and I have a question about execution order since it’s not super explicit in the documentation here . My function does a read-modify-write.
It receives a session from the sessionState, then reads the state, and merges the current session with the new session and then updates the state.
sessionState.asyncValue()
.thenApply(current -> {
if (current == null) current = new FullSessionMessage();
return current;
})
.thenAccept(current -> {
current.merge(value);
sessionState.asyncUpdate(current);
});
The docs state: “The execution order of user code entry flatMap for same-key elements are invoked strictly in order of element arrival.”
Assume I have 2 elements, A and B, with A coming before B. Does this mean element A’s full chain (callbacks + asyncUpdate) finishes before element B’s KeyedProcessFunction starts, when they share the same key? Cross-key parallelism & concurrency is obviously expected and welcome, but same-key would cause issues. Thank you!
tldr; Basically worried about the classic race condition in async V2 state in keyed states in the same stream:
1. A reads state X
2. B also reads state X (before A wrote back)
3. A writes X+A
4. B writes X+B → A’s merge is lost
Expected result is X + A + B