What does commit-log mean in Kafka?
Master System Design with Codemia
Enhance your system design skills with over 120 practice problems, detailed solutions, and hands-on exercises.
Apache Kafka is a distributed event streaming platform capable of handling trillions of events a day. Initially conceived as a messaging queue, Kafka is based on an abstraction of a distributed commit log. Understanding how Kafka's commit log works is fundamental to comprehending its high performance and durability characteristics.
What is a Commit Log?
At its core, a commit log is a simple data structure that appends records (logs) sequentially to a file or storage system. In Kafka, each broker maintains a commit log for each partition of every topic. Data within a Kafka topic is divided into partitions that allow for data to be spread across a cluster for scalability.
How Kafka Uses Commit Logs
Kafka commit logs serve as the single source of truth for message storage and are integral for fault tolerance and replication. Each message in a log is assigned a unique offset. Kafka does not overwrite or delete records in a log unless configured todo so by policy (such as time-based or size-based retention policies).
Write Operations
When a producer sends a message to a Kafka topic, the message is appended to the end of the commit log. The simplicity of this append-only mechanism facilitates high-throughput and low-latency write operations.
Read Operations
Consumers read messages from a specific offset and can read messages as fast as the network and disk allow. This design allows Kafka to provide real-time streaming capabilities.
Replication
To ensure data is safe and available, Kafka replicates commit logs across multiple brokers. When a producer sends a message, it is written to the leader of the partition, which then replicates it to its followers. This guarantees that data is not lost even in the event of a broker failure.
Durability and Performance
Kafka leverages the filesystem for storing commit logs. Modern operating systems are very efficient at persisting sequential writes to disk, and Kafka utilizes this by writing to logs in a sequential manner, which also minimizes disk seek times.
Log Compaction
Kafka also supports log compaction which ensures that the commit log does not grow indefinitely. This is particularly useful for log data that acts as a record of state changes (key-value data). Log compaction preserves at least the last known value for each key within the log.
Example of Log Anatomy in Kafka
Kafka's commit log consist of a sequence of files. Suppose you have a topic "events" with two partitions, the directory structure on a Kafka broker might look like:
Each .log file corresponds to a segment of the commit log for a partition, and the numbers represent the offset of the first message in the file.
Summarizing Key Points
| Feature | Description |
| Sequential Writes/Reads | Facilitates high throughput and low latency operations. |
| Persistence | Messages are stored reliably on disk. |
| Replication | Commit logs are copied across multiple brokers for fault tolerance. |
| Scalability | Kafka partitions allow logs to be spread across many servers. |
| Log Compaction | Maintains only the latest value for each key to control the log size. |
Conclusion
Kafka's architecture around a distributed commit log not only allows it to provide robust data streaming capabilities but also ensures that the system can scale efficiently while maintaining high performance. Proper understanding and utilization of Kafka's commit logging mechanism are essential for optimizing Kafka-based applications and maintaining system reliability and efficiency.

