AlgoMaster Logo

KStream vs KTable

High Priority10 min readUpdated November 11, 2026
Listen to this chapter
Unlock Audio

Kafka Streams offers two important abstractions: KStream and KTable.

Both represent data coming from Kafka, but they interpret that data differently.

A KStream treats every record as an independent event, while a KTable treats records as updates to the latest state associated with a key.

This distinction might seem small, but it determines how your application handles updates, aggregations, joins, and state management.

Let's understand both using practical examples.

1. Understanding KStream

A KStream represents a continuous stream of records where every record is treated as an independent event.

Consider an e-commerce application.

Whenever a customer places an order, the Order Service publishes an event to a Kafka topic called orders.

The topic contains records like these:

Notice that customer-1 appears multiple times.

In a KStream, these records represent four separate events.

Even though three records share the same key, each one represents a different order.

Kafka Streams doesn't automatically replace the previous record when another record with the same key arrives.

This makes KStream appropriate for data where every event matters.

Common examples include:

  • Orders and payments
  • User clicks and page views
  • Application logs
  • Sensor readings
  • Financial transactions

Creating a KStream

You can create a KStream using StreamsBuilder.

This creates a stream that continuously consumes records from the orders topic.

Each record has a string key and a string value.

You can now apply operations such as filtering, mapping, grouping, and aggregation.

For example:

This creates another KStream containing only records with non-null values.

The important point is that each input record is processed independently unless you explicitly perform an operation that combines multiple records.

2. Understanding KTable

Now, suppose your application also maintains customer profiles.

Each customer has a membership tier that can change over time.

A Kafka topic called customer-profiles contains records like these:

Unlike order events, these records represent changes to customer information.

When customer-1 changes from Silver to Gold, we usually care about the latest membership tier rather than treating both values as independent facts.

This is where KTable becomes useful.

A KTable represents a continuously updated table of key-value pairs.

Each incoming record is interpreted as an insert or update for its key.

Let's process our customer records one by one.

Initially, the table is empty.

After the first record:

After the second:

Now, when this record arrives:

The existing value for customer-1 is updated.

After all five records, the current table looks like this:

The table maintains the latest value for each key.

You can think of a KTable as a continuously updated materialized view of a Kafka topic.

Creating a KTable

Creating a KTable is similar to creating a KStream.

Instead of calling stream(), we use table().

Kafka Streams interprets the records from customer-profiles as changes to a table.

It maintains the latest state for each key using a local state store.

As new records arrive, Kafka Streams updates the corresponding entries.

This allows applications to perform efficient lookups and stateful operations without repeatedly scanning the entire Kafka topic.

3. KStream vs KTable: The Key Difference

Let's use a simple example to see why the distinction matters.

Suppose a Kafka topic contains:

If we interpret these records as a KStream, they represent three independent events.

If we calculate their sum, the result is:

Now, suppose we interpret the same records as a KTable.

Each record replaces the previous value for Alice.

The updates are:

The current value is simply:

This gives us two different interpretations of exactly the same data.

The diagram below sends the three Alice records down both paths.

The table below summarizes the differences.

KStreamKTable
Represents independent eventsRepresents changes to the latest state
Every record is meaningfulNew values replace previous values for the same key
Suitable for event historiesSuitable for current-state views
Commonly used for orders, clicks, and transactionsCommonly used for profiles, inventory, and account status
Typically processes records independentlyMaintains state by key

The correct abstraction depends on what your records represent.

If records describe things that happened, use a KStream.

If records describe how something's state changes, use a KTable.

4. How KTable Handles Deletions

So far, we've seen how a KTable handles inserts and updates.

But what happens when you want to delete a record?

Kafka uses a special record called a tombstone.

A tombstone is a record with a valid key and a null value.

For example:

When the KTable processes the last record, it removes customer-2 from its current state.

The resulting table becomes:

This is similar to deleting a row from a database.

This behavior is important when building applications that synchronize state between Kafka and databases.

5. KTable and Log Compaction

A Kafka topic used to represent table updates can contain many records for the same key.

For example:

If we only care about the latest membership tier, do we really need to retain every previous update forever?

Not necessarily.

Kafka provides a feature called log compaction, which can remove older records for a key while preserving its latest recorded value.

After compaction, the retained data might look like:

Log compaction is particularly useful for topics that represent continuously changing state.

It reduces the amount of historical data required to reconstruct the latest state.

However, compaction happens asynchronously. Older records may remain for some time, and enabling compaction doesn't immediately remove duplicates.

For event streams where every event matters, removing older records with the same key could change the meaning of the data. That's why compaction is generally more appropriate for changelog-style topics than for event-history topics.

6. Converting Between KStream and KTable

Kafka Streams allows you to convert between these abstractions.

The two methods covered in this section move in opposite directions, as the diagram shows.

Converting KStream to KTable

Suppose you have a stream of inventory updates:

Each value represents the new inventory quantity, not a change to be added.

You can convert this stream into a KTable:

The resulting table interprets every record as an update.

Its current state becomes:

You can also explicitly materialize the table into a named state store:

This is useful when you want Kafka Streams to maintain a queryable local view of the data.

Remember that the conversion changes the interpretation of records. It doesn't automatically turn event values into accumulated totals.

If each inventory record represents a stock adjustment, such as +10 or -5, you would need an aggregation instead.

Converting KTable to KStream

You can also convert a KTable into a KStream using toStream().

This creates a stream of changes emitted by the table.

For example:

Each emitted record represents a table update.

This is useful when you want to send table changes to another Kafka topic or downstream application.

One subtle detail: this isn't necessarily a complete record of every intermediate change. Kafka Streams may cache and combine consecutive table updates before forwarding them.

7. Combining KStream and KTable with Joins

One of the most useful Kafka Streams patterns is joining an event stream with a table.

Suppose you have two Kafka topics.

The first contains orders:

The second contains customer membership tiers:

You want to enrich every order with the customer's current membership tier.

Instead of calling a database for each order, you can join the KStream of orders with the KTable of customers.

Assuming the customer table has already received the profile updates, the output would look like:

Here's what happens internally:

  1. Kafka Streams maintains the current customer tiers in a state store.
  2. When an order arrives, it looks up the customer using the record key.
  3. It combines the order with the current membership tier.
  4. It forwards the enriched record downstream.

The diagram below follows order-101 through these four steps.

An important detail is that a stream-table join is triggered by records arriving on the KStream side.

For a regular partitioned KTable join, both inputs must be appropriately co-partitioned by the join key.

These details become important when designing reliable event enrichment pipelines.

8. Aggregations Produce KTables

Another common use of KTable is storing the result of an aggregation.

Suppose you have a stream of order events keyed by customer ID.

You want to count the number of orders placed by each customer.

Kafka Streams maintains a running count for every customer.

For example:

The current table state is:

Notice that count() returns a KTable, not a KStream.

That's because the count represents continuously changing state.

This is a common pattern in Kafka Streams:

Events flow through KStreams, while their aggregated results are often represented as KTables.

9. When Should You Use Each?

Consider what you're trying to represent.

Use a KStream when you need to process individual events, such as tracking user actions, filtering payments, or detecting suspicious transactions.

Use a KTable when you need to maintain the current value for each key, such as tracking inventory levels, customer profiles, or aggregated metrics.

In real applications, you'll often use both.

For example, an e-commerce application might process orders as a KStream, maintain product inventory as a KTable, and calculate order counts as another KTable.

These abstractions work together to support continuous processing without requiring every service to query a central database.

Key Takeaways

KStream and KTable are two fundamental abstractions in Kafka Streams.

A KStream represents independent events, where every record matters.

A KTable represents the latest state for each key, where new records update or delete existing values.

KTables are especially useful for maintaining materialized views, storing aggregation results, and enriching event streams through joins.

Understanding the difference helps you choose the right representation for your data and avoid mistakes when processing updates.

Quiz

KStream vs KTable Quiz

10 quizzes