You are viewing a plain text version of this content. The canonical link for it is here.
Posted to jira@kafka.apache.org by "nicktelford (via GitHub)" <gi...@apache.org> on 2026/06/19 15:09:44 UTC
[PR] KAFKA-20494: Transactional InMemoryKeyValueStore [kafka]
nicktelford opened a new pull request, #22626:
URL: https://github.com/apache/kafka/pull/22626
Adds a transactional implementation of `InMemoryKeyValueStore`, building
on the transaction-buffer framework (KAFKA-20490).
`InMemoryTransactionBuffer` implements `TransactionBuffer` for in-memory
stores backed by a `ConcurrentNavigableMap`: writes are staged in the
inherited `pendingWrites` map and flushed to the backing map on commit,
while base iterators are created directly from the backing map. Reads
merge staged writes over the base iterator so a transaction observes its
own uncommitted writes.
`InMemoryKeyValueStore` routes reads and writes through the buffer only
when transactional state stores are enabled, leaving the
non-transactional path unchanged. Restore bypasses the buffer to write
directly to the backing map, and the store reports its pending
write-buffer size via `approximateNumUncommittedBytes()`, feeding the
uncommitted-bytes limit added in KAFKA-20491.
Unlike RocksDB stores, in-memory stores never use `deleteRange`, so the
buffer carries no range-tombstone handling.
This is part of
[KIP-892](https://cwiki.apache.org/confluence/display/KAFKA/KIP-892%3A+Transactional+Semantics+for+State+Stores).
--
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.
To unsubscribe, e-mail: jira-unsubscribe@kafka.apache.org
For queries about this service, please contact Infrastructure at:
users@infra.apache.org
Re: [PR] KAFKA-20494: Transactional InMemoryKeyValueStore [kafka]
Posted by "bbejeck (via GitHub)" <gi...@apache.org>.
bbejeck commented on code in PR #22626:
URL: https://github.com/apache/kafka/pull/22626#discussion_r3454566968
##########
streams/src/main/java/org/apache/kafka/streams/state/internals/InMemoryKeyValueStore.java:
##########
@@ -231,13 +267,34 @@ public long approximateNumEntries() {
return map.size();
}
+ @Override
+ public long approximateNumUncommittedBytes() {
+ if (transactionBuffer != null) {
+ return transactionBuffer.approximateNumUncommittedBytes();
+ }
+ return 0;
+ }
+
@Override
public void commit(final Map<TopicPartition, Long> changelogOffsets) {
- // do-nothing since it is in-memory
+ commitStagedWrites();
+ }
+
+ void commitStagedWrites() {
+ if (transactionBuffer != null) {
+ transactionBuffer.commit();
+ }
+ }
+
+ void rollbackStagedWrites() {
+ if (transactionBuffer != null) {
+ transactionBuffer.rollback();
+ }
}
@Override
public void close() {
+ rollbackStagedWrites();
Review Comment:
Just a thought for discussion later - on `close` is there any chance we would be stranding any writes - but I don't think so. I believe if the KS producer had records to send and `KafkaStreams.close()` is executed and a commit would eventually follow clearing the staged writes - speaking of the happy-path only
--
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.
To unsubscribe, e-mail: jira-unsubscribe@kafka.apache.org
For queries about this service, please contact Infrastructure at:
users@infra.apache.org
Re: [PR] KAFKA-20494: Transactional InMemoryKeyValueStore [kafka]
Posted by "bbejeck (via GitHub)" <gi...@apache.org>.
bbejeck merged PR #22626:
URL: https://github.com/apache/kafka/pull/22626
--
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.
To unsubscribe, e-mail: jira-unsubscribe@kafka.apache.org
For queries about this service, please contact Infrastructure at:
users@infra.apache.org
Re: [PR] KAFKA-20494: Transactional InMemoryKeyValueStore [kafka]
Posted by "bbejeck (via GitHub)" <gi...@apache.org>.
bbejeck commented on PR #22626:
URL: https://github.com/apache/kafka/pull/22626#issuecomment-4772778724
Merged #22626 into trunk
--
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.
To unsubscribe, e-mail: jira-unsubscribe@kafka.apache.org
For queries about this service, please contact Infrastructure at:
users@infra.apache.org
Re: [PR] KAFKA-20494: Transactional InMemoryKeyValueStore [kafka]
Posted by "nicktelford (via GitHub)" <gi...@apache.org>.
nicktelford commented on code in PR #22626:
URL: https://github.com/apache/kafka/pull/22626#discussion_r3452097625
##########
streams/src/main/java/org/apache/kafka/streams/state/internals/InMemoryTransactionBuffer.java:
##########
@@ -0,0 +1,151 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one or more
+ * contributor license agreements. See the NOTICE file distributed with
+ * this work for additional information regarding copyright ownership.
+ * The ASF licenses this file to You under the Apache License, Version 2.0
+ * (the "License"); you may not use this file except in compliance with
+ * the License. You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+package org.apache.kafka.streams.state.internals;
+
+import org.apache.kafka.common.utils.Bytes;
+import org.apache.kafka.streams.KeyValue;
+
+import java.util.Map;
+import java.util.NavigableMap;
+import java.util.NoSuchElementException;
+import java.util.Optional;
+
+/**
+ * A {@link TransactionBuffer} implementation for {@link InMemoryKeyValueStore}.
+ * <p>
+ * Uses Option A from the design: accepts that non-owner scan iterators read values
+ * live from the base map (no snapshot isolation on values). This avoids the O(N)
+ * commit cost and double memory usage of the committed snapshot approach (Option D).
+ * Staged-key reads are always correct (staging takes precedence). The gap only affects
+ * base-store values that are concurrently being committed during iteration — a narrow
+ * window in practice.
+ */
+class InMemoryTransactionBuffer extends AbstractTransactionBuffer<Bytes> {
+
+ private final NavigableMap<Bytes, byte[]> baseMap;
+
+ InMemoryTransactionBuffer(final NavigableMap<Bytes, byte[]> baseMap) {
+ this.baseMap = baseMap;
+ }
+
+ @Override
+ int estimateKeySize(final Bytes key) {
+ return key.get().length;
+ }
+
+ @Override
+ void stageToBackend(final Bytes key, final byte[] value) {
+ // no-op — staging map is sufficient; no write-batch concept for in-memory
+ }
+
+ @Override
+ ManagedKeyValueIterator<Bytes, byte[]> newBaseIterator(final Bytes from, final Bytes to) {
+ return newBaseIterator(from, to, true, true);
+ }
+
+ @Override
+ ManagedKeyValueIterator<Bytes, byte[]> newBaseIterator(final Bytes from, final Bytes to,
+ final boolean forward, final boolean toInclusive) {
+ final NavigableMap<Bytes, byte[]> view;
+ if (from != null && to != null) {
+ view = baseMap.subMap(from, true, to, toInclusive);
+ } else if (from != null) {
+ view = baseMap.tailMap(from, true);
+ } else if (to != null) {
+ view = baseMap.headMap(to, toInclusive);
+ } else {
+ view = baseMap;
+ }
+ return new SnapshotMapIterator(forward ? view : view.descendingMap());
Review Comment:
Good catch. I've addressed this as per my above reply.
##########
streams/src/main/java/org/apache/kafka/streams/state/internals/InMemoryKeyValueStore.java:
##########
@@ -85,7 +86,12 @@ public void init(final StateStoreContext stateStoreContext,
(RecordBatchingStateRestoreCallback) records -> {
synchronized (position) {
for (final ConsumerRecord<byte[], byte[]> record : records) {
- put(Bytes.wrap(record.key()), record.value());
+ final Bytes key = Bytes.wrap(record.key());
Review Comment:
Oh yes, well spotted. Fixed!
--
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.
To unsubscribe, e-mail: jira-unsubscribe@kafka.apache.org
For queries about this service, please contact Infrastructure at:
users@infra.apache.org
Re: [PR] KAFKA-20494: Transactional InMemoryKeyValueStore [kafka]
Posted by "nicktelford (via GitHub)" <gi...@apache.org>.
nicktelford commented on code in PR #22626:
URL: https://github.com/apache/kafka/pull/22626#discussion_r3452095134
##########
streams/src/main/java/org/apache/kafka/streams/state/internals/InMemoryKeyValueStore.java:
##########
Review Comment:
Good catch, but I've decided to go in a different direction: taking a snapshot of the range being iterated when an IQ thread constructs an iterator.
This retains the `TreeMap` for the non-transactional path (and it's clear from KAFKA-8802 that ConcurrentSkipListMap's performance was an issue in the past), and keeps non-transactional behaviour completely untouched.
It's important to remember that the existing, non-transactional path unconditionally takes a snapshot of the range of keys in the map being iterated, but then produces the live value for each key during iteration. This prevents ConcurrentModificationException, and ensures that a StreamThread that modifies keys it iterates sees its own writes.
When transactions are enabled, we have a few different paths:
- `StreamThread`: the owner thread iterates the `TreeMap` *directly*. No snapshot needed. We can do this because writes target the transaction buffer, not the store, so it sees its own writes via the `AbstractTransactionBuffer#StagedMergeIterator`. It's not possible for the store itself to be updated during iteration because you can't `commit` from within a `Processor`.
- IQ threads: takes a snapshot of the range being iterated (including values!) to provide snapshot isolation; this snapshot is done under the same `readLock` that's used to snapshot the transaction buffer. Compared with the non-transactional snapshots, the performance impact should be similar, including memory usage (because it's only the value references being copied, not the values themselves). The main caveat is that `byte[]` values are effectively "pinned" on the heap until the `Iterator` is closed, preventing GC.
One concern I have: as I discovered in KAFKA-8802, performance of `ConcurrentSkipListMap` was deemed a regression vs. `TreeMap`, but our `AbstractTransactionBuffer#pendingWrites` uses a `ConcurrentSkipListMap`, which will likely impact the performance of in-memory stores. I'm going to investigate if there's a way to optimize this a bit better, but I think that's orthogonal to this PR.
--
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.
To unsubscribe, e-mail: jira-unsubscribe@kafka.apache.org
For queries about this service, please contact Infrastructure at:
users@infra.apache.org
Re: [PR] KAFKA-20494: Transactional InMemoryKeyValueStore [kafka]
Posted by "nicktelford (via GitHub)" <gi...@apache.org>.
nicktelford commented on PR #22626:
URL: https://github.com/apache/kafka/pull/22626#issuecomment-4752749920
@bbejeck
--
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.
To unsubscribe, e-mail: jira-unsubscribe@kafka.apache.org
For queries about this service, please contact Infrastructure at:
users@infra.apache.org
Re: [PR] KAFKA-20494: Transactional InMemoryKeyValueStore [kafka]
Posted by "nicktelford (via GitHub)" <gi...@apache.org>.
nicktelford commented on code in PR #22626:
URL: https://github.com/apache/kafka/pull/22626#discussion_r3452847297
##########
streams/src/main/java/org/apache/kafka/streams/state/internals/InMemoryKeyValueStore.java:
##########
Review Comment:
Regarding my last paragraph on `ConcurrentSkipListMap` performance: I have an alternative strategy to address that, but I'm going to raise that in a separate PR, because it involves changes in both `InMemoryTransactionBuffer` and `RocksDBTransactionBuffer`. So I'll wait until these 2 PRs land first.
--
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.
To unsubscribe, e-mail: jira-unsubscribe@kafka.apache.org
For queries about this service, please contact Infrastructure at:
users@infra.apache.org
Re: [PR] KAFKA-20494: Transactional InMemoryKeyValueStore [kafka]
Posted by "bbejeck (via GitHub)" <gi...@apache.org>.
bbejeck commented on code in PR #22626:
URL: https://github.com/apache/kafka/pull/22626#discussion_r3444567603
##########
streams/src/main/java/org/apache/kafka/streams/state/internals/InMemoryTransactionBuffer.java:
##########
@@ -0,0 +1,151 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one or more
+ * contributor license agreements. See the NOTICE file distributed with
+ * this work for additional information regarding copyright ownership.
+ * The ASF licenses this file to You under the Apache License, Version 2.0
+ * (the "License"); you may not use this file except in compliance with
+ * the License. You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+package org.apache.kafka.streams.state.internals;
+
+import org.apache.kafka.common.utils.Bytes;
+import org.apache.kafka.streams.KeyValue;
+
+import java.util.Map;
+import java.util.NavigableMap;
+import java.util.NoSuchElementException;
+import java.util.Optional;
+
+/**
+ * A {@link TransactionBuffer} implementation for {@link InMemoryKeyValueStore}.
+ * <p>
+ * Uses Option A from the design: accepts that non-owner scan iterators read values
+ * live from the base map (no snapshot isolation on values). This avoids the O(N)
+ * commit cost and double memory usage of the committed snapshot approach (Option D).
+ * Staged-key reads are always correct (staging takes precedence). The gap only affects
+ * base-store values that are concurrently being committed during iteration — a narrow
+ * window in practice.
+ */
+class InMemoryTransactionBuffer extends AbstractTransactionBuffer<Bytes> {
+
+ private final NavigableMap<Bytes, byte[]> baseMap;
+
+ InMemoryTransactionBuffer(final NavigableMap<Bytes, byte[]> baseMap) {
+ this.baseMap = baseMap;
+ }
+
+ @Override
+ int estimateKeySize(final Bytes key) {
+ return key.get().length;
+ }
+
+ @Override
+ void stageToBackend(final Bytes key, final byte[] value) {
+ // no-op — staging map is sufficient; no write-batch concept for in-memory
+ }
+
+ @Override
+ ManagedKeyValueIterator<Bytes, byte[]> newBaseIterator(final Bytes from, final Bytes to) {
+ return newBaseIterator(from, to, true, true);
+ }
+
+ @Override
+ ManagedKeyValueIterator<Bytes, byte[]> newBaseIterator(final Bytes from, final Bytes to,
+ final boolean forward, final boolean toInclusive) {
+ final NavigableMap<Bytes, byte[]> view;
+ if (from != null && to != null) {
+ view = baseMap.subMap(from, true, to, toInclusive);
+ } else if (from != null) {
+ view = baseMap.tailMap(from, true);
+ } else if (to != null) {
+ view = baseMap.headMap(to, toInclusive);
+ } else {
+ view = baseMap;
+ }
+ return new SnapshotMapIterator(forward ? view : view.descendingMap());
Review Comment:
This can result in a `ConcurrentModificationException` - `view` is a live view over `baseMap` solved by https://github.com/apache/kafka/pull/22626/changes#diff-dd735c4d6edacf3ee7378bfdcc2d5e89499e6846913fbbd9ee147b54235fc50bR57
##########
streams/src/main/java/org/apache/kafka/streams/state/internals/InMemoryKeyValueStore.java:
##########
@@ -85,7 +86,12 @@ public void init(final StateStoreContext stateStoreContext,
(RecordBatchingStateRestoreCallback) records -> {
synchronized (position) {
for (final ConsumerRecord<byte[], byte[]> record : records) {
- put(Bytes.wrap(record.key()), record.value());
+ final Bytes key = Bytes.wrap(record.key());
Review Comment:
this inlines `map.put/get` vs calling `put` -> `putInternal` which in turn called ` StoreQueryUtils.updatePosition(position, context)` so `position` is now dropped, so maybe go back to `put`?
##########
streams/src/main/java/org/apache/kafka/streams/state/internals/InMemoryKeyValueStore.java:
##########
Review Comment:
I think this should get updated to `ConcurrentSkipListMap` I'll explain in an another comment.
--
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.
To unsubscribe, e-mail: jira-unsubscribe@kafka.apache.org
For queries about this service, please contact Infrastructure at:
users@infra.apache.org
Re: [PR] KAFKA-20494: Transactional InMemoryKeyValueStore [kafka]
Posted by "bbejeck (via GitHub)" <gi...@apache.org>.
bbejeck commented on code in PR #22626:
URL: https://github.com/apache/kafka/pull/22626#discussion_r3444580490
##########
streams/src/main/java/org/apache/kafka/streams/state/internals/InMemoryKeyValueStore.java:
##########
@@ -85,7 +86,12 @@ public void init(final StateStoreContext stateStoreContext,
(RecordBatchingStateRestoreCallback) records -> {
synchronized (position) {
for (final ConsumerRecord<byte[], byte[]> record : records) {
- put(Bytes.wrap(record.key()), record.value());
+ final Bytes key = Bytes.wrap(record.key());
Review Comment:
this inlines `map.put/get` vs calling `put` -> `putInternal` which in turn called ` StoreQueryUtils.updatePosition(position, context)` so `position` is now dropped, so maybe use `putInternal` which would preserve the `position`
--
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.
To unsubscribe, e-mail: jira-unsubscribe@kafka.apache.org
For queries about this service, please contact Infrastructure at:
users@infra.apache.org