Kafka Streams KTable store with change log topic vs log compacted source topic
Master System Design with Codemia
Enhance your system design skills with over 120 practice problems, detailed solutions, and hands-on exercises.
Apache Kafka Streams is a client library used for building applications and microservices that process and analyze data stored in Kafka topics. Within Kafka Streams, there are primary ways the data is represented: KStream and KTable. In this discussion, we will delve into the nuances of KTable and specifically how it interacts with change log topics and log-compacted source topics.
Understanding KTable
KTable is a high-level abstraction provided by Kafka Streams that models a changelog stream as a continuously updating table. A KTable object in Kafka Streams represents an ever-evolving table of data. Each record in Kafka might represent a change (either an update or a delete) corresponding to a key in it. Essentials of KTable include:
- Fault tolerance: It is maintained by backing up the content with a Kafka topic.
- State Store usage: Underneath, a KTable often uses a local state store that can be queried.
Change Log Topics vs. Log Compacted Source Topics
KTable interacts closely with two major types of Kafka topics:
1. Change Log Topic
This is a special kind of topic in Kafka used specifically to back a KTable. It effectively logs all changes that occur in the KTable, allowing for a backup and replication mechanism that enhances fault-tolerance.
- Functionality: Each entry in a KTable is a representation of the latest value for a given key, and the change log topic will store all changes that lead to the current value.
- Recovery: In the case of node failure, the change log topic is used to restore the state of the KTable.
2. Log Compacted Source Topic
Log compaction is a feature in Kafka that allows retaining only the last known value for each key in the partition. This is crucial for source topics meant to restore state in applications.
- Efficiency: Through the retention of merely the latest value per key, log compaction reduces storage needs and speeds up recovery time for consumer applications.
- Usage: A typical use case involves source topics for KTables, where only the most current representation of data per key is relevant.
How They Interact
Change Log Topic:
- Kafka Streams internally uses a change log topic to back every KTable. This topic parallels the records of the state store associated with the KTable.
- When a KTable is restored from a failure, the change log topic is replayed to reconstruct the state store from scratch.
Log Compacted Source Topic:
- Often, the source topics feeding data into a KTable are themselves log-compacted. This means that Kafka Streams consumers starting up or recovering can quickly catch up to the latest state by consuming the latest values per key, skipping over all intermediate updates.
Practical Example
Consider an application tracking user statuses in real-time with Kafka Streams:
Source Topic (Log Compacted):
- Topic Name:
user-status - Stores latest status update per user.
KTable:
- Reads from
user-status. - Key: User ID.
- Value: Latest status.
Change Log Topic:
- Automatically managed by Kafka Streams.
- Records every change to the user status as updated through the
user-statusKTable.
Summary Table
| Aspect | Change Log Topic | Log Compacted Source Topic |
| Purpose | Back up KTable state. | Source data for KTable. |
| Compaction | Full change history per key. | Only latest value per key. |
| Use in Recovery | Rebuilds entire KTable from history. | Quick catch up to latest. |
| Storage Efficiency | Lower due to complete history retention. | Higher due to single value. |
| Typical Content | Every change in table state. | Latest state per key only. |
Conclusion
KTable forms a foundational part of the Kafka Streams data handling paradigm, significantly enhanced by its interaction with both change log and log compacted source topics. This combination ensures resilience and efficiency, accommodating both the immediate recovery needs and long-term data integrity and access demands of real-world applications. These properties make Kafka Streams a powerful tool for stateful large-scale data processing in distributed systems.

