Kafka
Sequential Disk Access
Data Streaming
Message Queue
System Architecture

How does Kafka guarantee sequential disk access?

Master System Design with Codemia

Enhance your system design skills with over 120 practice problems, detailed solutions, and hands-on exercises.

Apache Kafka, a distributed event streaming platform, is designed to handle high throughput and low-latency reading and writing of messages. One of its key architectural advantages is its ability to guarantee sequential disk access. This is crucial because sequential access to disks is significantly faster than random disk access, especially with traditional spinning hard disks and even benefits solid-state drives (SSDs) to a good extent.

Sequential Disk Access in Kafka

At its core, Kafka’s ability to ensure sequential disk access lies in its use of an append-only log structure. When messages are produced to a Kafka topic, they are appended sequentially to a commit log and indexed for quick access. Here is a step-by-step understanding of how Kafka achieves this:

1. Log Structure

Every topic in Kafka is divided into partitions, and each partition is essentially an ordered, immutable sequence of records that are continually appended to a structured commit log. The records in the partitions are assigned a sequential ID called an offset.

2. Writing Data

When a producer sends data to a Kafka server (broker), the data is appended to the end of the appropriate partition log. The server does not need to seek to where the data will be written; it simply appends it to the end of the file. This append operation ensures that the disk’s write head moves in one direction, thereby maximizing throughput by minimizing disk seek time.

3. Reading Data

Consumers read records from a Kafka partition at their own pace. The consumer tracks its offset. When reading messages, the consumer provides the offset to Kafka, which then reads the data sequentially starting from that point. Since the log files are written sequentially, reading operations benefit similarly by sequential disk access.

4. Disk Flush and File Sync

Kafka allows producers to specify when data should be considered "writen". Producers can choose from different acknowledged modes, which can dictate when data is considered written: immediately after write to buffer, after flushed to disk, or after replicated and flushed on a leader and follower replicas. Depending on the chosen mode, this can influence disk I/O operations.

5. Index Files

Kafka creates an index file for each log segment. The index file stores the offset and physical position of each message within the log segment. This mechanism allows Kafka to jump quickly to a specific message within a log segment while maintaining sequential access patterns for reading the log data itself.

Practical Example

Consider a Kafka topic with three partitions. Each partition may be hosted on a different server or the same server but in different directory paths, each represented by a unique log file.

  • Producer A sends a message to Partition 1. Kafka appends the message to the end of the partition’s log file.
  • Consumer B requests messages starting with offset 105. Kafka consults the index to find the physical position corresponding to offset 105 in the log file and then reads sequentially from there to serve the consumer’s request.

Summary Table

FeatureDescriptionBenefit
Append-Only LogsMessages are appended in a strict sequential order.Reduces disk seek time; Enhances throughput.
PartitioningData is split among multiple logs.Parallelism; Scalability.
Offset IndexingEach log segment is indexed by offset.Quick location of messages in a large file.
Configurable AcknowledgmentsProducers determine when messages are considered "written".Reliability and durability control.

Additional Considerations

While Kafka’s design predominantly ensures sequential disk access, certain operations like log compaction (which cleans up old or superseded messages) and deletion can introduce non-sequential access patterns. However, these operations are typically managed in a way to minimize their impact on the performance of normal reads and writes.

Kafka’s relentless focus on sequential disk access is a key factor behind its high performance and reliability as a messaging system, making it a preferred choice for high-throughput, low-latency applications across varied industries.


Course illustration
Course illustration

All Rights Reserved.