Here’s what makes Apache Kafka so fast: Kafka Series - Part 3
Tags: kafka, showdev
Published: 2019-05-05 17:01:45

---
title: Here’s what makes Apache Kafka so fast: Kafka Series - Part 3
published: true
description: Kafka Architecture - what makes Kafka so fast
tags: Kafka,showdev
canonical_url: https://cloudnweb.dev/2019/05/heres-what-makes-apache-kafka-so-fast-kafka-series-part-3/
series: Kafka
---

<!-- wp:paragraph -->
<p>This is Kafka Series Part 3. To Read Previous Blog Posts.</p>
<!-- /wp:paragraph -->

<!-- wp:paragraph -->
<p><a href="https://cloudnweb.dev/2019/04/how-apache-kafka-works-kafka-getting-started-part-1/">Blog Series Kafka - Part 1</a></p>
<!-- /wp:paragraph -->

<!-- wp:paragraph -->
<p><a href="https://cloudnweb.dev/2019/04/kafka-getting-started-kafka-series-part-2/">Kafka Series - Part 2</a></p>
<!-- /wp:paragraph -->

<!-- wp:paragraph -->
<p>Kafka is a distributed streaming platform which supports high-throughput, highly distributed, fault-tolerant with low-latency delivery of messages.</p>
<!-- /wp:paragraph -->

<!-- wp:heading {"level":3} -->
<h3>Low-Latency IO:</h3>
<!-- /wp:heading -->

<!-- wp:paragraph -->
<p>Here, we will see how Kafka achieves the low latency message delivery</p>
<!-- /wp:paragraph -->

<!-- wp:paragraph -->
<p>One of the old traditional ways to achieve low latency in message delivery is through random access memory(RAM). Although this approach makes them fast, the cost of RAM is much more than disk. Such a system is usually costlier to run when you have several 100s GB’s of Data.</p>
<!-- /wp:paragraph -->

<!-- wp:paragraph -->
<p>Kafka relies on disk for storage and caching. but the problem is, disks are slower than RAM.</p>
<!-- /wp:paragraph -->

<!-- wp:paragraph -->
<p>It achieves low latency as random access memory(RAM) through Sequential IO.</p>
<!-- /wp:paragraph -->

<!-- wp:heading {"level":3} -->
<h3>What is Sequential IO ? </h3>
<!-- /wp:heading -->

<!-- wp:paragraph -->
<p>Here is an excellent analogy which explains the Sequential IO in a much simpler way</p>
<!-- /wp:paragraph -->

<!-- wp:paragraph -->
<p><a href="https://flashdba.com/2013/04/15/understanding-io-random-vs-sequential/">Understanding Sequential IO</a></p>
<!-- /wp:paragraph -->

<!-- wp:heading {"level":3} -->
<h3>Let's understand what makes Kafka So Fast</h3>
<!-- /wp:heading -->

<!-- wp:heading {"level":4} -->
<h4>Zero - Copy Principal.</h4>
<!-- /wp:heading -->

<!-- wp:paragraph -->
<p>According to wikipedia,</p>
<!-- /wp:paragraph -->

<!-- wp:paragraph -->
<p><em>"<strong>Zero-copy</strong>" describes computer operations in which the <a href="https://en.wikipedia.org/wiki/Central_processing_unit">CPU</a> does not perform the task of copying data from one <a href="https://en.wikipedia.org/wiki/RAM">memory</a> area to another. This is frequently used to save CPU cycles and memory bandwidth when transmitting a file over a network.<sup><a href="https://en.wikipedia.org/wiki/Zero-copy#cite_note-lj-zerocopy-1">[1]</a></sup></em></p>
<!-- /wp:paragraph -->

<!-- wp:paragraph -->
<p>Confusing. right?</p>
<!-- /wp:paragraph -->

<!-- wp:paragraph -->
<p>Let's see this concept with a simple example. Let's consider a traditional way of the file transfer.  when a client requests a file from the static website. Firstly, website static files read from the disk and write the exact same files to the response socket. This is a very inefficient activity though it appears as the CPU is not performing much activity here. </p>
<!-- /wp:paragraph -->

<!-- wp:paragraph -->
<p>The kernel reads the data off of disk and pushes it across the kernel-user boundary to the application, and then the application pushes it back across the kernel-user boundary to be written out to the socket. In effect, the application serves as an inefficient intermediary that gets the data from the disk file to the socket.</p>
<!-- /wp:paragraph -->

<!-- wp:image {"id":427} -->
<figure class="wp-block-image"><img src="https://cloudnweb.dev/wp-content/uploads/2019/05/Kafka-Zero_Copy.png" alt="" class="wp-image-427"/><figcaption>Tradition Data Copying Approach</figcaption></figure>
<!-- /wp:image -->

<!-- wp:paragraph -->
<p>Every time there is a data transfer beyond the user-boundary kernel, there will be consumption of CPU cycles and memory bandwidth, resulting in a drop in performance especially when the data volumes are huge. This is exactly what the <strong>zero-copy principal</strong> addresses.</p>
<!-- /wp:paragraph -->

<!-- wp:paragraph -->
<p>Kafka makes use of this <strong>zero-copy principal</strong> by requesting the kernel to move the data to sockets rather than moving it via the application.</p>
<!-- /wp:paragraph -->

<!-- wp:paragraph -->
<p>Zero-copy greatly improves application performance and reduces the number of context switches between kernel and user mode.</p>
<!-- /wp:paragraph -->

<!-- wp:image {"id":428} -->
<figure class="wp-block-image"><img src="https://cloudnweb.dev/wp-content/uploads/2019/05/Kafka-Zero_Copy_lat.png" alt="" class="wp-image-428"/><figcaption>Zero-copy Approach.<br></figcaption></figure>
<!-- /wp:image -->

<!-- wp:heading {"level":4} -->
<h4>what else?</h4>
<!-- /wp:heading -->

<!-- wp:paragraph -->
<p>Kafka uses many other techniques apart from the ones mentioned above to make systems much faster and efficient:</p>
<!-- /wp:paragraph -->

<!-- wp:list -->
<ul><li>Batching of data to reduce network calls, and also converting a lot of random writes into sequential ones.</li><li>Compression of batches (and not individual messages) using <a rel="noreferrer noopener" href="https://en.wikipedia.org/wiki/LZ4_%28compression_algorithm%29" target="_blank">LZ4</a>, <a rel="noreferrer noopener" href="https://en.wikipedia.org/wiki/Snappy_%28compression%29" target="_blank">SNAPPY</a>or <a rel="noreferrer noopener" href="https://en.wikipedia.org/wiki/Gzip" target="_blank">GZIP</a> codecs. Much of the data is consistent across messages within a batch (for example, message fields and metadata information). This can lead to better compression ratios.</li></ul>
<!-- /wp:list -->

<!-- wp:paragraph -->
<p>To Read more about Kafka,</p>
<!-- /wp:paragraph -->

<!-- wp:paragraph -->
<p><a href="https://cloudnweb.dev/2019/04/how-apache-kafka-works-kafka-getting-started-part-1/">Kafka Series - Part 1</a></p>
<!-- /wp:paragraph -->

<!-- wp:paragraph -->
<p><a href="https://cloudnweb.dev/2019/04/kafka-getting-started-kafka-series-part-2/">Kafka Series - Part 2</a></p>
<!-- /wp:paragraph -->