Apache Kafka Tutorial PDF
Apache Kafka Tutorial PDF
This tutorial will explore the principles of Kafka, installation, operations and then it will walk you
through with the deployment of Kafka cluster. Finally, we will conclude with real-time
applications and integration with Big Data Technologies.
Audience
This tutorial has been prepared for professionals aspiring to make a career in Big Data Analytics
using Apache Kafka messaging system. It will give you enough understanding on how to use
Kafka clusters.
Prerequisites
Before proceeding with this tutorial, you must have a good understanding of Java, Scala,
Distributed messaging system, and Linux environment.
All the content and graphics published in this e-book are the property of Tutorials Point (I) Pvt.
Ltd. The user of this e-book is prohibited to reuse, retain, copy, distribute or republish any
contents or a part of contents of this e-book in any manner without written consent of the
publisher.
We strive to update the contents of our website and tutorials as timely and as precisely as
possible, however, the contents may contain inaccuracies or errors. Tutorials Point (I) Pvt. Ltd.
provides no guarantee regarding the accuracy, timeliness or completeness of our website or its
contents including this tutorial. If you discover any errors on our website or in this tutorial,
please notify us at contact@tutorialspoint.com
i
Apache Kafka
Table of Contents
About the Tutorial............................................................................................................................................... i
Audience ............................................................................................................................................................. i
Prerequisites ....................................................................................................................................................... i
2. KAFKA – FUNDAMENTALS.............................................................................................................. 4
4. KAFKA – WORKFLOW..................................................................................................................... 9
Role of ZooKeeper............................................................................................................................................ 11
ii
Apache Kafka
Configuration Settings...................................................................................................................................... 25
Configuration Settings...................................................................................................................................... 31
Execution ......................................................................................................................................................... 44
iii
Apache Kafka
iv
Apache Kafka
1. Kafka – Introduction
In Big Data, an enormous volume of data is used. Regarding data, we have two main
challenges. The first challenge is how to collect large volume of data and the second challenge
is to analyze the collected data. To overcome those challenges, you must need a messaging
system.
Kafka is designed for distributed high throughput systems. Kafka tends to work very well as
a replacement for a more traditional message broker. In comparison to other messaging
systems, Kafka has better throughput, built-in partitioning, replication and inherent fault-
tolerance, which makes it a good fit for large-scale message processing applications.
5
Apache Kafka
What is Kafka?
Apache Kafka is a distributed publish-subscribe messaging system and a robust queue that
can handle a high volume of data and enables you to pass messages from one end-point to
another. Kafka is suitable for both offline and online message consumption. Kafka messages
are persisted on the disk and replicated within the cluster to prevent data loss. Kafka is built
on top of the ZooKeeper synchronization service. It integrates very well with Apache Storm
and Spark for real-time streaming data analysis.
Benefits
Following are a few benefits of Kafka:
Durability - Kafka uses “Distributed commit log” which means messages persists on
disk as fast as possible, hence it is durable.
Performance - Kafka has high throughput for both publishing and subscribing
messages. It maintains stable performance even many TB of messages are stored.
6
Apache Kafka
Kafka is very fast and guarantees zero downtime and zero data loss.
Use Cases
Kafka can be used in many Use Cases. Some of them are listed below:
Metrics - Kafka is often used for operational monitoring data. This involves
aggregating statistics from distributed applications to produce centralized feeds of
operational data.
Log Aggregation Solution - Kafka can be used across an organization to collect logs
from multiple services and make them available in a standard format to multiple
consumers.
Stream Processing - Popular frameworks such as Storm and Spark Streaming read
data from a topic, processes it, and write processed data to a new topic where it
becomes available for users and applications. Kafka’s strong durability is also very
useful in the context of stream processing.
7
Apache Kafka
2. Kafka – Fundamentals
Before moving deep into the Kafka, you must aware of the main terminologies such as topics,
brokers, producers and consumers. The following diagram illustrates the main terminologies
and the table describes the diagram components in detail.
In the above diagram, a topic is configured into three partitions. Partition 1 has two offset
factors 0 and 1. Partition 2 has four offset factors 0, 1, 2, and 3. Partition 3 has one offset
factor 0. The id of the replica is same as the id of the server that hosts it.
Assume, if the replication factor of the topic is set to 3, then Kafka will create 3 identical
replicas of each partition and place them in the cluster to make available for all its operations.
To balance a load in cluster, each broker stores one or more of those partitions. Multiple
producers and consumers can publish and retrieve messages at the same time.
8
Apache Kafka
Components Description
Topics are split into partitions. For each topic, Kafka keeps a
minimum of one partition. Each such partition contains messages in
an immutable ordered sequence. A partition is implemented as a set
Partition of segment files of equal sizes.
ii) Assume if there are N partitions in a topic and more than N brokers
(n + m), the first N broker will have one partition and the next M
Brokers broker will not have any partition for that particular topic.
iii) Assume if there are N partitions in a topic and less than N brokers
(n-m), each broker will have one or more partition sharing among
them. This scenario is not recommended due to unequal load
distribution among the broker.
Kafka’s having more than one broker are called as Kafka cluster. A
Kafka Cluster Kafka cluster can be expanded without downtime. These clusters are
used to manage the persistence and replication of message data.
9
Apache Kafka
"Leader" is the node responsible for all reads and writes for the given
Leader
partition. Every partition has one server acting as a leader.
10
Apache Kafka
3. Kafka – Cluster Architecture
Take a look at the following illustration. It shows the cluster diagram of Kafka.
11
Apache Kafka
12