System Design: Why is Kafka fast?
Summary of System Design: Why is Kafka fast? from ByteByteGo · Published 2022-06-29 · Views: 1,231,856
This note was generated automatically from the video transcript.
TL;DR
Kafka’s speed comes from two core design choices: using an append‑only log to guarantee sequential disk I/O, and leveraging the zero‑copy system‑call path (e.g., sendfile()) to move data from disk to the network with a single memory copy.
Key Insights
- Sequential I/O vs. random I/O: Sequential writes on HDD arrays achieve hundreds of MB/s, while random writes drop to hundreds of KB/s – a difference of three orders of magnitude.
- Append‑only log: Kafka stores records in a single, ever‑growing file per partition, ensuring every write is sequential.
- Cost‑effective storage: HDDs cost ~⅓ of SSDs and provide ~3× the capacity, enabling long‑term retention without sacrificing throughput.
- Zero‑copy data path: With
sendfile(), Kafka copies data only once (OS cache → NIC buffer), eliminating multiple user‑space copies. - DMA offloads copy work: Modern NICs move data from memory to the wire via Direct Memory Access, freeing the CPU.
- Throughput focus: Kafka optimizes for moving large volumes of data quickly, not for ultra‑low latency per‑message.
Detailed Breakdown
1. What “fast” means for Kafka
Kafka is marketed as “fast” primarily in terms of throughput—the ability to move massive amounts of data (records) per second. The analogy used is a wide pipe: a larger diameter (higher throughput) moves more liquid (data) than a narrow one, regardless of the speed of the flow.
2. Sequential I/O – the foundation
- Disk access patterns:
- Random I/O → arm of HDD moves to different tracks → high latency, low bandwidth.
- Sequential I/O → arm stays on a contiguous track → high bandwidth.
- Performance numbers (modern HDD arrays):
- Sequential writes: ≈ hundreds MB/s.
- Random writes: ≈ hundreds KB/s.
- Kafka’s data structure: An append‑only log per partition. New records are always written to the end of the file, guaranteeing sequential writes.
flowchart LR
Producer["Producer"] -->|writes| Log["Append‑only Log (partition)"]
Log -->|sequential write| Disk["HDD Array"]
Disk -->|stores| Data["Persisted Records"]
- Economic advantage: HDDs are ~⅓ the price of SSDs and provide ~3× the capacity, allowing Kafka clusters to retain large volumes of data cheaply while still achieving high throughput.
3. Zero‑copy – minimizing data movement
3.1 Non‑zero‑copy path (inefficient)
- Disk → OS page cache.
- OS cache → Kafka user‑space buffer (copy).
- Kafka buffer → socket buffer (copy).
- Socket buffer → NIC buffer (copy).
- NIC transmits over the network.
Result: 4 copies + 2 system calls.
3.2 Zero‑copy path (efficient)
- Disk → OS page cache (same as above).
- Kafka invokes
sendfile()→ kernel copies directly from OS cache to NIC buffer. - NIC DMA transfers data to the wire (CPU not involved).
Result: 1 copy + 1 system call.
sequenceDiagram
participant Producer
participant KafkaApp
participant OSCache
participant NIC
participant Consumer
Note over KafkaApp: Non‑zero‑copy path
Producer->>KafkaApp: Read request
KafkaApp->>OSCache: read() copy #1
OSCache->>KafkaApp: data copy #2
KafkaApp->>NIC: write() copy #3
NIC->>Consumer: transmit copy #4
Note over KafkaApp: Zero‑copy path
Producer->>KafkaApp: Read request
KafkaApp->>OSCache: sendfile() (no copy)
OSCache->>NIC: DMA copy #1
NIC->>Consumer: transmit
- DMA (Direct Memory Access): The NIC reads data directly from memory without CPU intervention, further reducing CPU load and latency.
4. Why these two choices dominate
- Sequential I/O maximizes disk bandwidth, allowing Kafka to ingest and retain massive streams on inexpensive hardware.
- Zero‑copy eliminates unnecessary memory copies, freeing CPU cycles for handling more connections or processing more messages.
Together, they give Kafka its hallmark high‑throughput capability while keeping hardware costs low.
Trade‑offs and Gotchas
- Latency vs. throughput: Kafka’s design favors bulk data movement; per‑message latency can be higher than systems optimized for low‑latency (e.g., in‑memory queues).
- Hardware dependence: The sequential‑I/O advantage shrinks on pure SSD deployments where random I/O is much faster; however, cost considerations still favor HDDs for large retention.
- Zero‑copy limitations:
sendfile()works only for file‑based data; custom serialization or compression that requires transformation in user space may break the zero‑copy path. - Operating‑system support: Zero‑copy relies on OS kernels that implement
sendfile()efficiently; older kernels may not provide the same benefit. - Back‑pressure handling: Because Kafka can ingest data extremely fast, producers must respect broker back‑pressure; otherwise, disk usage can balloon.
Takeaways
- Design for sequential writes: Use an append‑only log to keep disk I/O strictly sequential and reap 1000× bandwidth gains over random writes.
- Leverage zero‑copy APIs:
sendfile()(or equivalents) reduces memory copies and CPU overhead when moving data from disk to network. - Choose storage wisely: HDDs give a cost‑effective way to store petabytes while still delivering high throughput thanks to sequential I/O.
- Expect trade‑offs: High throughput comes at the expense of per‑message latency and may require careful producer throttling.
- Modern NICs matter: DMA offloading is essential to fully realize zero‑copy benefits; ensure your hardware supports it.
Glossary
- Sequential I/O: Disk operations that read or write data in a contiguous order, minimizing seek time.
- Append‑only log: A file where new records are always added at the end, never overwritten in place.
- Zero‑copy: A technique where data is transferred between kernel buffers and the network interface without intermediate copies in user space.
sendfile(): A Unix system call that moves data directly from a file descriptor to a socket descriptor within the kernel.- DMA (Direct Memory Access): A hardware feature that allows devices (e.g., NICs) to read/write memory without CPU intervention.
Leave a comment