Definition
- KTable represents a continuously updated table where each key has exactly one current value, derived from the latest record for that key in the source topic.
- Changelog semantics means each record is an upsert: if the key exists, the value is updated; if the value is null, the key is deleted (tombstone).
- State store is the local storage (RocksDB by default) where KTable keeps the latest value per key; it is backed by a changelog topic in Kafka for fault-tolerant recovery.
- Materialized is the configuration object that lets you name the state store, set serdes, and control caching and logging for the KTable.
- builder.table() reads a topic as a KTable directly; groupByKey().aggregate() produces a KTable from a KStream by accumulating state.
Think of the account balance ledger at National Bank of India. The ledger has one row per account. When Rajesh's balance changes from 50,000 to 80,000, the bank does not add a new row. It updates the existing row for SAV2001. If an account is closed, the bank deletes that row. That is exactly how KTable works: one value per key, updated in place, deleted with a null. The KStream is the transaction log (every deposit and withdrawal). The KTable is the balance sheet (current balance per account).
Understanding the Concepts
What is KTable?
KTable is an abstraction in Kafka Streams that represents the current state of data from a Kafka topic, keyed by the record key. For each key, KTable holds only the latest value. When a new record arrives with an existing key, the old value is replaced.
You create a KTable by calling builder.table("topic-name").
package com.bank.streams;
import org.apache.kafka.streams.StreamsBuilder;
import org.apache.kafka.streams.Topology;
import org.apache.kafka.streams.kstream.KTable;
import org.apache.kafka.streams.kstream.Materialized;
public class KTableBasics {
public static void main(String[] args) {
StreamsBuilder builder = new StreamsBuilder();
// Create a KTable from account-balances topic
// Each key (account ID) has one current value (balance)
KTable<String, String> balances = builder.table(
"account-balances",
Materialized.as("balance-store"));
// Convert to stream to see changes as they happen
balances.toStream()
.peek((accountId, balance) ->
System.out.println("Balance update: " + accountId + " -> " + balance))
.to("balance-updates");
Topology topology = builder.build();
System.out.println("KTable topology:");
System.out.println(topology.describe());
}
}
Expected output:
KTable topology:
Topologies:
Sub-topology: 0
Source: KSTREAM-SOURCE-0000000000 (topics: [account-balances])
--> KTABLE-SOURCE-0000000001
Processor: KTABLE-SOURCE-0000000001 (stores: [balance-store])
--> KTABLE-TOSTREAM-0000000002
<-- KSTREAM-SOURCE-0000000000
Processor: KTABLE-TOSTREAM-0000000002 (stores: [])
--> KSTREAM-PEEK-0000000003
<-- KTABLE-SOURCE-0000000001
Processor: KSTREAM-PEEK-0000000003 (stores: [])
--> KSTREAM-SINK-0000000004
<-- KTABLE-TOSTREAM-0000000002
Sink: KSTREAM-SINK-0000000004 (topic: balance-updates)
<-- KSTREAM-PEEK-0000000003
Notice the state store balance-store attached to the KTable processor node.
How does KTable handle updates and deletes?
When a new record arrives with a key that already exists, KTable replaces the old value. When a record arrives with a null value, KTable deletes that key (tombstone).
package com.bank.streams;
import org.apache.kafka.streams.StreamsBuilder;
import org.apache.kafka.streams.StreamsConfig;
import org.apache.kafka.streams.TopologyTestDriver;
import org.apache.kafka.streams.TestInputTopic;
import org.apache.kafka.streams.TestOutputTopic;
import org.apache.kafka.streams.kstream.KTable;
import org.apache.kafka.streams.kstream.Materialized;
import org.apache.kafka.streams.state.KeyValueStore;
import org.apache.kafka.streams.state.ValueAndTimestamp;
import org.apache.kafka.common.serialization.Serdes;
import org.apache.kafka.common.serialization.StringSerializer;
import org.apache.kafka.common.serialization.StringDeserializer;
import java.util.Properties;
public class KTableUpdateDeleteDemo {
public static void main(String[] args) {
StreamsBuilder builder = new StreamsBuilder();
KTable<String, String> balances = builder.table(
"account-balances", Materialized.as("balance-store"));
balances.toStream().to("balance-output");
Properties props = new Properties();
props.put(StreamsConfig.APPLICATION_ID_CONFIG, "test");
props.put(StreamsConfig.BOOTSTRAP_SERVERS_CONFIG, "dummy:1234");
props.put(StreamsConfig.DEFAULT_KEY_SERDE_CLASS_CONFIG,
Serdes.String().getClass());
props.put(StreamsConfig.DEFAULT_VALUE_SERDE_CLASS_CONFIG,
Serdes.String().getClass());
try (TopologyTestDriver driver =
new TopologyTestDriver(builder.build(), props)) {
TestInputTopic<String, String> input = driver.createInputTopic(
"account-balances",
new StringSerializer(), new StringSerializer());
TestOutputTopic<String, String> output = driver.createOutputTopic(
"balance-output",
new StringDeserializer(), new StringDeserializer());
// Insert: SAV2001 balance = 50000
input.pipeInput("SAV2001", "50000");
// Update: SAV2001 balance changes to 80000
input.pipeInput("SAV2001", "80000");
// Insert: SAV2002 balance = 25000
input.pipeInput("SAV2002", "25000");
// Delete: SAV2002 closed (null value = tombstone)
input.pipeInput("SAV2002", (String) null);
KeyValueStore<String, ValueAndTimestamp<String>> store =
driver.getTimestampedKeyValueStore("balance-store");
System.out.println("State store after all records:");
ValueAndTimestamp<String> sav2001 = store.get("SAV2001");
ValueAndTimestamp<String> sav2002 = store.get("SAV2002");
System.out.println(" SAV2001: " + (sav2001 != null ? sav2001.value() : "null"));
System.out.println(" SAV2002: " + (sav2002 != null ? sav2002.value() : "DELETED"));
System.out.println();
System.out.println("Output records (all changes emitted):");
while (!output.isEmpty()) {
var record = output.readKeyValue();
System.out.println(" key=" + record.key + ", value=" + record.value);
}
}
}
}
Expected output:
State store after all records:
SAV2001: 80000
SAV2002: DELETED
Output records (all changes emitted):
key=SAV2001, value=50000
key=SAV2001, value=80000
key=SAV2002, value=25000
key=SAV2002, value=null
The state store shows only the latest values: SAV2001 is 80000 (not 50000), and SAV2002 is deleted. The output stream shows all four changes because toStream() converts every state change back to a KStream event.
How does KTable differ from KStream?
| Aspect | KStream | KTable |
|---|---|---|
| Semantics | Event stream (append-only log) | Changelog (latest value per key) |
| Same key, new record | Both records kept (two events) | Old value replaced by new value |
| Null value | Passed through as a record | Tombstone: key deleted from state store |
| State store | None (stateless by default) | Local state store (RocksDB) |
| Created by | builder.stream() | builder.table() or groupByKey().aggregate() |
| Analogy | Transaction log (every deposit) | Balance sheet (current balance) |
| Use case | Events: payments, logins, clicks | State: balances, profiles, statuses |
package com.bank.streams;
public class KStreamVsKTableComparison {
public static void main(String[] args) {
System.out.println("Input: 3 records for key SAV2001");
System.out.println(" SAV2001 -> 50000");
System.out.println(" SAV2001 -> 80000");
System.out.println(" SAV2001 -> 70000");
System.out.println();
System.out.println("KStream sees: 3 separate events");
System.out.println(" [50000, 80000, 70000]");
System.out.println();
System.out.println("KTable sees: latest value only");
System.out.println(" SAV2001 -> 70000");
System.out.println(" (50000 and 80000 are replaced)");
System.out.println();
System.out.println("KStream = what happened (all events)");
System.out.println("KTable = what is now (current state)");
}
}
Expected output:
Input: 3 records for key SAV2001
SAV2001 -> 50000
SAV2001 -> 80000
SAV2001 -> 70000
KStream sees: 3 separate events
[50000, 80000, 70000]
KTable sees: latest value only
SAV2001 -> 70000
(50000 and 80000 are replaced)
KStream = what happened (all events)
KTable = what is now (current state)
What operations does KTable support?
KTable supports a subset of KStream operations, plus some unique ones.
| Operation | What it does |
|---|---|
| filter(predicate) | Keep entries matching condition |
| filterNot(predicate) | Remove entries matching condition |
| mapValues(mapper) | Transform values (key stays the same) |
| toStream() | Convert KTable to KStream (emit every change as an event) |
| join(otherKTable) | Join two KTables by key |
| leftJoin(otherKTable) | Left join: keep all entries from this KTable |
| groupBy(mapper) | Regroup by a new key for aggregation |
| suppress(config) | Buffer updates and emit only the final result per window |
package com.bank.streams;
import org.apache.kafka.streams.StreamsBuilder;
import org.apache.kafka.streams.kstream.KTable;
import org.apache.kafka.streams.kstream.Materialized;
import org.apache.kafka.streams.Topology;
import java.math.BigDecimal;
public class KTableOperations {
public static void main(String[] args) {
StreamsBuilder builder = new StreamsBuilder();
KTable<String, String> balances = builder.table(
"account-balances", Materialized.as("balances"));
// filter: keep only accounts with balance above 10,000
KTable<String, String> highBalance = balances
.filter((accountId, balance) -> {
BigDecimal b = new BigDecimal(balance);
return b.compareTo(new BigDecimal("10000")) > 0;
});
// mapValues: format for display
KTable<String, String> formatted = highBalance
.mapValues(balance -> "INR " + balance);
// toStream: convert to KStream for writing to output topic
formatted.toStream().to("high-balance-accounts");
System.out.println("KTable operations:");
System.out.println(" table() -> create KTable");
System.out.println(" filter() -> keep balances > 10,000");
System.out.println(" mapValues() -> format as INR");
System.out.println(" toStream() -> emit changes for output");
}
}
Expected output:
KTable operations:
table() -> create KTable
filter() -> keep balances > 10,000
mapValues() -> format as INR
toStream() -> emit changes for output
What is a complete banking project using KTable?
This project builds a real-time account balance tracker for National Bank of India. It reads transaction events as a KStream, aggregates them into running balances as a KTable, and publishes balance changes to an output topic.
Project structure
balance-tracker-service/
├── pom.xml
└── src/
├── main/
│ └── java/
│ └── com/
│ └── bank/
│ └── streams/
│ ├── BalanceTrackerApp.java
│ ├── BalanceTrackerTopology.java
│ └── Transaction.java
└── test/
└── java/
└── com/
└── bank/
└── streams/
└── BalanceTrackerTest.java
Transaction.java
package com.bank.streams;
import java.math.BigDecimal;
public class Transaction {
private String accountId;
private String type; // CREDIT or DEBIT
private BigDecimal amount;
public Transaction() {}
public Transaction(String accountId, String type, BigDecimal amount) {
this.accountId = accountId;
this.type = type;
this.amount = amount;
}
public String getAccountId() { return accountId; }
public String getType() { return type; }
public BigDecimal getAmount() { return amount; }
@Override
public String toString() {
return accountId + "|" + type + "|" + amount;
}
public static Transaction fromString(String value) {
String[] p = value.split("\\|");
return new Transaction(p[0], p[1], new BigDecimal(p[2]));
}
}
BalanceTrackerTopology.java
package com.bank.streams;
import org.apache.kafka.streams.StreamsBuilder;
import org.apache.kafka.streams.Topology;
import org.apache.kafka.streams.kstream.KStream;
import org.apache.kafka.streams.kstream.KTable;
import org.apache.kafka.streams.kstream.Materialized;
import org.apache.kafka.common.serialization.Serdes;
import org.apache.kafka.common.utils.Bytes;
import org.apache.kafka.streams.state.KeyValueStore;
import java.math.BigDecimal;
public class BalanceTrackerTopology {
public static Topology build() {
StreamsBuilder builder = new StreamsBuilder();
// Read transactions as KStream (every event)
KStream<String, String> transactions = builder.stream("bank-transactions");
// Aggregate into KTable: running balance per account
KTable<String, String> balances = transactions
.groupByKey()
.aggregate(
// Initializer: start with zero balance
() -> "0",
// Aggregator: add or subtract based on type
(accountId, txnValue, currentBalance) -> {
Transaction txn = Transaction.fromString(txnValue);
BigDecimal balance = new BigDecimal(currentBalance);
if ("CREDIT".equals(txn.getType())) {
balance = balance.add(txn.getAmount());
} else {
balance = balance.subtract(txn.getAmount());
}
return balance.toPlainString();
},
// State store configuration
Materialized.<String, String, KeyValueStore<Bytes, byte[]>>as(
"account-balances-store")
.withKeySerde(Serdes.String())
.withValueSerde(Serdes.String())
);
// Publish balance changes to output topic
balances.toStream()
.peek((accountId, balance) ->
System.out.println("Balance: " + accountId + " = INR " + balance))
.to("account-balance-updates");
return builder.build();
}
}
BalanceTrackerApp.java
package com.bank.streams;
import org.apache.kafka.streams.KafkaStreams;
import org.apache.kafka.streams.StreamsConfig;
import org.apache.kafka.streams.Topology;
import org.apache.kafka.common.serialization.Serdes;
import java.util.Properties;
import java.util.concurrent.CountDownLatch;
public class BalanceTrackerApp {
public static void main(String[] args) {
Properties props = new Properties();
props.put(StreamsConfig.APPLICATION_ID_CONFIG, "balance-tracker-service");
props.put(StreamsConfig.BOOTSTRAP_SERVERS_CONFIG, "localhost:9092");
props.put(StreamsConfig.DEFAULT_KEY_SERDE_CLASS_CONFIG,
Serdes.String().getClass());
props.put(StreamsConfig.DEFAULT_VALUE_SERDE_CLASS_CONFIG,
Serdes.String().getClass());
Topology topology = BalanceTrackerTopology.build();
KafkaStreams streams = new KafkaStreams(topology, props);
CountDownLatch latch = new CountDownLatch(1);
Runtime.getRuntime().addShutdownHook(new Thread(() -> {
streams.close();
latch.countDown();
System.out.println("Balance tracker stopped");
}));
try {
streams.start();
System.out.println("Balance tracker started");
System.out.println(" Input: bank-transactions");
System.out.println(" State: account-balances-store (KTable)");
System.out.println(" Output: account-balance-updates");
latch.await();
} catch (Exception e) {
System.exit(1);
}
}
}
BalanceTrackerTest.java
package com.bank.streams;
import org.apache.kafka.streams.TopologyTestDriver;
import org.apache.kafka.streams.TestInputTopic;
import org.apache.kafka.streams.TestOutputTopic;
import org.apache.kafka.streams.StreamsConfig;
import org.apache.kafka.streams.state.KeyValueStore;
import org.apache.kafka.streams.state.ValueAndTimestamp;
import org.apache.kafka.common.serialization.Serdes;
import org.apache.kafka.common.serialization.StringSerializer;
import org.apache.kafka.common.serialization.StringDeserializer;
import java.util.Properties;
public class BalanceTrackerTest {
public static void main(String[] args) {
Properties props = new Properties();
props.put(StreamsConfig.APPLICATION_ID_CONFIG, "test");
props.put(StreamsConfig.BOOTSTRAP_SERVERS_CONFIG, "dummy:1234");
props.put(StreamsConfig.DEFAULT_KEY_SERDE_CLASS_CONFIG,
Serdes.String().getClass());
props.put(StreamsConfig.DEFAULT_VALUE_SERDE_CLASS_CONFIG,
Serdes.String().getClass());
try (TopologyTestDriver driver =
new TopologyTestDriver(BalanceTrackerTopology.build(), props)) {
TestInputTopic<String, String> input = driver.createInputTopic(
"bank-transactions",
new StringSerializer(), new StringSerializer());
TestOutputTopic<String, String> output = driver.createOutputTopic(
"account-balance-updates",
new StringDeserializer(), new StringDeserializer());
// Rajesh (SAV2001): deposit 50000, deposit 30000, withdraw 10000
input.pipeInput("SAV2001", "SAV2001|CREDIT|50000");
input.pipeInput("SAV2001", "SAV2001|CREDIT|30000");
input.pipeInput("SAV2001", "SAV2001|DEBIT|10000");
// Priya (CUR3001): deposit 100000
input.pipeInput("CUR3001", "CUR3001|CREDIT|100000");
// Check state store (KTable current values)
KeyValueStore<String, ValueAndTimestamp<String>> store =
driver.getTimestampedKeyValueStore("account-balances-store");
System.out.println("KTable state (current balances):");
System.out.println(" SAV2001: INR " + store.get("SAV2001").value());
System.out.println(" CUR3001: INR " + store.get("CUR3001").value());
System.out.println();
System.out.println("Output stream (every balance change):");
while (!output.isEmpty()) {
var record = output.readKeyValue();
System.out.println(" " + record.key + " -> INR " + record.value);
}
System.out.println();
System.out.println("Test passed: KTable holds 70000 for SAV2001, 100000 for CUR3001");
}
}
}
Expected output:
Balance: SAV2001 = INR 50000
Balance: SAV2001 = INR 80000
Balance: SAV2001 = INR 70000
Balance: CUR3001 = INR 100000
KTable state (current balances):
SAV2001: INR 70000
CUR3001: INR 100000
Output stream (every balance change):
SAV2001 -> INR 50000
SAV2001 -> INR 80000
SAV2001 -> INR 70000
CUR3001 -> INR 100000
Test passed: KTable holds 70000 for SAV2001, 100000 for CUR3001
How to run
# Step 1: Create topics
kafka-topics.sh --create --topic bank-transactions --partitions 4 --replication-factor 1 --bootstrap-server localhost:9092
kafka-topics.sh --create --topic account-balance-updates --partitions 4 --replication-factor 1 --bootstrap-server localhost:9092
# Step 2: Build and test
cd balance-tracker-service
mvn clean package
java -cp target/balance-tracker-service-1.0.0.jar com.bank.streams.BalanceTrackerTest
# Step 3: Run
java -cp target/balance-tracker-service-1.0.0.jar com.bank.streams.BalanceTrackerApp
# Step 4: Produce transactions
kafka-console-producer.sh --topic bank-transactions --property "parse.key=true" --property "key.separator=:" --bootstrap-server localhost:9092
# SAV2001:SAV2001|CREDIT|50000
# SAV2001:SAV2001|CREDIT|30000
# SAV2001:SAV2001|DEBIT|10000
# CUR3001:CUR3001|CREDIT|100000
# Step 5: Consume balance updates
kafka-console-consumer.sh --topic account-balance-updates --from-beginning --bootstrap-server localhost:9092
What are common mistakes when working with KTable?
Wrong: reading a high-volume event topic as KTable
package com.bank.streams;
public class WrongHighVolumeKTable {
public static void main(String[] args) {
System.out.println("WRONG: builder.table('bank-transactions')");
System.out.println();
System.out.println(" Problem: bank-transactions has millions of events per day");
System.out.println(" KTable reads ALL records to build the state store");
System.out.println(" Most records are overwritten by newer ones for the same key");
System.out.println(" But every record must be processed during state restoration");
System.out.println();
System.out.println(" Result:");
System.out.println(" - Slow startup (rebuilding state from all events)");
System.out.println(" - High disk usage (state store + changelog)");
System.out.println(" - Wasted processing on overwritten values");
}
}
Expected output:
WRONG: builder.table('bank-transactions')
Problem: bank-transactions has millions of events per day
KTable reads ALL records to build the state store
Most records are overwritten by newer ones for the same key
But every record must be processed during state restoration
Result:
- Slow startup (rebuilding state from all events)
- High disk usage (state store + changelog)
- Wasted processing on overwritten values
Correct: use a compacted topic for KTable source
package com.bank.streams;
public class CorrectCompactedKTable {
public static void main(String[] args) {
System.out.println("CORRECT: Use a compacted topic for KTable");
System.out.println();
System.out.println(" Step 1: Create a compacted topic");
System.out.println(" kafka-topics.sh --create --topic account-balances");
System.out.println(" --config cleanup.policy=compact");
System.out.println();
System.out.println(" Step 2: Read it as KTable");
System.out.println(" builder.table('account-balances')");
System.out.println();
System.out.println(" Why compacted:");
System.out.println(" - Kafka keeps only the latest value per key");
System.out.println(" - State restoration reads fewer records");
System.out.println(" - Smaller state store on disk");
System.out.println(" - Faster startup after crash recovery");
}
}
Expected output:
CORRECT: Use a compacted topic for KTable
Step 1: Create a compacted topic
kafka-topics.sh --create --topic account-balances
--config cleanup.policy=compact
Step 2: Read it as KTable
builder.table('account-balances')
Why compacted:
- Kafka keeps only the latest value per key
- State restoration reads fewer records
- Smaller state store on disk
- Faster startup after crash recovery
What should a senior or principal engineer know?
At the senior level, you should understand that KTable source topics should be compacted, that state stores use RocksDB by default, and that changelog topics back up the state for fault tolerance.
At the principal level, consider:
- Caching: KTable caches updates in memory before flushing to the state store. This deduplicates rapid updates for the same key, reducing downstream output and state store writes. Configure with
cache.max.bytes.buffering(default 10MB). - State store sizing: the KTable state store grows with the number of unique keys. For a topic with 10 million unique accounts, RocksDB needs disk space proportional to key count times average value size.
- Changelog compaction: the changelog topic for a KTable should have
cleanup.policy=compactso Kafka removes old versions of keys. Without compaction, state restoration reads the full changelog history. - Versioned state stores (Kafka 3.5+): allow querying the state store at a specific timestamp, not just the latest value. Useful for point-in-time balance queries.
- Interactive queries: expose the KTable state store via a REST API so external services can query account balances without consuming the output topic.
package com.bank.streams;
import org.apache.kafka.streams.StreamsConfig;
import java.util.Properties;
public class KTablePrincipalConfig {
public static void main(String[] args) {
Properties props = new Properties();
props.put(StreamsConfig.APPLICATION_ID_CONFIG, "balance-tracker");
props.put(StreamsConfig.CACHE_MAX_BYTES_BUFFERING_CONFIG, 10 * 1024 * 1024L);
System.out.println("Principal-level KTable configuration:");
System.out.println();
System.out.println("1. Caching (cache.max.bytes.buffering = 10MB):");
System.out.println(" Deduplicates rapid updates before flushing");
System.out.println(" Reduces downstream output and store writes");
System.out.println();
System.out.println("2. State store sizing:");
System.out.println(" 10M accounts x 100 bytes avg = ~1 GB RocksDB");
System.out.println(" Plan disk for 2x (active + compaction overhead)");
System.out.println();
System.out.println("3. Source topic: cleanup.policy=compact");
System.out.println(" Changelog topic: automatically compacted");
System.out.println();
System.out.println("4. Interactive queries:");
System.out.println(" ReadOnlyKeyValueStore<String, String> store =");
System.out.println(" streams.store(StoreQueryParameters.fromNameAndType(");
System.out.println(" 'balance-store', QueryableStoreTypes.keyValueStore()));");
System.out.println(" Expose via REST: GET /balance/SAV2001 -> 70000");
}
}
Expected output:
Principal-level KTable configuration:
1. Caching (cache.max.bytes.buffering = 10MB):
Deduplicates rapid updates before flushing
Reduces downstream output and store writes
2. State store sizing:
10M accounts x 100 bytes avg = ~1 GB RocksDB
Plan disk for 2x (active + compaction overhead)
3. Source topic: cleanup.policy=compact
Changelog topic: automatically compacted
4. Interactive queries:
ReadOnlyKeyValueStore<String, String> store =
streams.store(StoreQueryParameters.fromNameAndType(
'balance-store', QueryableStoreTypes.keyValueStore()));
Expose via REST: GET /balance/SAV2001 -> 70000
What are common interview follow-up questions?
- What happens to the KTable state store when the application restarts? (It is rebuilt from the changelog topic. If standby replicas are configured, the failover is faster because the standby already has the state.)
- Can you update a KTable by writing to its source topic from outside the Streams app? (Yes. Any producer can write to the source topic. The KTable will pick up the new record and update its state store.)
- What is the difference between builder.table() and groupByKey().aggregate()? (builder.table() reads a topic directly as a KTable. groupByKey().aggregate() computes a KTable from a KStream by applying an aggregation function.)
- How does KTable handle out-of-order records? (Within a partition, records are ordered. KTable always applies the latest record it receives. If records arrive out of order due to producer retries, the last-writer-wins.)
- Can you join a KTable with a KStream? (Yes. A KStream-KTable join enriches each KStream event with the latest value from the KTable. The KTable acts as a lookup table.)
- What is the difference between KTable and GlobalKTable? (KTable is partitioned: each instance holds state for its assigned partitions only. GlobalKTable replicates the entire topic to every instance.)
Visual Overview
KTable: Latest Value Per Key ============================== Input records (account-balances topic): SAV2001 -> 50000 SAV2001 -> 80000 (replaces 50000) SAV2002 -> 25000 SAV2002 -> null (tombstone: deletes SAV2002) State Store (RocksDB): ┌─────────┬─────────┐ │ Key │ Value │ ├─────────┼─────────┤ │ SAV2001 │ 80000 │ │ SAV2002 │ DELETED │ └─────────┴─────────┘ KStream to KTable via Aggregation ================================== KStream (bank-transactions) │ │ SAV2001|CREDIT|50000 │ SAV2001|CREDIT|30000 │ SAV2001|DEBIT|10000 │ ▼ groupByKey() KGroupedStream │ ▼ aggregate() KTable (account-balances-store) ┌─────────┬─────────┐ │ Key │ Balance │ ├─────────┼─────────┤ │ SAV2001 │ 70000 │ └─────────┴─────────┘
Key Takeaway
KTable is the Kafka Streams abstraction for maintaining the current state of data, keyed by record key. Unlike KStream, where every record is a separate event, KTable keeps only the latest value for each key. A new record replaces the old one, and a null value deletes the key.
KTable is backed by a local state store, typically RocksDB, and a changelog topic in Kafka. When the application restarts, the state store is rebuilt from the changelog. For a source topic read with builder.table(), the source topic itself serves as the changelog, so it should use compacted retention.
In our National Bank of India balance tracker project, we read transaction events as a KStream, grouped by account ID, and aggregated credits and debits into a running balance stored in a KTable. The state store held the current balance for each account: 70,000 for SAV2001 after three transactions, 100,000 for CUR3001 after one. The output stream emitted every balance change, while the state store held only the final values.
At the principal level, configure caching to deduplicate rapid updates, size your state stores based on unique key count, use compacted source topics, and consider interactive queries to expose the KTable state via REST without consuming the output topic.
Remember one sentence: KTable keeps the latest value per key in a local state store, so it is a continuously updated table, not an event log.
---