Understanding the Architecture of Apache Kafka

January 17, 2023

Photo by Evgeniy Surzhan on Unsplash

Apache Kafka is a distributed streaming platform for developing real-time data pipelines and streaming applications. It is a platform with high throughput and low latency that can handle millions of events per second. It was created by LinkedIn’s engineering team and has since become a popular choice for building data pipelines and streaming applications.

Kafka is a popular choice for handling large volumes of data in real-time because it is designed to be highly scalable and fault-tolerant. It is frequently used to construct data pipelines that collect, store, and process data from various sources such as log files, social media feeds, and sensor data.

Kafka, in addition to its core streaming capabilities, includes a number of other features that make it a powerful data processing and distribution platform. These features include data replay, partitioning, and compression support, as well as integration with a wide range of external systems via Kafka Connect.

Kafka Architecture

The architecture of Kafka is built on a distributed publish-subscribe messaging model. It is made up of the following key components:

Topics: A topic is a data publication category or feed. Topics are created by producers and consumed by consumers. Each topic is divided into one or more partitions, allowing for parallelism in data consumption.

Partitions: In Kafka, a partition is a horizontally scalable unit of data storage. Each partition is an ordered, immutable sequence of records that is being added to all the time. Partitions are an important way for Kafka to scale horizontally, as more consumers can read from a topic at the same time by consuming different partitions.

Brokers: A Kafka broker is a server that stores and serves data to consumers. A Kafka cluster is typically made up of multiple brokers, which provides data redundancy as well as increased scalability.

Producers: Client applications that write data to Kafka topics are known as producers. Producers can select which topic to write to and configure their behavior in a variety of ways, including the ability to set the partition for the data or specify a key for the data to determine to which partition it will belong.

Consumers: Client applications that read data from Kafka topics are known as consumers. Consumers can subscribe to one or more topics and configure their behavior in a variety of ways, such as specifying an offset to begin reading from or joining a consumer group to share the workload of consuming a topic.

Kafka also includes features such as Kafka Streams, a library for building stream processing applications, and Kafka Connect, a tool for integrating Kafka with external systems, in addition to these core components.

In the following sections, we’ll go over these components and how they work in greater detail.

Kafka’s Topics

Topics in Kafka are categories or feeds to which data is published. Topics are created by producers and consumed by consumers. Each topic is divided into one or more partitions, allowing for parallelism in data consumption.

When a producer writes data to a topic, the data is sent to the partition leader of the topic. The data is then replicated by the leader to the partition’s other replicas (also known as followers). The data is stored in the partition in the order in which it was received, and the partition is an ordered, immutable sequence of records that is constantly being appended to.

When a consumer reads data from a topic, it sends a request to the broker in charge of the partition from which the data is being read. The data is then served to the consumer by the broker from the appropriate partition.

Overall, topics are an important concept in Kafka’s architecture, serving as both the entry and exit point for data producers and consumers. They enable producers and consumers to communicate, as well as organize and store data in a scalable and fault tolerant manner.

Kafka’s Partitions

Partitions are a horizontally scalable unit of data storage in Kafka. Each partition is an ordered, immutable sequence of records that is being added to all the time. Partitions are an important way for Kafka to scale horizontally, as more consumers can read from a topic at the same time by consuming different partitions.

When a producer writes data to a topic, the partition to which the data should be written can be specified by setting the partition field in the ProducerRecord object. If no partition is specified, Kafka will use a partitioner or a default partitioner to determine which partition the data should be written to based on the key.

When a consumer reads data from a topic, it sends a request to the broker in charge of the partition from which the data is being read. The data is then served to the consumer by the broker from the appropriate partition.

Overall, partitions are an important concept in Kafka’s architecture because they enable horizontal scalability and high-throughput data processing. They enable data within a topic to be divided into smaller units that can be consumed in parallel, allowing for more efficient and scalable data processing.

Kafka’s Brokers

Brokers in Kafka are servers that store and serve data to consumers. A Kafka cluster is typically made up of multiple brokers, which provides data redundancy as well as increased scalability.

Brokers are in charge of storing data from producers and delivering it to consumers. Each broker manages a set of topic partitions and is in charge of writing data to the appropriate partition and serving it to consumers for that partition.

When a producer writes data to a topic, the data is routed to a specific broker, known as the partition leader. The data is stored in the appropriate partition by the leader broker, who then replicates it to a configurable number of replicas known as in-sync replicas. This replication ensures that the data is durable and accessible in the event of a failure.

When a consumer wishes to read data from a topic, it connects to a broker and requests the information for a specific partition. The data is then served to the consumer by the broker from the appropriate partition. If the consumer is a member of a consumer group, it will be assigned a set of partitions from which to consume and will connect to the appropriate broker for each partition.

Brokers play an important role in Kafka’s architecture by storing and serving data to consumers. They provide data durability and scalability by replicating data across multiple brokers and allowing multiple consumers to consume data in parallel.

Kafka’s Producers

Producers in Kafka are client applications that write data to topics. Producers can write data to any number of topics, and they can configure their behavior in a variety of ways.

A producer must first create a KafkaProducer object and configure it with the appropriate settings before writing data to a Kafka topic. For example, the producer may be required to specify the Kafka broker to which to connect the data serialization format, and any security settings.

After creating the KafkaProducer object, the producer can use the send() method to write data to a topic. The send() method takes as an argument a Kafka ProducerRecord object, which contains the topic name, the data to be written, and any optional key and partition information.

There are a few options available for configuring producer behavior:

Partitioning: By setting the partition field in the ProducerRecord object, producers can specify which partition the data should be written to. If no partition is specified, Kafka will use a partitioner or a default partitioner to determine which partition the data should be written to based on the key.

Key: By setting the key field in the ProducerRecord object, producers can specify a key for the data being written. The key will be used by the partitioner to determine which partition the data should be written to.

Acks: When data is written, producers can specify the level of acknowledgement they want from the Kafka broker by setting the acks configuration property. The values “all” (the broker will wait for all in-sync replicas to acknowledge the write), “leader” (the broker will only wait for the leader replica to acknowledge the write), and “none” are all possible (the broker will not wait for any acknowledgement).

Retries: By setting the “retries” configuration property, producers can specify the number of times to retry sending data in the event of a failure.

Producers have a variety of options for writing data to Kafka topics and configuring their behavior to meet the needs of the specific use case.

Kafka’s Consumers

Consumers in Kafka are client applications that read data from topics. Consumers can subscribe to one or more topics and choose from a variety of options for configuring their behavior.

A consumer must first create a KafkaConsumer object and configure it with the appropriate settings before reading data from a Kafka topic. The consumer may, for example, be required to specify the Kafka broker to connect to, the topic to subscribe to, and any security settings.

After creating the KafkaConsumer object, the consumer can read data from the topic using the poll() method. The poll() method retrieves a batch of records from the topic, which the consumer can process as needed.

There are several options for configuring consumer behavior:

Consumer groups: To share the workload of consuming a topic, consumers can join a consumer group. When a consumer joins a consumer group, it is assigned a set of partitions from which to consume. If the group contains multiple consumers, each consumer will be assigned a different set of partitions, allowing for parallel data consumption.

Offsets: By setting the seek() method on the KafkaConsumer object, consumers can specify an offset from which to begin reading. The offset determines where in the partition the consumer will begin reading.

Autocommit: By setting the enable.auto.commit configuration property, consumers can specify whether to automatically commit offsets after reading data. If enabled, the consumer will commit the offset of the last record read from the partition automatically.

Commit sync/async: By setting the enable.sync.commit configuration property, consumers can specify whether offset commits should be performed synchronously or asynchronously. If this option is enabled, the consumer will wait for the commit to finish before continuing to read data.

Overall, consumers have a variety of options for reading data from Kafka topics and configuring their behavior to meet the requirements of the specific use case. Consumer groups and offsets are especially useful for allowing consumers to read data at their own pace as well as for fault tolerance in the event of a failure.

Conclusion

In summary, Apache Kafka is a distributed streaming platform for developing data pipelines and streaming applications. It has several key features, such as durability, publish-subscribe messaging, and stream processing capabilities, which make it well suited for handling large amounts of data and enabling real-time data processing.

Topics, partitions, brokers, producers, and consumers are all key components of Kafka’s architecture. Topics are created by producers and consumed by consumers. Brokers are servers that store and serve data to consumers, and topics are partitioned to allow for horizontal scalability and efficient data processing.

Overall, Kafka’s architecture is intended to handle large amounts of data while also allowing for real-time data processing, making it an effective tool for building data pipelines and streaming applications.