Over a million developers have joined DZone.
{{announcement.body}}
{{announcement.title}}

Kafka Topic Architecture - Replication, Failover, and Parallel Processing

DZone's Guide to

Kafka Topic Architecture - Replication, Failover, and Parallel Processing

Digging deeper into Kafka architecture, this article covers the details of replication, failover, and parallel processing in this data pipeline software.

· Big Data Zone
Free Resource

Learn best practices according to DataOps. Download the free O'Reilly eBook on building a modern Big Data platform.

This article covers some lower level details of Kafka topic architecture. It is a continuation of the Kafka Architecture article.

This article covers Kafka Topic’s Architecture with a discussion of how partitions are used for fail-over and parallel processing.

Kafka Topics, Logs, and Partitions

Recall that a Kafka topic is a named stream of records. Kafka stores topics in logs. A topic log is broken up into partitions. Kafka spreads log’s partitions across multiple servers or disks. Think of a topic as a category, stream name or feed.

Topics are inherently published and subscribe style messaging. A Topic can have zero or many subscribers called consumer groups. Topics are broken up into partitions for speed, scalability, and size.

Kafka Topic Partitions

Kafka breaks topic logs up into partitions. A record is stored on a partition usually by record key if the key is present and round-robin if the key is missing (default behavior). The record key, by default, determines which partition a producer sends the record.

Kafka uses partitions to scale a topic across many servers for producer writes. Also, Kafka also uses partitions to facilitate parallel consumers. Consumers consume records in parallel up to the number of partitions.

The order guaranteed per partition. If partitioning by key then all records for the key will be on the same partition which is useful if you ever have to replay the log. Kafka can replicate partitions to multiple brokers for failover.

Kafka Topic Log Partition’s Ordering and Cardinality

Kafka maintains record order only in a single partition. A partition is an ordered, immutable record sequence. Kafka continually appended to partitions using the partition as a structured commit log. Records in partitions are assigned sequential id number called the offset. The offset identifies each record location within the partition. Topic partitions allow Kafka log to scale beyond a size that will fit on a single server. Topic partitions must fit on servers that host it, but topics can span many partitions hosted on many servers. Also, topic partitions are a unit of parallelism - a partition can only be worked on by one consumer in a consumer group at a time. Consumers can run in their own process or their own thread. If a consumer stops, Kafka spreads partitions across the remaining consumer in the same consumer group.

Kafka Architecture: Topic Partition Layout and Offsets

Kafka Architecture: Topic Partition Layout and Offsets Diagram

Kafka Topic Partition Replication

Kafka can replicate partitions across a configurable number of Kafka servers which is used for fault tolerance. Each partition has a leader server and zero or more follower servers. Leaders handle all read and write requests for a partition.
Followers replicate leaders and take over if the leader dies. Kafka uses also uses partitions for parallel consumer handling within a group. Kafka distributes topic log partitions over servers in the Kafka cluster. Each server handles its share of data and requests by sharing partition leadership.

Replication: Kafka Partition Leaders, Followers, and ISRs

Kafka chooses one broker’s partition’s replicas as leader using ZooKeeper.
The broker that has the partition leader handles all reads and writes of records for the partition. Kafka replicates writes to the leader partition to followers (node/partition pair). A follower that is in-sync is called an ISR (in-sync replica). If a partition leader fails, Kafka chooses a new ISR as the new leader.

Replicating to Partition 0

Kafka Architecture: Kafka Replication - Replicating to Partition 0

The record is considered “committed” when all ISRs for partition wrote to their log. Only committed records are readable from consumer. Another partition can be owned by another leader on another Kafka Broker.

Replicating to Partition 1

Kafka Architecture: Kafka Replication - Replicating to Partition 1

Kafka Topic Architecture in Review

What Is an ISR?

An ISR is an in-sync replica. If a leader fails, an ISR is picked to be a new leader.

How Does Kafka Scale Consumers?

Kafka scales consumers by partition such that each consumer gets its share of partitions. A consumer can have more than one partition, but a partition can only be used by one consumer in a consumer group at a time. If you only have one partition, then you can only have one consumer.

What Are Leaders? Followers?

Leaders perform all reads and writes to a particular topic partition. Followers replicate leaders.

How Does Kafka Perform Failover for Consumers?

If a consumer in a consumer group dies, the partitions assigned to that consumer is divided up amongst the remaining consumers in that group.

How Does Kafka Perform Failover for Brokers?

If a broker dies, then Kafka divides up leadership of its topic partitions to the remaining brokers in the cluster.

Please continue reading about Kafka Architecture. The next article covers Kafka Producer Architecture with a discussion of how partitions are picked for records.


Jean-Paul Azar works at Cloudurable. Cloudurable provides Kafka trainingKafka consultingKafka support and helps setting up Kafka clusters in AWS.

Find the perfect platform for a scalable self-service model to manage Big Data workloads in the Cloud. Download the free O'Reilly eBook to learn more.

Topics:
kafka ,pipelines ,integration ,parallel processing

Published at DZone with permission of Jean-Paul Azar. See the original article here.

Opinions expressed by DZone contributors are their own.

{{ parent.title || parent.header.title}}

{{ parent.tldr }}

{{ parent.urlSource.name }}