Kafka¶
What is Kafka?¶
Kafka is a distributed event streaming platform that allows applications to publish, store, and consume streams of data in real-time or batch mode. It is designed to handle continuous streams of events or records by functioning as a distributed commit log, where data is written sequentially and can be read independently by multiple consumers.
Kafka follows a publish-subscribe model where producers write data to topics divided into partitions, and consumers pull data from those partitions at their own pace. Kafka ensures that data flows reliably between producers and consumers, with fault-tolerant replication and durable storage to prevent data loss.
At its heart, Kafka provides three core functionalities: 1. Message Streaming: Enabling systems to send and receive continuous streams of data asynchronously. 2. Durable Storage: Persisting messages on disk, ensuring data is not lost even in case of failures. 3. Distributed Processing: Allowing data to be partitioned and processed across multiple servers for scalability and fault tolerance.
Core Components and Keywords¶
Component / Keyword | Description |
---|---|
Topic | A logical channel for data, used to categorize messages. Topics are divided into partitions for parallelism. |
Partition | A segment of a topic that stores messages in a log structure. It ensures parallel processing. Each partition contains messages with offsets to track their position. |
Producer | A client or application that publishes messages to a Kafka topic. Producers can distribute messages across partitions. |
Consumer | A client or application that subscribes to Kafka topics and reads messages from partitions at their own pace. |
Broker | A Kafka server that stores messages, handles requests, and coordinates with other brokers in a Kafka cluster. |
Kafka Cluster | A group of multiple Kafka brokers working together to provide scalability and fault tolerance. |
Zookeeper / KRaft | Zookeeper is used for metadata management and leader election in older Kafka versions. Newer versions replace Zookeeper with KRaft (Kafka Raft) for native metadata management. |
Offset | A unique identifier for each message within a partition, representing its position in the log. Consumers use offsets to track the messages they have processed. |
Replication | Kafka duplicates partitions across multiple brokers to ensure fault tolerance and data availability. |
Leader Partition | The primary replica of a partition that handles all reads and writes for that partition. Other replicas act as followers. |
Follower Partition | A copy of the leader partition that replicates its data. If the leader fails, a follower can take over. |
Consumer Group | A group of consumers sharing the same group ID, ensuring that each partition is consumed by only one member of the group at any given time. |
In-Sync Replica (ISR) | A replica that is up-to-date with the leader partition. Kafka promotes an ISR as the new leader if the current leader fails. |
Acknowledgments (ACKs) | A producer configuration that defines when a message is considered successfully sent (e.g., only after being replicated to all followers). |
Retention Policy | A configuration that determines how long Kafka retains messages before deleting or compacting them. Messages can be removed based on time or size limits. |
Log Compaction | A process that keeps only the latest version of a key within a topic, useful for data clean-up and long-term storage. |
Controller | A designated broker responsible for managing partition leadership and cluster rebalancing. |
Kafka Streams | A lightweight client library used for processing and analyzing data streams directly within Kafka. |
Kafka Connect | A framework for integrating Kafka with external systems by providing connectors for data ingestion and extraction. |
How Kafka Achieves High Scalability ?¶
Kafka’s design is fundamentally scalable, allowing it to handle millions of events per second efficiently. It achieves this by leveraging distributed architecture, partitioning, horizontal scaling, and several load balancing strategies. Let’s explore the mechanisms, architecture, and workflows that enable Kafka to scale end-to-end.
1. Partitioning: The Foundation of Scalability¶
- Partitioning is the primary mechanism through which Kafka ensures horizontal scalability.
- Each topic in Kafka is divided into partitions, and each partition stores an ordered, immutable sequence of messages.
- Producers can write messages to different partitions in parallel, improving throughput.
- Consumers in a consumer group process data independently from different partitions, spreading the load.
Partition Key and Load Distribution¶
- A partition key can be used to determine to which partition a message will be routed.
- If no key is provided, Kafka defaults to round-robin distribution across partitions.
- Partitioning ensures both scalability and ordering guarantees within a partition.
Impact of More Partitions¶
- More partitions increase the parallelism, allowing more producers and consumers to work simultaneously.
- However, each additional partition incurs overhead (memory, I/O, metadata), so the partition count must be tuned carefully.
2. Distributed Broker Architecture¶
- Kafka is deployed as a cluster of brokers, with each broker responsible for multiple partitions.
- As Kafka clusters grow, more brokers can be added to distribute the load.
- Kafka ensures no single broker becomes a bottleneck by distributing topic partitions across all brokers.
Partition Replication Across Brokers¶
- Kafka supports partition replication for fault tolerance. Each partition has a leader replica on one broker and follower replicas on other brokers.
- This means even if one broker handles the leader partition, the workload is distributed across brokers with followers syncing in the background.
3. Horizontal Scaling: Add More Brokers or Partitions¶
- Kafka clusters scale horizontally, meaning new brokers can be added without downtime.
- As traffic or data volume increases, administrators can:
- Add brokers: Redistribute partitions across the new brokers.
- Add partitions: Redistribute topics into new partitions.
Scaling the Producer and Consumer Layers¶
- Kafka’s producers and consumers are stateless, meaning they can scale horizontally.
- Producers write to multiple partitions in parallel.
-
Consumers in a consumer group split the partitions among themselves for parallel processing.
-
When Kafka detects new brokers, partition leadership rebalances automatically to evenly distribute the load across brokers.
4. Producer Scalability: Load Balancing and Batch Processing¶
- Kafka producers scale by distributing messages across partitions. Producers can use:
- Round-robin partitioning: Evenly distributing messages to all partitions.
-
Key-based partitioning: Ensuring messages with the same key always go to the same partition for order guarantees.
-
Kafka producers also use batching to group multiple messages into a single request, reducing network overhead and increasing throughput.
- Asynchronous sends allow producers to continue processing without waiting for acknowledgments, further improving scalability.
5. Consumer Scalability: Consumer Groups and Load Sharing¶
- Kafka scales consumer applications by using consumer groups. A consumer group is a set of consumers working together to process messages from a topic.
- Each partition in a topic is consumed by only one consumer in the group at any time, ensuring no two consumers in the same group read the same data.
- More consumers can be added to a group to share the load, provided the number of partitions is larger than or equal to the number of consumers.
Rebalancing Consumers¶
- Kafka dynamically adjusts when new consumers are added or removed from a consumer group by rebalancing partitions across the group. This ensures efficient data processing, even in a changing environment.
6. Efficient Load Balancing and Rebalancing Mechanisms¶
- Kafka uses partition rebalancing when brokers or consumers are added or removed.
- The controller broker manages the partition leader assignment and rebalancing.
- During rebalancing, Kafka ensures minimal disruption by only migrating the necessary partitions, ensuring continuous data flow.
Sticky Partitioning Strategy¶
- Kafka optimizes load balancing by sticky partition assignments, meaning it tries to keep partitions assigned to the same consumer or broker to minimize churn.
7. Network Optimization and Zero-Copy Technology¶
- Kafka relies heavily on zero-copy techniques to achieve high throughput.
- Using sendfile() system calls, Kafka can move data directly from the disk to the network buffer, reducing CPU usage and latency.
- This makes Kafka suitable for transferring large volumes of data with minimal resource consumption.
8. Broker-Level Optimizations and Compression¶
- Kafka brokers use I/O batching and compression to improve scalability:
- Batching: Brokers accumulate messages in memory and flush them to disk in batches, reducing disk I/O operations.
-
Compression: Messages can be compressed (e.g., with Gzip or Snappy) to reduce the amount of data transferred over the network.
-
Kafka also makes use of page cache to keep frequently accessed data in memory, further optimizing performance.
9. Kafka Controller and Partition Leadership¶
- Kafka assigns one broker as the controller broker, which handles:
- Leader election: Assigning leader partitions across brokers.
-
Metadata updates: Ensuring all brokers have consistent metadata.
-
Dynamic partition leadership reassignment ensures that even when new brokers are added or partitions are migrated, Kafka distributes workload evenly.
10. Handling Backpressure for Scalability¶
- Kafka producers and consumers operate asynchronously, which allows the system to handle backpressure without affecting throughput.
- Producers continue writing to partitions regardless of consumer lag.
- Consumers can process messages at their own pace using offsets, without impacting producers or causing system slowdowns.
11. Configuring for High Scalability: Tuning Parameters¶
- Kafka clusters can be optimized for scalability with various tuning parameters:
- Partition count: More partitions allow more parallel processing.
- Replication factor: Higher replication ensures fault tolerance, though it also adds overhead.
- Batch size: Larger batches increase throughput but introduce some latency.
- Compression: Compressing messages reduces network load but requires more CPU.
Summary of Kafka’s High Scalability Mechanisms¶
Aspect | How Kafka Scales |
---|---|
Partitioning | Divides topics into partitions for parallelism. |
Distributed Brokers | Workload spread across multiple brokers. |
Horizontal Scaling | New brokers or partitions can be added dynamically. |
Producer Parallelism | Producers write to multiple partitions concurrently. |
Consumer Groups | Consumers share partitions for parallel processing. |
Rebalancing | Redistributes workload when brokers/consumers change. |
Batching & Compression | Reduces I/O and network overhead. |
Zero-Copy Technology | Efficient data transfer with low CPU usage. |
How Kafka Achieves High Availability and Fault Tolerance ?¶
Kafka’s design for high availability and fault tolerance centers on replication, leader election, distributed brokers, and self-healing mechanisms. Together, these mechanisms ensure Kafka can handle hardware, software, and network failures, while maintaining data integrity, durability, and continuity.
1. Partition Replication – Foundation of HA and FT¶
- Kafka divides topics into partitions and replicates each partition across multiple brokers.
- For each partition, there is a leader replica (primary copy) and follower replicas.
- Replication Factor (set per topic) determines the number of copies for each partition. For example, a replication factor of 3 means one leader and two followers per partition.
How Replication Ensures HA and FT:¶
- Leader-follower model: Only the leader replica handles read and write requests. Followers passively replicate the leader’s data.
- Automatic failover: If the leader broker fails, one of the followers (from the in-sync replica set, or ISR) is promoted as the new leader, ensuring the partition remains available.
- This setup ensures continuous data availability and minimal downtime when individual brokers fail.
2. Leader Election and Failover Mechanism¶
- Kafka assigns one replica as the leader and others as followers for each partition.
- Zookeeper (or KRaft) coordinates leader election and keeps metadata consistent across brokers.
Leader Election Process During Failures:¶
- Detection: Zookeeper (or KRaft) detects a failed broker.
- Election: The controller broker initiates a leader election for partitions on the failed broker.
- Promotion: An in-sync follower (replica that’s fully up-to-date) is promoted to leader.
- Metadata update: Kafka updates cluster metadata to reflect the new leader, ensuring clients redirect requests to the new leader.
This automated and rapid leader election ensures Kafka remains operational with minimal interruption.
3. In-Sync Replicas (ISR) – Ensuring Data Integrity¶
- Kafka tracks in-sync replicas (ISR), which are replicas fully synchronized with the leader.
- Kafka only allows ISR replicas to be promoted to leader during failover, ensuring the new leader has the latest data.
Role of ISR in Minimizing Data Loss:¶
- Kafka only acknowledges writes to producers if messages are successfully replicated to all ISR replicas (if
acks=all
). - If a replica falls behind, it is temporarily removed from ISR until it catches up, ensuring only fully synced replicas can be promoted to leader.
4. Controller Broker – Managing HA and FT Orchestration¶
- Kafka designates one broker as the controller, responsible for partition leadership management and rebalancing operations.
- The controller broker coordinates leader elections and metadata updates across the cluster.
- If the controller broker fails, a new one is elected to maintain continuity without disrupting operations.
5. Distributed Brokers – Avoiding Single Points of Failure¶
- Kafka distributes partitions and their replicas across multiple brokers, ensuring no single broker failure causes data loss.
- Each broker holds different partitions and replicas, providing load balancing and data redundancy.
How Broker Distribution Improves HA and FT:¶
- Kafka ensures that no two replicas of the same partition are hosted on the same broker, minimizing risk during a broker failure.
- If a broker fails, other brokers hosting replicas continue serving data, maintaining availability.
- Kafka dynamically rebalances partitions and replicas across brokers when a broker joins or leaves the cluster.
6. Acknowledgment Policies (ACKs) – Ensuring Data Durability¶
Kafka provides acknowledgment modes for tuning data durability against performance. These settings control when a message is considered “successfully written”:
- acks=0: Producer does not wait for any acknowledgment (fast, but no durability).
- acks=1: Leader acknowledges after writing to local storage (data loss possible if the leader fails).
- acks=all: Leader waits until message replication to all ISR replicas, maximizing durability.
Role of ACKs in Fault Tolerance:¶
- With acks=all, Kafka ensures even if the leader fails immediately after acknowledging, the message is still replicated on follower brokers.
- This reduces data loss risk and enables safe failover to a new leader.
7. Log Compaction and Retention for Data Availability¶
Kafka employs log compaction and retention to maintain long-term data availability:
- Time-based retention: Kafka retains messages for a configurable period (e.g., 7 days).
- Size-based retention: Kafka deletes old messages once partition logs reach a certain size.
Log Compaction for Data Recovery:¶
- Log compaction keeps only the latest version of each key. This is essential for stateful applications where the latest state must always be available.
- Log compaction allows Kafka to replay and recover data even during consumer failures or recovery needs.
8. Handling Split-Brain Scenarios – Consistency Through Quorums¶
A split-brain scenario happens when a broker loses connectivity with others, risking data inconsistency.
Kafka’s Approach to Preventing Split-Brain:¶
- Kafka removes disconnected brokers from the ISR, preventing them from serving outdated data.
- Zookeeper (or KRaft) ensures that only one leader per partition exists, preventing conflicting writes.
This quorum-based replication prevents data corruption during network partitions.
9. Self-Healing and Automated Recovery¶
Kafka’s self-healing mechanisms enable it to quickly recover from broker or replica failures:
- Controller failover: If the controller broker fails, a new controller is elected automatically.
- Replica recovery: Kafka automatically adds a failed broker back to ISR once it recovers and synchronizes with the leader.
- Rebalancing: Kafka reassigns leadership of partitions to healthy brokers during rebalancing to prevent bottlenecks or potential outages.
These self-healing features maintain availability and data consistency without requiring manual intervention.
10. Multi-Datacenter Replication for Disaster Recovery¶
Kafka supports multi-datacenter replication for cross-region fault tolerance using tools like Kafka MirrorMaker.
How Multi-Cluster Replication Ensures Availability and Fault Tolerance:¶
- Kafka replicates data across geographically separate clusters to ensure disaster recovery.
- If an entire Kafka cluster becomes unavailable, the replicated cluster in another data center takes over, ensuring continuity.
11. Client-Side Handling of Failures for HA and FT¶
Kafka’s producers and consumers have built-in resilience to handle failures gracefully:
- Producers: Retry sends if a leader partition is temporarily unavailable, with updated metadata guiding them to the new leader.
- Consumers: Retry reads from a new leader if they detect a leader change, and consumer groups dynamically rebalance to share the load among available consumers.
Summary of Kafka’s High Availability and Fault Tolerance Mechanisms¶
Mechanism | How It Ensures HA and FT |
---|---|
Partition Replication | Multiple copies of data across brokers ensure availability even during broker failures. |
Leader Election | Automatically promotes a follower to leader when the leader broker fails. |
In-Sync Replicas (ISR) | Only fully synchronized replicas can be promoted to leader to prevent data loss. |
Controller Broker | Manages partition leadership and rebalancing operations, ensuring consistent cluster state. |
Distributed Brokers | Spreads data across brokers to avoid single points of failure. |
Dynamic Rebalancing | Adjusts workload across brokers and consumers to handle changes or failures smoothly. |
Acknowledgment Policies (ACKs) | Ensures data is safely replicated before acknowledging writes, reducing data loss risk. |
Log Compaction | Maintains the latest data state for recovery during consumer or application failures. |
Client-Side Recovery | Producers and consumers handle broker failures with retries and automatic rebalancing. |
Network Partition Handling | Uses Zookeeper/KRaft to prevent split-brain scenarios and ensure data consistency. |
Multi-Datacenter Replication | Provides disaster recovery and redundancy across regions. |
Kafka features Impacts on HA and FT¶
Feature/Configuration | Configuration Details | Impact on HA | Impact on FT | Explanation |
---|---|---|---|---|
Partition Replication | Set a high replication factor (e.g., 3 or 5 ) |
High | High | Ensures multiple copies of data across brokers; if the leader fails, a follower can be promoted, maintaining data availability and durability. |
In-Sync Replicas (ISR) | Use acks=all to ensure ISR sync before acknowledgments |
Moderate | High | Guarantees data consistency by ensuring messages are replicated to all ISR replicas before confirming writes, reducing data loss. |
Leader Election Mechanism | Managed by Zookeeper or KRaft for automatic failover | High | Moderate | Automatically promotes a follower to leader when the current leader fails, minimizing downtime. |
Controller Broker | Redundancy provided by re-election if the current controller fails | High | Moderate | Ensures the orchestrator of metadata and rebalancing has a backup, maintaining consistent cluster operations. |
Distributed Broker Placement | Spread partitions across brokers; no two replicas on the same broker | High | High | Reduces the risk of data unavailability and loss by preventing single points of failure. |
Rebalancing Strategy | Configure partition reassignment for balanced broker load | High | Low | Prevents overload on individual brokers, enhancing availability; this has less impact on data durability. |
Acknowledgment Policy (ACKs) | Set acks=all for highest data durability |
Low | High | Ensures writes are only confirmed after replication to all ISR replicas, reducing the risk of data loss. |
Log Compaction | Enable for compacted topics to retain latest state | Moderate | Moderate | Retains the latest state of each key, useful for stateful applications; supports recovery but doesn’t guarantee availability. |
Retention Policies | Configure time-based or size-based retention | High | Low | Maintains historical data for consumer recovery, contributing to high availability if consumers fall behind. |
Client Retry Mechanisms | Configure producer and consumer retries | High | Low | Enables producers and consumers to handle temporary broker unavailability, ensuring continuous operation. |
Consumer Group Rebalancing | Set rebalancing policies to avoid bottlenecks | High | Low | Ensures efficient load distribution among consumers, enhancing availability but minimally impacting data durability. |
Multi-Datacenter Replication | Enable with Kafka MirrorMaker or similar tools | High | High | Provides cross-region redundancy for both availability and fault tolerance, especially critical for disaster recovery. |
Backpressure Handling | Use offset tracking and monitoring | Moderate | High | Allows consumers to fall behind producers without causing data loss, enhancing fault tolerance by protecting against backpressure failures. |
Split-Brain Handling | Managed by Zookeeper/KRaft to avoid conflicting writes | Low | High | Prevents data inconsistency by ensuring only one leader exists per partition, critical for consistency in partitioned network conditions. |
Log Recovery | Enable brokers to rebuild from log segments on restart | Moderate | High | Ensures brokers can recover their state after a crash, minimizing data loss and ensuring continuity post-restart. |
Kafka’s architecture for high availability and fault tolerance ensures the system remains resilient under various failure scenarios. Through partition replication, leader election, dynamic rebalancing, and multi-datacenter replication, Kafka provides a robust infrastructure with no single points of failure and near-zero downtime, making it reliable for critical real-time data streaming and processing applications.
What Makes Kafka Unique ?¶
Append only Log-Based Architecture and High-Throughput with Low-Latency Design two of Kafka’s core features that make it unique.
1. Log-Based Architecture: The Foundation of Kafka’s Data Model¶
Kafka’s log-based architecture is what makes it fundamentally different from traditional messaging systems. It’s built on the concept of a distributed, partitioned, and immutable log, allowing Kafka to scale, preserve data ordering, and enable consumers to replay data as needed. Here’s a deep dive into what this architecture entails and why it’s special.
How Log-Based Architecture Works¶
- Topics and Partitions as Logs:
- In Kafka, each topic is split into partitions, and each partition acts as an independent log.
- Within a partition, messages are appended sequentially in an ordered, immutable fashion, with each message assigned an offset (a unique, incremental ID).
-
Offsets serve as pointers to each message’s position within the log, making it easy for consumers to track their progress.
-
Immutable Log Storage:
- Messages in each partition are stored as an append-only log—once written, messages cannot be modified or deleted (unless retention policies specify otherwise).
-
This immutability provides consistency and durability, as every message has a fixed position within the partition log.
-
Replayable and Persistent Data:
- Kafka’s log structure allows consumers to replay messages from any offset within the retention period. Consumers can reread data for recovery, reprocessing, or analytics without impacting other consumers or producers.
- Since Kafka retains messages based on time or size-based retention policies, consumers can pick up data where they left off or reprocess older data without affecting ongoing data flows.
Why Log-Based Architecture Makes Kafka Unique¶
-
Efficient Ordering: Kafka maintains strict ordering within each partition since messages are written sequentially. This guarantees that messages within a partition are processed in the order they were produced, which is essential for applications like transaction logs and real-time event processing.
-
Parallelism and Scalability: By dividing topics into multiple partitions, Kafka enables horizontal scalability. Each partition can be processed independently by separate consumers, allowing for parallel data processing and high throughput across large datasets.
-
Fault Tolerance and Durability: The log-based structure, combined with replication (having multiple copies of each partition on different brokers), ensures that data remains durable and available even in the event of broker failures.
Kafka’s Log-Based Architecture in Practice¶
- Data Lake Ingestion: Kafka’s log-based architecture makes it perfect for acting as a highly durable source of truth in a data lake architecture, where data can be stored in raw form and later reprocessed.
- Audit Trails and Event Sourcing: Kafka’s immutable, ordered log allows applications to store audit logs or event-sourced data with an unalterable history, which is essential for compliance and traceability.
- Consumer Independence: Because consumers track their own offsets, they can read at their own pace, without being forced to process data in real-time. This enables flexibility for different applications (e.g., one consumer could process events in real-time, while another could do batch processing from the same log).
2. High-Throughput with Low-Latency Design¶
Kafka’s design is optimized for handling millions of events per second with minimal delay, even under heavy load. This high throughput and low latency are achieved through a combination of disk I/O optimizations, data compression, and efficient network handling. Let’s explore these in detail.
Key Components of Kafka’s High-Throughput, Low-Latency Design¶
- Sequential Disk I/O:
- Kafka writes messages to disk sequentially rather than performing random writes. This significantly reduces seek time, as the disk head doesn’t need to jump around to write or read data.
-
Sequential writes take advantage of modern disks’ ability to handle high-speed sequential I/O, especially in SSDs, allowing Kafka to process large volumes of data quickly.
-
Page Cache Usage:
- Kafka leverages the OS’s page cache to keep frequently accessed data in memory. By utilizing page cache, Kafka avoids direct disk reads for recently accessed data, reducing read latency.
-
For producers writing data, Kafka batches messages in memory before flushing them to disk, improving throughput by reducing the number of disk write operations.
-
Zero-Copy Data Transfer:
- Kafka uses zero-copy technology, specifically the sendfile() system call, to transfer data directly from disk to network sockets without additional memory copies.
-
This allows Kafka to handle network I/O with minimal CPU usage, reducing latency and increasing throughput, especially for large messages.
-
Batching and Compression:
- Kafka batches multiple messages into a single disk write or network packet, minimizing the number of I/O operations required.
-
Batching not only increases efficiency but also improves compression rates by reducing network overhead. Kafka supports Gzip, Snappy, and LZ4 compression, reducing data size and thus speeding up data transfer.
-
Network Optimization for Low Latency:
- Kafka’s network layer is designed to handle high-speed data transfer between producers, brokers, and consumers. Kafka supports asynchronous data processing, allowing producers to continue sending messages without waiting for each acknowledgment, which further reduces latency.
- Kafka brokers are stateful, meaning they store partition offsets and metadata locally, reducing the need to rely on external systems for routing information. This minimizes delays in routing messages to the correct consumers.
Why Kafka’s Throughput and Latency Make It Special¶
- Handling Massive Data Streams: Kafka’s design allows it to ingest millions of events per second across hundreds or thousands of partitions, making it highly scalable and capable of handling enterprise-scale data streams in real-time.
- Low-Latency Processing: Kafka’s ability to process data with minimal delay is essential for real-time applications where low latency is critical, such as fraud detection, real-time analytics, and financial trading.
- Reduced Overhead for Producers and Consumers: By using batching, compression, and zero-copy technology, Kafka minimizes the load on producers and consumers, allowing them to process data efficiently without overwhelming system resources.
Kafka’s High-Throughput, Low-Latency Design in Practice¶
- Real-Time Analytics and Monitoring: Kafka’s ability to process data in near real-time enables real-time monitoring and analytics applications. Organizations can monitor user activity, system performance, or financial transactions in real-time with minimal delay.
- Microservices Communication: In microservices architectures, Kafka acts as a high-throughput backbone for event-driven communication between services. Kafka’s low-latency design ensures that events are delivered quickly, enabling responsive and reactive service interactions.
- Data Pipelines and ETL: Kafka’s high throughput supports data pipeline workloads where large volumes of data need to be extracted, transformed, and loaded across different systems. Kafka can handle ETL pipelines with minimal latency, making it suitable for streaming data processing.
Summary of Kafka’s Unique Log-Based Architecture and High-Throughput Design¶
Aspect | Log-Based Architecture | High-Throughput with Low-Latency Design |
---|---|---|
Core Principle | Immutable, append-only logs for each partition. | Optimized for sequential writes, memory management, and efficient data transfer. |
Data Storage | Messages are stored as ordered, append-only logs in each partition, ensuring data immutability and ordering. | Batching, compression, and zero-copy transfer ensure efficient storage and minimal latency in data handling. |
Fault Tolerance | Replication across brokers enables resilience against broker failures, as partitions can be replayed and failover to available replicas. | Brokers use page cache and zero-copy I/O to keep data transfer reliable under heavy load, even in the case of high data volumes. |
Parallelism and Scalability | Partitions allow Kafka to scale horizontally, providing parallel processing across producers and consumers, with strict ordering within each partition. | Sequential I/O and batching enable Kafka to handle high-throughput workloads, supporting massive parallelism across clients. |
Use Cases | Event sourcing, state recovery, and audit logs where ordered, immutable data is essential. | Real-time monitoring, analytics, and streaming data pipelines that require fast ingestion and minimal latency. |
Recommended Kafka Settings¶
Here are the organized into individual tables based on their sections. Each table provides a summary of the key settings along with recommended values and explanations.
1. Broker-Level Settings¶
Setting | Recommended Value | Purpose |
---|---|---|
replication.factor |
3 |
Ensures redundancy and fault tolerance, allowing partition recovery if a broker fails. |
min.insync.replicas |
2 |
Reduces data loss risk by ensuring at least two replicas acknowledge writes. |
num.partitions |
6 (or based on load) |
Balances throughput and scalability; more partitions allow greater parallel processing. |
log.retention.hours |
168 (7 days) |
Controls how long messages are retained; suitable for standard processing and replay needs. |
log.segment.bytes |
1 GB |
Manages the segment size for optimal disk usage and performance in log rolling. |
log.cleanup.policy |
delete or compact |
delete for default; compact for retaining only the latest version of each key. |
compression.type |
producer , snappy , or lz4 |
Saves bandwidth and improves throughput, especially under high data volume conditions. |
2. Producer-Level Settings¶
Setting | Recommended Value | Purpose |
---|---|---|
acks |
all |
Ensures that all in-sync replicas acknowledge writes, increasing reliability. |
retries |
Integer.MAX_VALUE |
Handles transient network issues by allowing indefinite retries, preventing message loss. |
retry.backoff.ms |
100 |
Introduces a pause between retries, avoiding retry flooding and improving stability. |
enable.idempotence |
true |
Prevents duplicate messages by ensuring exactly-once semantics in data delivery. |
batch.size |
32 KB |
Enhances throughput by accumulating records into batches before sending them. |
linger.ms |
5 |
Small linger time allows batches to fill, reducing network overhead without delaying sends. |
compression.type |
snappy or lz4 |
Compresses data to reduce payload size, saving bandwidth and reducing transfer time. |
3. Consumer-Level Settings¶
Setting | Recommended Value | Purpose |
---|---|---|
auto.offset.reset |
earliest |
Ensures consumers start reading from the beginning if no offset is committed. |
max.poll.records |
500 |
Controls the batch size per poll, balancing throughput and processing time. |
session.timeout.ms |
30,000 |
Provides enough time for consumers to process data without triggering unnecessary rebalances. |
heartbeat.interval.ms |
10,000 |
Sets the interval for heartbeat checks within the session timeout, reducing rebalance triggers. |
fetch.min.bytes |
1 MB |
Improves fetch efficiency by waiting for a minimum data size before retrieving. |
fetch.max.bytes |
50 MB |
Enables large batch sizes for high-throughput consumers, reducing network calls. |
enable.auto.commit |
false |
Disables automatic offset commits, allowing applications to commit only after processing. |
4. Cluster-Level Settings for High Availability and Fault Tolerance¶
Setting | Recommended Value | Purpose |
---|---|---|
min.insync.replicas |
2 |
Ensures at least two replicas must be in sync, providing better durability. |
controlled.shutdown.enable |
true |
Enables controlled shutdown, allowing brokers to gracefully transition leadership. |
unclean.leader.election.enable |
false |
Prevents out-of-sync replicas from being elected as leaders, protecting data consistency. |
num.network.threads |
8 |
Increases concurrency for network traffic, supporting high-throughput applications. |
num.io.threads |
8 |
Increases I/O concurrency, allowing efficient data transfer under heavy load. |
num.replica.fetchers |
4 |
Enhances replication speed by allowing multiple fetcher threads for synchronizing replicas. |
5. Zookeeper Settings¶
Setting | Recommended Value | Purpose |
---|---|---|
zookeeper.session.timeout.ms |
18,000 |
Prevents frequent Zookeeper disconnections during high loads, stabilizing metadata handling. |
zookeeper.connection.timeout.ms |
6,000 |
Ensures reliable connections to Zookeeper, reducing the likelihood of leader election issues. |
6. KRaft (Kafka Raft) Settings¶
Setting | Recommended Value | Purpose |
---|---|---|
process.roles |
broker,controller |
Defines the roles of Kafka nodes. A KRaft cluster typically has combined broker and controller roles, but can be split if desired. |
controller.quorum.voters |
List of controllers | Specifies the list of controller nodes in the form nodeID@hostname:port , where each entry represents a voter in the Raft consensus group. |
controller.listener.names |
CONTROLLER |
Designates the listener name for inter-controller communication in the Raft quorum. |
controller.heartbeat.interval.ms |
2,000 |
Sets the interval between heartbeats for controller nodes, ensuring they stay connected and responsive within the Raft quorum. |
controller.metrics.sample.window.ms |
30,000 |
Configures the window size for collecting metrics, helping to monitor Raft performance over time. |
controller.log.dirs |
/path/to/controller/logs |
Specifies the directory where controller logs are stored. It’s best to use a dedicated disk for controller logs to avoid I/O contention with brokers. |
metadata.log.segment.bytes |
1 GB |
Controls the segment size for metadata logs, managing disk usage and log rolling frequency for metadata in KRaft mode. |
metadata.log.retention.bytes |
-1 (unlimited) |
Configures metadata log retention based on disk space, allowing infinite retention by default. Adjust based on available storage. |
metadata.log.retention.ms |
604,800,000 (7 days) |
Retains metadata for a set duration; typically configured for a week to enable rollback in case of issues. |
controller.socket.timeout.ms |
30,000 |
Sets the timeout for controller-to-controller connections, ensuring stability during network issues. |
leader.imbalance.check.interval.seconds |
300 (5 minutes) |
Defines the interval at which the controller checks for leader imbalance, helping to maintain even load distribution across brokers. |
Reference Links:
https://www.hellointerview.com/learn/system-design/deep-dives/kafka