Scrivio Interview library
ConceptIntermediate6 min read · updated 12 November 2026

Explain the difference between a KStream, a KTable and a GlobalKTable in Kafka Streams, and how state stores and changelog topics make stateful processing fault tolerant.

A KStream treats every record as an independent event, a KTable treats records as updates and keeps only the latest value per key, and a GlobalKTable is a KTable whose full contents are copied to every application instance. Stateful operations such as aggregations and joins keep their state in local state stores, RocksDB by default, and every store is backed by a compacted changelog topic in Kafka. If an instance fails, another instance rebuilds the store from the changelog, and standby replicas (num.standby.replicas) keep warm copies so that recovery takes seconds instead of minutes.

Why this question is asked

Kafka Streams lets us write stateful stream processing, counts, joins and running totals, as an ordinary Java library inside our service. The first concepts we meet are its three abstractions, and choosing the wrong one is a classic source of wrong results. The second question is how state survives a crash when it lives on a local disk. Interviewers ask both together because they reveal whether we understand the stream-table duality behind Kafka Streams. In this answer, we will compare the three abstractions with one small example, see how state stores work, follow a failover through the changelog, and see how standby replicas speed it up.

1KStream: Every Record Is an Event

A KStream is an unbounded sequence of records where each record stands on its own. If the topic page-views receives (user-7, /home) and then (user-7, /cart), a KStream sees two separate facts: user 7 viewed the home page, and user 7 viewed the cart. Neither replaces the other. Counting a KStream by key gives 2 for user 7.

KStreams fit facts that happen: clicks, payments, sensor readings, orders placed. We filter them, map them, branch them and aggregate them, and each record is processed once as it flows through.

2KTable and GlobalKTable: Latest Value per Key

A KTable reads the same kind of topic differently: each record is an update to the current value for its key. If the topic customer-tier receives (cust-42, SILVER) and then (cust-42, GOLD), the KTable holds one row for cust-42 with the value GOLD. A record with a null value, a tombstone, deletes the key. This view is called a changelog: the topic is the history of changes, and the table is the current state those changes produce.

A KTable is partitioned like its source topic. Each application instance holds only the keys of the partitions it is assigned, so with 6 partitions and 3 instances, each instance holds about a third of the customers. A GlobalKTable, by contrast, is fully replicated: every instance reads all partitions and holds every key. That costs memory and disk on each instance but allows joins on any key without the co-partitioning rules a KTable join needs, which suits small, slowly changing reference data such as country codes or product categories.

Java
StreamsBuilder b = new StreamsBuilder();
KStream<String, String> views = b.stream("page-views"); // every record an event
KTable<String, String> tiers = b.table("customer-tier"); // latest value per key, partitioned
GlobalKTable<String, String> countries = b.globalTable("countries"); // all keys on every instance
// Input to page-views: (user-7, /home), (user-7, /cart)
views.groupByKey().count(); // user-7 -> 2
// Input to customer-tier: (cust-42, SILVER), (cust-42, GOLD)
// tiers now holds cust-42 -> GOLD (one row)
Same idea, three abstractions
AbstractionA record meansData held per instanceGood for
KStreamAn independent eventNone by itselfClicks, payments, orders
KTableAn update to the key's valueKeys of assigned partitionsCurrent state, large tables
GlobalKTableAn update to the key's valueEvery key of the topicSmall reference data

3Where Stateful Operations Keep Their State

A stateless operation such as filter needs nothing but the current record. A stateful one, such as count, aggregate, reduce or a join, must remember something between records: the count so far, or the latest customer for each key. Kafka Streams keeps that in a state store, a local key-value store owned by one task, the unit of work that processes one partition of the input.

By default, persistent state stores use RocksDB, an embedded key-value database that writes to local disk under state.dir. RocksDB lets state grow far beyond the JVM heap, and reads are local, so a lookup takes microseconds instead of a network round trip to a database. We can also choose in-memory stores when state is small.

Java
KTable<String, Long> viewsPerUser = views
.groupByKey()
.count(Materialized.as("views-per-user")); // RocksDB store named views-per-user
// Kafka Streams also creates the topic
// <application.id>-views-per-user-changelog (cleanup.policy=compact)

4How the Changelog Makes State Fault Tolerant

Local disk alone would be fragile: if the machine dies, the counts are gone. So every update a task makes to a persistent store is also written to a changelog topic in Kafka, named after the application id and the store. The changelog is compacted, which means Kafka keeps at least the latest record per key and removes older ones, so the topic stays about as large as the state itself rather than growing forever.

When an instance fails, the group rebalances and its tasks move to other instances. A new owner of task 0_3 finds no local store, or an outdated one, and restores it by reading changelog partition 3 from the beginning, or from the offset its local checkpoint file recorded, until it reaches the end. Only then does it start processing new input. With exactly-once enabled, the store updates, the changelog writes and the input offsets all commit together, so restored state matches the processed input exactly.

Step throughFailover of task 0_3
Instance AChangelog topicInstance B
every store update: (user-7, 2), (user-9, 5) ...
crash
rebalance: task 0_3 assigned to B
read changelog partition 3 from start or checkpoint
latest value per key (compacted)
store restored; resume input from committed offset

6 messages in order. Play them, or step through them one at a time.

With large state, the restore step can take minutes; standby replicas avoid most of it.

5Standby Replicas Shorten Recovery

Restoring from the changelog is reliable but can be slow. A 50 GB store may take many minutes to rebuild, and the task processes nothing in the meantime. Standby replicas solve this. With num.standby.replicas=1, which defaults to 0, Kafka Streams assigns each stateful task a shadow copy on another instance. The standby continuously reads the changelog and applies it to its own local store, but does not process input.

When the active instance fails, Kafka Streams prefers to move the task to the instance holding its standby. That instance only needs to catch up on the last few changelog records, so recovery takes seconds. The cost is extra disk and some extra reading on the brokers. Kafka Streams also uses warmup replicas during scaling, building state on a new instance in the background before moving the active task there.

Java
Properties p = new Properties();
p.put(StreamsConfig.APPLICATION_ID_CONFIG, "view-counter");
p.put(StreamsConfig.NUM_STANDBY_REPLICAS_CONFIG, 1); // one warm copy of every store
p.put(StreamsConfig.PROCESSING_GUARANTEE_CONFIG, StreamsConfig.EXACTLY_ONCE_V2);

6Follow-up questions

When would you pick a GlobalKTable over a KTable?

When the table is small enough to fit on every instance, and either the join key is not the stream's key or co-partitioning would be awkward. A GlobalKTable join can use any field of the stream record as the lookup key and needs no repartition. For large tables, a KTable is better because each instance holds only its share.

Is the changelog the same topic as the KTable's source topic?

Not necessarily. For a table read directly from a compacted topic, Kafka Streams can use the source topic itself to restore, if the topology optimization for source-topic reuse is enabled. For stores created by aggregations, there is no source topic with the right content, so a separate changelog topic is created.

What does a KTable do with a null value?

It treats it as a tombstone and deletes the key from the table, and downstream operations see the deletion. That is how a customer is removed from a customer table.

7Conclusion

In this answer, we saw that a KStream treats each record as an independent event, a KTable keeps the latest value per key for its assigned partitions, and a GlobalKTable holds every key on every instance. Stateful operations store their state in local RocksDB stores by default, and every change is written to a compacted changelog topic so that another instance can rebuild the store after a failure. Standby replicas keep warm copies of stores and cut recovery from minutes to seconds.

What to say in the interviewAbout 80 seconds aloud

Open with

A KStream is a stream of independent events: two records for the same key are two facts, so counting gives two. A KTable reads a topic as a changelog, where each record is an update and the table holds the latest value per key, with a null value deleting the key.

Then, if they want more

A KTable is partitioned, so each instance holds only the keys of its assigned partitions. A GlobalKTable is fully replicated to every instance, which costs memory but lets you join on any key without co-partitioning, so it's good for small reference data.

For fault tolerance, stateful operations like aggregations and joins keep their state in local state stores, RocksDB by default, which lets state be larger than the heap and keeps lookups local. Every update to a store is also written to a compacted changelog topic in Kafka.

If an instance dies, its tasks move to other instances, which rebuild the store by replaying the changelog before processing new input, and with exactly-once the store, the changelog and the offsets commit together. Restoring large state can take minutes, so I'd set num.standby.replicas to at least one for important applications.

Standbys keep warm copies on other instances, and Kafka Streams fails over to them, so recovery takes seconds.

About this answer

Last updated
12 November 2026
Checked
Its structure and format are checked automatically. Use it as a carefully prepared study note: where a detail matters, confirm it against the documentation below. What the checks cover
Where the question comes from
Written by Scrivio for practice. It is not taken from an interview.
Report an issue with this answer

Copy the line below and send it to the author with what you found. It carries the address of this page.

Issue with "Explain the difference between a KStream, a KTable and a GlobalKTable in Kafka Streams...": https://scrivio-interview-library.netlify.app/spring-boot/kafka/1e5fecd980/