<?xml version="1.0" encoding="UTF-8"?><rss xmlns:dc="http://purl.org/dc/elements/1.1/" xmlns:content="http://purl.org/rss/1.0/modules/content/" xmlns:atom="http://www.w3.org/2005/Atom" version="2.0" xmlns:cc="http://cyber.law.harvard.edu/rss/creativeCommonsRssModule.html">
    <channel>
        <title><![CDATA[Stories by NootCode on Medium]]></title>
        <description><![CDATA[Stories by NootCode on Medium]]></description>
        <link>https://medium.com/@nootcode?source=rss-020c641bf0f5------2</link>
        <image>
            <url>https://cdn-images-1.medium.com/fit/c/150/150/1*d91gHDwcsmyHALDiFY5LjA.png</url>
            <title>Stories by NootCode on Medium</title>
            <link>https://medium.com/@nootcode?source=rss-020c641bf0f5------2</link>
        </image>
        <generator>Medium</generator>
        <lastBuildDate>Sat, 23 May 2026 16:24:17 GMT</lastBuildDate>
        <atom:link href="https://medium.com/@nootcode/feed" rel="self" type="application/rss+xml"/>
        <webMaster><![CDATA[yourfriends@medium.com]]></webMaster>
        <atom:link href="http://medium.superfeedr.com" rel="hub"/>
        <item>
            <title><![CDATA[What is ISR (In-Sync Replicas) in Kafka?]]></title>
            <link>https://medium.com/@nootcode/what-is-isr-in-sync-replicas-in-kafka-8074c9f2c270?source=rss-020c641bf0f5------2</link>
            <guid isPermaLink="false">https://medium.com/p/8074c9f2c270</guid>
            <category><![CDATA[apache-kafka]]></category>
            <category><![CDATA[kafka]]></category>
            <category><![CDATA[kafka-isr]]></category>
            <dc:creator><![CDATA[NootCode]]></dc:creator>
            <pubDate>Thu, 20 Feb 2025 08:29:38 GMT</pubDate>
            <atom:updated>2025-02-20T08:29:38.919Z</atom:updated>
            <content:encoded><![CDATA[<h3>What is ISR?</h3><p>ISR (In-Sync Replicas) is a fundamental concept in Kafka that represents a set of replicas that are in sync with the leader partition. This set includes the leader replica itself and all follower replicas that are actively syncing with the leader. The ISR mechanism is crucial for ensuring high availability and data consistency in Kafka.</p><h3>How ISR Works</h3><h3>1. Basic Concepts</h3><p>Each partition’s ISR list contains two types of replicas:</p><ul><li>Leader Replica: The primary replica that handles all read and write requests</li><li>Follower Replicas: Secondary replicas that replicate data from the leader</li></ul><p>Key characteristics of the ISR mechanism:</p><ul><li>ISR membership is dynamic and automatically adjusts based on replica sync status</li><li>Only replicas in the ISR are eligible to become the new leader</li><li>With acks=all, writes are considered successful only after all ISR replicas confirm</li><li>Kafka uses ZooKeeper to persist and synchronize ISR changes</li></ul><p>Here’s a concrete example: Consider a partition with 3 replicas:</p><ul><li>Replica on Broker-1 is the Leader</li><li>Replica on Broker-2 is in sync (lag &lt; 5s)</li><li>Replica on Broker-3 is lagging (20s behind)</li></ul><p>In this case, the ISR list only includes replicas on Broker-1 and Broker-2. The replica on Broker-3 is temporarily removed from ISR. Once it catches up, it will automatically rejoin the ISR list.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/0*JFU07gG583OkXMJE.png" /></figure><h3>2. ISR Membership Rules</h3><p>Requirements to join ISR:</p><ul><li>Follower’s message lag must be within acceptable limits (controlled by replica.lag.time.max.ms)</li><li>Follower must maintain active fetch requests to the leader</li></ul><p>Conditions for removal from ISR:</p><ul><li>Replica falls behind beyond the allowed time threshold</li><li>Broker hosting the replica fails</li><li>Replica encounters synchronization errors</li></ul><h3>ISR Configuration</h3><h3>1. Core Settings</h3><pre># Maximum allowed time for replica lag<br>replica.lag.time.max.ms=10000<br><br># Minimum number of in-sync replicas required<br>min.insync.replicas=2<br><br># Whether to allow non-ISR replicas to become leader<br>unclean.leader.election.enable=false</pre><h3>2. Producer Settings</h3><pre># Ensure writes to all ISR replicas<br>acks=all<br><br># Number of retry attempts<br>retries=3</pre><h3>ISR in Practice</h3><h3>1. Data Reliability</h3><p>When producers use acks=all:</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/0*ke25fsm-XPqZ_Y2k.png" /></figure><h3>2. Leader Election</h3><p>When the leader replica fails:</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/0*WFYSYahliG7-a4wa.png" /></figure><h3>Common Issues and Solutions</h3><h3>1. Frequent ISR Shrinking</h3><p>Common causes:</p><ul><li>Network latency spikes leading to sync timeouts</li><li>High system load on follower nodes</li><li>Extended GC pauses disrupting sync processes</li><li>Disk I/O bottlenecks affecting write performance</li></ul><p>Recommended solutions:</p><p><strong>Parameter Optimization</strong></p><ul><li>Increase replica.lag.time.max.ms for networks with higher latency</li><li>Adjust replica.fetch.wait.max.ms based on network characteristics</li><li>Increase replica.fetch.max.bytes to optimize sync efficiency</li></ul><p><strong>Follower Performance Optimization</strong></p><ul><li>Monitor and address CPU usage spikes</li><li>Optimize memory allocation and usage</li><li>Consider SSD storage for improved I/O performance</li><li>Isolate Kafka brokers from other resource-intensive services</li></ul><p><strong>JVM Optimization</strong></p><ul><li>Select an appropriate GC algorithm for the workload</li><li>Optimize heap size configuration</li><li>Implement comprehensive GC monitoring</li></ul><p><strong>Network Optimization</strong></p><ul><li>Ensure adequate network bandwidth</li><li>Monitor and address network latency issues</li></ul><h3>2. Data Loss Risks</h3><p>Critical scenarios:</p><ul><li>ISR set reduction to a single replica</li><li>Enabled unclean leader election</li><li>Network partitioning events</li><li>Unexpected traffic surges</li></ul><p>Prevention strategies:</p><p><strong>Replica Management</strong></p><ul><li>Maintain minimum of 2 in-sync replicas</li><li>Disable unclean leader election</li><li>Implement regular replica status monitoring</li><li>Deploy balanced replica distribution</li></ul><p><strong>Monitoring Strategy</strong></p><ul><li>Implement ISR size change monitoring</li><li>Track replica synchronization metrics</li></ul><p><strong>Capacity Management</strong></p><ul><li>Maintain adequate resource headroom</li><li>Monitor cluster metrics</li><li>Plan proactive scaling</li></ul><h3>Summary</h3><p>The ISR mechanism is fundamental to Kafka’s reliability and high availability. Successful implementation requires balancing data durability with performance requirements. Each deployment should be tuned according to specific use cases and operational requirements.</p><p>Related Topics:</p><ul><li><a href="https://www.nootcode.com/knowledge/en/kafka-what-is-topic-and-partition">What are Topics and Partitions in Kafka?</a></li><li><a href="https://www.nootcode.com/knowledge/en/kafka-consumer-rebalance">How Does Kafka Consumer Rebalancing Work?</a></li><li><a href="https://www.nootcode.com/problem-sets/message-queue-essentials">Practice Message Queue Interview Questions Like LeetCode</a></li></ul><p><em>Originally published at </em><a href="https://www.nootcode.com/knowledge/en/kafka-isr"><em>https://www.nootcode.com</em></a><em>.</em></p><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=8074c9f2c270" width="1" height="1" alt="">]]></content:encoded>
        </item>
        <item>
            <title><![CDATA[What is Zero Copy in Kafka?]]></title>
            <link>https://medium.com/@nootcode/what-is-zero-copy-in-kafka-7d6a4d1b6bb7?source=rss-020c641bf0f5------2</link>
            <guid isPermaLink="false">https://medium.com/p/7d6a4d1b6bb7</guid>
            <category><![CDATA[apache-kafka]]></category>
            <category><![CDATA[kafka]]></category>
            <category><![CDATA[zero-copy]]></category>
            <dc:creator><![CDATA[NootCode]]></dc:creator>
            <pubDate>Mon, 27 Jan 2025 02:14:13 GMT</pubDate>
            <atom:updated>2025-01-27T02:15:11.034Z</atom:updated>
            <content:encoded><![CDATA[<h3>What is Zero Copy?</h3><p>Zero Copy is a technique that eliminates unnecessary data copying between memory regions by the CPU. In Kafka, this technology optimizes data transfer from disk files to the network, reducing redundant data copies and improving transmission efficiency.</p><h3>Traditional Copy vs. Zero Copy</h3><h4>Traditional Copy Process</h4><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/0*QFGn50EnvVtTQaDA.png" /></figure><p>The traditional data copy process involves 4 copies and 4 context switches:</p><ol><li>Disk → Kernel Buffer</li><li>Kernel Buffer → Application Buffer</li><li>Application Buffer → Socket Buffer</li><li>Socket Buffer → NIC Buffer</li></ol><h4>Zero Copy Process</h4><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/0*r3oMU12upBgvyXhN.png" /></figure><p>Zero Copy requires only 2 copies and 2 context switches:</p><ol><li>Disk → Kernel Buffer</li><li>Kernel Buffer → NIC Buffer</li></ol><h3>Performance Benefits of Zero Copy</h3><p><strong>Reduced CPU Copy Operations</strong></p><ul><li>Decreased from 4 copies to 2</li><li>Lower CPU utilization</li></ul><p><strong>Fewer Context Switches</strong></p><ul><li>Reduced from 4 switches to 2</li><li>Decreased system call overhead</li></ul><p><strong>Enhanced Data Transfer Efficiency</strong></p><ul><li>Direct data flow from page cache to NIC</li><li>Elimination of intermediate buffers</li></ul><h3>Zero Copy Implementation in Kafka</h3><p>Kafka’s Zero Copy implementation relies on two key features of Java NIO: memory mapping (mmap) and the sendfile system call. These mechanisms offer different advantages for optimizing data transfer efficiency.</p><h4>1. mmap (Memory Mapping)</h4><p>Memory mapping allows direct access to kernel space memory from user space, eliminating the need to copy data between kernel and user space. This method is particularly effective for small file transfers.</p><pre>// Implementing memory mapping using MappedByteBuffer<br>FileChannel fileChannel = new RandomAccessFile(file, &quot;rw&quot;).getChannel();<br>MappedByteBuffer buffer = fileChannel.map(<br>    FileChannel.MapMode.READ_WRITE, 0, fileChannel.size());</pre><h4>2. sendfile</h4><p>Introduced in Linux 2.1, sendfile is a system call that directly transfers data between file descriptors. It’s ideal for large file transfers and is implemented through FileChannel’s transferTo method in Java NIO.</p><pre>// Implementing Zero Copy using transferTo<br>public static void transferTo(String source, String dest) throws IOException {<br>    FileChannel sourceChannel = new FileInputStream(source).getChannel();<br>    FileChannel destChannel = new FileOutputStream(dest).getChannel();<br>    sourceChannel.transferTo(0, sourceChannel.size(), destChannel);<br>}</pre><h3>Comparison of Implementation Methods</h3><p><strong>mmap:</strong></p><ul><li>Pros: Suitable for small files, supports random access</li><li>Cons: Higher memory usage, potential page faults</li></ul><p><strong>sendfile:</strong></p><ul><li>Pros: Optimal for large files, more efficient Zero Copy</li><li>Cons: No data modification support, whole-file transfer only</li></ul><h3>Applications in Kafka</h3><h4>Log File Transfer</h4><ul><li>Brokers use Zero Copy to efficiently send log files directly to consumers</li><li>Leverages sendfile for high-performance bulk log transfer</li><li>Significantly reduces memory usage and CPU overhead</li></ul><h4>Message Production and Consumption</h4><ul><li>Optimizes network transfer for large batch message production</li><li>Enables efficient data retrieval during batch consumption</li><li>Uses mmap for flexible access to small message batches</li></ul><h4>Cluster Data Synchronization</h4><ul><li>Facilitates efficient data transfer from Leader to Follower replicas</li><li>Reduces network overhead in cross-datacenter replication</li><li>Accelerates large-scale data migration processes</li></ul><h3>Best Practices</h3><p><strong>Strategic Implementation</strong></p><ul><li>Choose implementation based on file size: mmap for files under 1MB, sendfile for larger files</li><li>Apply appropriate methods per use case: sendfile for log transfer, mmap for random access</li><li>Balance memory usage and performance: monitor available system memory</li></ul><p><strong>Performance Monitoring</strong></p><ul><li>Track key metrics: CPU usage, memory utilization, I/O wait times</li><li>Set appropriate alerts: trigger at 70% CPU or 80% memory usage</li><li>Identify bottlenecks through I/O wait time analysis</li></ul><p><strong>Configuration Optimization</strong></p><ul><li>Tune system parameters: adjust vm.max_map_count, file descriptors</li><li>Optimize memory allocation: configure JVM heap size, reserve page cache memory</li><li>Fine-tune socket buffer sizes based on workload</li></ul><p><strong>Security Considerations</strong></p><ul><li>Monitor file descriptor leaks</li><li>Plan capacity based on growth projections</li><li>Implement robust backup strategies</li></ul><h3>Summary</h3><p>Zero Copy is a fundamental technology behind Kafka’s high performance. By minimizing data copies and context switches, it significantly improves data transfer efficiency. Success in implementation requires careful consideration of use cases and ongoing performance monitoring.</p><p>Related Resources:</p><ul><li><a href="http://localhost:3000/problems/mq-kafka-high-performance">Kafka’s High-Performance Design</a></li><li><a href="http://localhost:3000/problem-sets/message-queue-essentials">Practice Message Queue Interview Questions Like LeetCode</a></li></ul><p><em>Originally published at </em><a href="https://www.nootcode.com/knowledge/en/kafka-zero-copy"><em>https://www.nootcode.com</em></a><em>.</em></p><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=7d6a4d1b6bb7" width="1" height="1" alt="">]]></content:encoded>
        </item>
        <item>
            <title><![CDATA[How Does Kafka Log Compaction Work?]]></title>
            <link>https://medium.com/@nootcode/how-does-kafka-log-compaction-work-e05eea6a00de?source=rss-020c641bf0f5------2</link>
            <guid isPermaLink="false">https://medium.com/p/e05eea6a00de</guid>
            <category><![CDATA[kafka]]></category>
            <category><![CDATA[distributed-systems]]></category>
            <category><![CDATA[apache-kafka]]></category>
            <category><![CDATA[kafka-log-compaction]]></category>
            <dc:creator><![CDATA[NootCode]]></dc:creator>
            <pubDate>Sun, 12 Jan 2025 02:40:07 GMT</pubDate>
            <atom:updated>2025-01-12T02:40:07.316Z</atom:updated>
            <content:encoded><![CDATA[<h3>What is Log Compaction?</h3><p>Log Compaction is Kafka’s intelligent way of managing data retention. Instead of simply deleting old messages, it keeps the most recent value for each message key while removing outdated values. This approach is especially valuable when you need to maintain the current state of your data, such as with database changes or configuration settings.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/0*ssvkBIDkg3rBMrgJ.png" /></figure><h3>How Log Compaction Works</h3><h3>1. Log Storage Structure</h3><p>Kafka divides the log into two segments:</p><ul><li><strong>Clean Segment</strong>: Data that has been compacted</li><li><strong>Dirty Segment</strong>: New data waiting for compaction</li></ul><h3>2. Compaction Process</h3><p>The compaction process consists of two main phases:</p><p><strong>Scanning Phase:</strong></p><ul><li>Scans through all messages in the Dirty segment</li><li>Creates an index of message keys and their latest positions</li></ul><p><strong>Cleaning Phase:</strong></p><ul><li>Preserves only the most recent record for each key</li><li>Removes outdated duplicate records</li><li>Maintains the original message sequence</li></ul><h3>3. Compaction Triggers</h3><p>Compaction kicks in when:</p><ul><li>Uncompacted data ratio exceeds threshold</li><li>Scheduled time interval is reached</li><li>Manual compaction is triggered</li></ul><h3>How to Configure Log Compaction?</h3><p>Here’s how to set up log compaction:</p><pre># Enable log compaction<br>log.cleanup.policy=compact<br><br># Set compaction check interval<br>log.cleaner.backoff.ms=30000<br><br># Set compaction trigger threshold<br>log.cleaner.min.cleanable.ratio=0.5<br><br># Set compaction thread count<br>log.cleaner.threads=1</pre><h3>Use Cases</h3><p>Log compaction is best suited for the following scenarios:</p><h3>1. Database Change Records</h3><p>Example of user information updates:</p><ul><li>Initial record: key=1001, value=John</li><li>Update record: key=1001, value=John Smith</li><li>After compaction: key=1001, value=John Smith</li></ul><h3>2. System Configuration Management</h3><p>Example of connection settings:</p><ul><li>Initial config: key=max_connections, value=100</li><li>Updated config: key=max_connections, value=200</li><li>After compaction: key=max_connections, value=200</li></ul><h3>3. State Data Storage</h3><ul><li>Maintain latest entity states</li><li>Save storage space</li></ul><h3>Important Considerations</h3><p>When using log compaction, keep these points in mind:</p><p><strong>Messages Must Have Keys</strong></p><ul><li>Only messages with keys can be compacted</li><li>Keyless messages will remain untouched</li></ul><p><strong>Impact on System Performance</strong></p><ul><li>Compaction process consumes system resources</li><li>Configure parameters appropriately</li></ul><p><strong>Message Order Guarantees</strong></p><ul><li>Messages with the same key stay in order</li><li>Ordering between different keys isn’t guaranteed</li></ul><h3>Summary</h3><p>Kafka’s log compaction offers a smart way to manage our data retention needs. It’s perfect for cases where we only need the latest state of your data, helping you save storage space while keeping your data accessible. When properly configured, it can significantly improve our Kafka cluster’s efficiency.</p><p>Related Topics:</p><ul><li><a href="http://localhost:3000/knowledge/en/kafka-what-is-topic-and-partition">What are Topics and Partitions in Kafka?</a></li><li><a href="http://localhost:3000/knowledge/en/kafka-consumer-rebalance">How Does Kafka Consumer Rebalance Work?</a></li><li><a href="http://localhost:3000/problem-sets/message-queue-essentials">Practice Message Queue Interview Questions Like LeetCode</a></li></ul><p><em>Originally published at </em><a href="https://www.nootcode.com/knowledge/en/kafka-log-compaction"><em>https://www.nootcode.com</em></a><em>.</em></p><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=e05eea6a00de" width="1" height="1" alt="">]]></content:encoded>
        </item>
        <item>
            <title><![CDATA[What is a Kafka Consumer Group?]]></title>
            <link>https://medium.com/@nootcode/what-is-a-kafka-consumer-group-0ad1b3a5fba0?source=rss-020c641bf0f5------2</link>
            <guid isPermaLink="false">https://medium.com/p/0ad1b3a5fba0</guid>
            <category><![CDATA[apache-kafka]]></category>
            <category><![CDATA[kafka]]></category>
            <category><![CDATA[kafka-consumer-group]]></category>
            <category><![CDATA[kafka-consumer]]></category>
            <dc:creator><![CDATA[NootCode]]></dc:creator>
            <pubDate>Tue, 07 Jan 2025 02:18:19 GMT</pubDate>
            <atom:updated>2025-01-07T02:18:19.281Z</atom:updated>
            <content:encoded><![CDATA[<h3>What is a Consumer Group?</h3><p>A Consumer Group is Kafka’s mechanism for organizing consumers to collectively process messages from topics. It enables multiple Consumer instances to work together, providing horizontal scalability while ensuring partition-level ordering guarantees.</p><h3>Basic Concept</h3><figure><img alt="" src="https://cdn-images-1.medium.com/max/918/0*M8koxgWDZNh_k8_b.png" /></figure><h3>Key Features of Consumer Groups</h3><h3>1. Consumption Division</h3><ul><li>Each partition is consumed by only one consumer within a group</li><li>A single consumer can handle multiple partitions</li><li>Automatic load balancing between group members</li></ul><h3>2. Consumer Offset Management</h3><pre>Consumption Progress Tracking: <br>├── Auto-commit: enable.auto.commit=true <br>└── Manual commit: enable.auto.commit=false</pre><h3>3. Consumer Group Isolation</h3><p>Different Consumer Groups operate independently:</p><pre>Topic-A <br>├── Consumer Group 1: Order Processing <br>└── Consumer Group 2: Order Analytics</pre><h3>Real-World Use Cases</h3><h3>1. Message Broadcasting</h3><pre>One message needs processing by multiple systems: <br>├── Group 1: Order System <br>├── Group 2: Logistics System <br>└── Group 3: Analytics System</pre><h3>2. Load Balancing</h3><pre>When single consumer capacity is insufficient: <br>Topic: &quot;User Registration&quot; <br>├── Consumer 1: Processes 25% of messages <br>├── Consumer 2: Processes 25% of messages <br>├── Consumer 3: Processes 25% of messages <br>└── Consumer 4: Processes 25% of messages</pre><h3>Consumer Group Configuration Best Practices</h3><h3>1. Basic Configuration</h3><pre># Consumer Group ID<br>group.id=order-processing-group<br><br># Commit mode<br>enable.auto.commit=false<br><br># Session timeout<br>session.timeout.ms=10000<br><br># Heartbeat interval<br>heartbeat.interval.ms=3000</pre><h3>2. Consumer Count Planning</h3><p>Here are the key principles for configuring Consumer count:</p><p><strong>Minimum</strong></p><ul><li>At least 1 Consumer is required</li><li>Ensures coverage of all partitions</li></ul><p><strong>Maximum</strong></p><ul><li>Should not exceed the total partition count</li><li>Additional Consumers will remain idle</li><li>Results in wasted system resources</li></ul><p><strong>Recommended</strong></p><ul><li>Formula: Partitions ÷ Single Consumer capacity</li><li>Adjust according to actual load</li><li>Maintain a 30% capacity buffer</li></ul><h3>3. Consumer Offset Commit Strategy</h3><p>Consumer Offset is a crucial mechanism for tracking consumption progress. Each Consumer must periodically commit its consumption position (offset) to Kafka to ensure correct recovery after restarts or failures.</p><p>Two main commit strategies:</p><ul><li><strong>Auto-commit</strong>: Handled automatically by Kafka client, simple but may lose messages</li><li><strong>Manual commit</strong>: Developer controls commit timing, higher reliability</li></ul><p>Example of manual commit:</p><pre>// Manual commit example<br>while (true) {<br>    // Poll messages with 100ms timeout<br>    ConsumerRecords&lt;String, String&gt; records = consumer.poll(Duration.ofMillis(100));<br>    for (ConsumerRecord&lt;String, String&gt; record : records) {<br>        // Process message<br>        processMessage(record);<br>    }<br>    // Manual commit after batch processing<br>    // commitSync() blocks until commit succeeds or fails<br>    consumer.commitSync();<br>}</pre><p><strong>Code explanation:</strong></p><ol><li>Use poll() to batch fetch messages</li><li>Process each message in loop</li><li>Commit offset only after all messages are processed</li><li>Use commitSync() for guaranteed commit results</li></ol><p>This approach, while slightly slower, ensures no message loss and is suitable for scenarios requiring high data reliability.</p><h3>Common Issues and Solutions</h3><h3>1. Too Many Consumers</h3><p><strong>Issue</strong>: Having more Consumers than partitions leads to resource waste.</p><p><strong>Solutions</strong>:</p><ul><li>Maintain Consumer count at or below partition count</li><li>If higher parallelism is needed, consider adding partitions</li><li>Assess actual processing capacity requirements</li></ul><h3>2. Consumption Skew</h3><p><strong>Problem</strong>: Some Consumers are overloaded while others are idle.</p><p><strong>Solutions</strong>:</p><ul><li>Review partition assignment strategy</li><li>Consider adding partitions for finer-grained load balancing</li><li>Optimize message key distribution to avoid hot partitions</li><li>Monitor Consumer capacity and load</li></ul><h3>3. Duplicate Processing</h3><p><strong>Problem</strong>: Messages are processed multiple times, affecting business logic.</p><p><strong>Solutions</strong>:</p><ul><li>Implement message idempotency</li><li>Use manual offset commit strategy</li><li>Set appropriate commit intervals</li><li>Implement business-level deduplication</li></ul><h3>Summary</h3><p>Consumer Groups are a key mechanism for Kafka’s scalability and fault tolerance. Through proper configuration and usage of Consumer Groups, we can build more reliable message processing systems.</p><p>Related Topics:</p><ul><li><a href="https://www.nootcode.com/knowledge/en/kafka-consumer-rebalance">How Does Kafka Consumer Rebalance Work?</a></li><li><a href="https://www.nootcode.com/knowledge/en/kafka-producer-retry">How to Use Kafka Producer Retries?</a></li><li><a href="https://www.nootcode.com/problem-sets/message-queue-essentials">Practice Message Queue Interview Questions Like LeetCode</a></li></ul><p><em>Originally published at </em><a href="https://www.nootcode.com/knowledge/en/kafka-consumer-group"><em>https://www.nootcode.com</em></a><em>.</em></p><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=0ad1b3a5fba0" width="1" height="1" alt="">]]></content:encoded>
        </item>
        <item>
            <title><![CDATA[How to use Kafka Retries?]]></title>
            <link>https://medium.com/@nootcode/how-to-use-kafka-retries-5d32729531da?source=rss-020c641bf0f5------2</link>
            <guid isPermaLink="false">https://medium.com/p/5d32729531da</guid>
            <category><![CDATA[apache-kafka]]></category>
            <category><![CDATA[retry]]></category>
            <category><![CDATA[retry-policy]]></category>
            <category><![CDATA[kafka]]></category>
            <category><![CDATA[distributed-systems]]></category>
            <dc:creator><![CDATA[NootCode]]></dc:creator>
            <pubDate>Thu, 02 Jan 2025 07:02:13 GMT</pubDate>
            <atom:updated>2025-01-02T07:02:13.171Z</atom:updated>
            <content:encoded><![CDATA[<figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*atxptduArd31wdwIrOj-aQ.png" /></figure><h3>Why Do We Need Retries?</h3><p>In distributed systems, network failures and server outages are inevitable. Kafka is no exception. The Producer retry mechanism is designed specifically to handle these temporary failures gracefully.</p><h3>How Producer Retry Works?</h3><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/0*mDA24VjUFfD4QCIx.png" /></figure><h3>Key Configuration Parameters</h3><h3>1. retries</h3><pre># Number of retry attempts<br>retries=3                   # Retry 3 times</pre><h3>2. retry.backoff.ms</h3><pre># Time between retries<br>retry.backoff.ms=100        # Base retry interval of 100ms</pre><blockquote><em>Note: Since Kafka 2.1, Producers use exponential backoff by default. The actual wait time increases with each retry:</em></blockquote><blockquote>- 1st retry: 100ms wait</blockquote><blockquote>- 2nd retry: 200ms wait</blockquote><blockquote>- 3rd retry: 400ms wait This prevents unnecessary rapid retries during sustained failures.</blockquote><h3>3. delivery.timeout.ms</h3><pre># Total timeout for message delivery<br>delivery.timeout.ms=120000  # Wait up to 2 minutes</pre><h3>4. enable.idempotence</h3><pre># Enable idempotence to prevent duplicate messages during retries<br>enable.idempotence=true</pre><blockquote><em>Strongly recommended for production environments. This ensures each message is written exactly once, even when retries occur.</em></blockquote><h3>Real-World Scenarios</h3><h3>Scenario 1: Network Hiccup</h3><p>Here’s how the retry mechanism handles network instability:</p><pre>Send message ❌ Network timeout <br>↓ <br>Wait 100ms and retry <br>↓ <br>Retry successful ✅ Message delivered</pre><h3>Scenario 2: Broker Failover</h3><p>When a broker fails, Kafka automatically elects a new leader:</p><pre>Send message ❌ Leader unavailable <br>↓ <br>Wait 100ms (while leader election happens) <br>↓ <br>Retry sending ✅ New leader online, message delivered</pre><h3>Best Practices</h3><p><strong>Enable Idempotence</strong></p><ul><li>Prevents message duplication during retries</li><li>Set enable.idempotence=true</li></ul><p><strong>Set Reasonable Retry Limits</strong></p><ul><li>Based on your business requirements</li><li>Avoid infinite retries</li></ul><p><strong>Monitor Retry Metrics</strong></p><ul><li>Track retry counts</li><li>Set up alerting thresholds</li></ul><h3>Key Takeaways</h3><p>A well-configured retry mechanism ensures message reliability while avoiding performance issues from excessive retries. It’s a crucial component of any robust messaging system.</p><p>Related Topics:</p><ul><li><a href="https://www.nootcode.com/knowledge/en/kafka-what-is-topic-and-partition">What are Kafka Topics and Partitions?</a></li><li><a href="https://www.nootcode.com/knowledge/en/kafka-consumer-rebalance">How Does Kafka Consumer Rebalancing Work?</a></li></ul><p><em>Originally published at </em><a href="https://www.nootcode.com/knowledge/en/kafka-producer-retry"><em>https://www.nootcode.com</em></a><em>.</em></p><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=5d32729531da" width="1" height="1" alt="">]]></content:encoded>
        </item>
        <item>
            <title><![CDATA[How Does Kafka Consumer Rebalance Work?]]></title>
            <link>https://medium.com/@nootcode/how-does-kafka-consumer-rebalance-work-c95aaa612e5e?source=rss-020c641bf0f5------2</link>
            <guid isPermaLink="false">https://medium.com/p/c95aaa612e5e</guid>
            <category><![CDATA[kafka-rebalancing]]></category>
            <category><![CDATA[kafka]]></category>
            <category><![CDATA[apache-kafka]]></category>
            <category><![CDATA[kafka-consumer]]></category>
            <dc:creator><![CDATA[NootCode]]></dc:creator>
            <pubDate>Tue, 31 Dec 2024 08:30:31 GMT</pubDate>
            <atom:updated>2024-12-31T08:30:31.227Z</atom:updated>
            <content:encoded><![CDATA[<figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*bFMRcVyowxrPPYKK1tLW4w.png" /></figure><h3>What is Consumer Rebalance?</h3><p>When you run Kafka with multiple consumers, you’ll need to handle Consumer Rebalance. It happens when Kafka needs to shuffle around which consumer reads from which partition — usually when consumers come and go from your consumer group. Think of it like redistributing work when people join or leave your team. While this keeps things running smoothly, doing it too often can slow everything down.</p><p>Here’s a simple example:</p><pre>Initial Consumer Group State: <br>Consumer 1 --&gt; Partition 0, 1 <br>Consumer 2 --&gt; Partition 2, 3 <br><br>After Consumer 2 crashes: <br>Consumer 1 --&gt; Partition 0, 1, 2, 3</pre><p>Why do we need this?</p><ul><li>Load balancing</li><li>High availability</li><li>Fault tolerance</li></ul><h3>What Triggers a Rebalance?</h3><h4>1. Consumer Group Membership Changes</h4><ul><li>You add a new consumer</li><li>A consumer shuts down normally</li><li>A consumer crashes unexpectedly</li></ul><h4>2. Topic Subscription Changes</h4><ul><li>Topic deletion</li><li>Partition count changes</li><li>Consumer subscription changes</li></ul><h4>3. Manual Trigger by Admin</h4><h3>Rebalance Process</h3><p>Let’s break down what happens during a rebalance:</p><pre>Phase 1: Group Membership Change <br>├── Consumers send JoinGroup request <br>├── Group Coordinator selects leader <br>└── Returns member info to leader <br><br>Phase 2: Partition Assignment <br>├── Leader determines assignment plan <br>├── Sends SyncGroup request <br>└── All members receive assignments <br><br>Phase 3: Start Consuming <br>├── Consumers get their partitions <br>├── Commit old offsets <br>└── Begin consuming from new partitions</pre><h3>Partition Assignment Strategies</h3><h4>1. Range Strategy (Default)</h4><pre>Topic-A: 4 partitions <br>├── Consumer-1: Partition 0, 1 <br>└── Consumer-2: Partition 2, 3 <br><br>Good: Assigns nearby partitions together <br>Bad: Some consumers might get more work</pre><h4>2. RoundRobin Strategy</h4><pre>Topic-A: 4 partitions <br>├── Consumer-1: Partition 0, 2 <br>└── Consumer-2: Partition 1, 3 <br><br>Good: Each consumer gets equal work <br>Bad: Partitions are spread out</pre><h4>3. Sticky Strategy</h4><pre>Characteristics: <br>├── Shares work fairly <br>├── Keeps working assignments if possible <br>└── Moves partitions only when needed</pre><h3>Performance Optimization Tips</h3><h4>1. Proper Timeout Settings</h4><pre>// Example configuration<br>properties.put(&quot;session.timeout.ms&quot;, &quot;10000&quot;);<br>properties.put(&quot;heartbeat.interval.ms&quot;, &quot;3000&quot;);<br>properties.put(&quot;max.poll.interval.ms&quot;, &quot;300000&quot;);</pre><h4>2. Avoid Frequent Rebalancing</h4><ul><li>Set the right heartbeat timing</li><li>Process messages quickly</li><li>Use Static Membership when possible</li></ul><h4>3. Monitoring and Alerts</h4><p>Watch out for:</p><ul><li>Rebalance frequency</li><li>Rebalance duration</li><li>Consumer lag</li></ul><h3>Common Issues and Solutions</h3><h4>1. Frequent Rebalancing</h4><p>Why it happens:</p><ul><li>Slow message processing</li><li>Long GC pauses</li><li>Network instability</li></ul><p>Fix it by:</p><pre>1. Increase session.timeout.ms<br>2. Tune GC parameters<br>3. Enable Static Membership<br>4. Optimize message processing logic</pre><h3>2. Slow Rebalance Process</h3><p>The usual suspects:</p><ul><li>Too many group members</li><li>Too many subscribed topics</li><li>Too many partitions</li></ul><p>Here’s what works:</p><pre>1. Control consumer group size<br>2. Use multiple consumer groups<br>3. Optimize partition assignment strategy</pre><h3>Summary</h3><p>Understanding Rebalance is key to maintaining a healthy Kafka cluster. You’ll likely get asked about it as part of Kafka interview questions too. When running in production, make sure to monitor rebalance events closely, adjust configurations as needed, and keep a watchful eye on your metrics.</p><p>Related Resources:</p><ul><li><a href="https://www.nootcode.com/knowledge/en/kafka-what-is-topic-and-partition">What are Kafka Topics and Partitions?</a></li><li><a href="https://www.nootcode.com/problem-sets/message-queue-essentials">Practice Message Queue Interview Questions Like LeetCode</a></li></ul><p><em>Originally published at </em><a href="https://www.nootcode.com/knowledge/en/kafka-consumer-rebalance"><em>https://www.nootcode.com</em></a><em>.</em></p><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=c95aaa612e5e" width="1" height="1" alt="">]]></content:encoded>
        </item>
        <item>
            <title><![CDATA[What are Kafka Topics and Partitions?]]></title>
            <link>https://medium.com/@nootcode/what-are-kafka-topics-and-partitions-b86a7653f8cc?source=rss-020c641bf0f5------2</link>
            <guid isPermaLink="false">https://medium.com/p/b86a7653f8cc</guid>
            <category><![CDATA[kafka]]></category>
            <category><![CDATA[kafka-topic]]></category>
            <category><![CDATA[kafka-partition]]></category>
            <category><![CDATA[apache-kafka]]></category>
            <category><![CDATA[message-queue]]></category>
            <dc:creator><![CDATA[NootCode]]></dc:creator>
            <pubDate>Sun, 29 Dec 2024 01:05:42 GMT</pubDate>
            <atom:updated>2024-12-29T01:05:42.494Z</atom:updated>
            <content:encoded><![CDATA[<figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*Rxy2IVvskb0LXXlSIYji4A.png" /></figure><h3>What is a Topic?</h3><p>A Topic is Kafka’s fundamental building block for organizing messages. It’s essentially a feed or channel where messages flow through. If Kafka were a post office, Topics would be like different mailboxes, each dedicated to a specific type of message.</p><h3>What is a Partition?</h3><p>Each Topic can be divided into multiple Partitions, which is a key feature for scalability. Think of it as splitting a busy highway into multiple lanes. Here’s why Partitions are important:</p><ol><li><strong>Parallel Processing</strong> — Each Partition operates independently, similar to multiple CPU cores</li><li><strong>Load Distribution</strong> — Data is spread across your cluster, preventing single-server bottlenecks</li><li><strong>High Throughput</strong> — Multiple Partitions enable concurrent operations for better performance</li></ol><h3>Partition Storage Model</h3><pre>Topic: &quot;Order Messages&quot; <br>├── Partition 0: [Order1] -&gt; [Order2] -&gt; [Order3] <br>├── Partition 1: [Order4] -&gt; [Order5] -&gt; [Order6] <br>└── Partition 2: [Order7] -&gt; [Order8] -&gt; [Order9]</pre><p>Each message in a Partition receives a unique offset number, which serves as its sequential identifier within that Partition.</p><h3>Partition Replication Mechanism</h3><p>For fault tolerance, Kafka maintains multiple copies of each Partition:</p><ul><li><strong>Leader Replica</strong> — The primary copy that handles all read/write operations</li><li><strong>Follower Replicas</strong> — Backup copies that maintain synchronization and provide failover capability</li></ul><pre>Partition 0 <br>├── Leader (Server 1) <br>├── Follower (Server 2) <br>└── Follower (Server 3)</pre><h3>Producer Assignment Strategies</h3><p>Producers use several strategies to distribute messages across Partitions:</p><ol><li><strong>Round-Robin</strong> — Distributes messages evenly across Partitions</li><li><strong>Key-Based</strong> — Routes messages with the same key to the same Partition</li><li><strong>Custom Logic</strong> — Implements specific routing rules based on business requirements</li></ol><h3>Consumer Reading Patterns</h3><p>Consumer groups coordinate Partition reading through different assignment strategies:</p><ol><li><strong>Range Assignment</strong> — Allocates continuous Partition ranges to consumers</li><li><strong>Round-Robin Assignment</strong> — Distributes Partitions evenly across consumers</li><li><strong>Sticky Assignment</strong> — Maintains stable assignments to minimize rebalancing overhead</li></ol><h3>Practical Recommendations</h3><p><strong>Partition Sizing Guidelines</strong></p><ul><li>Calculate your expected message volume</li><li>Consider your infrastructure capacity</li><li>Formula: Partition count = (Target throughput/sec) ÷ (Single partition throughput)</li></ul><p><strong>Important Considerations</strong></p><ul><li>Each Partition requires system resources</li><li>Adding Partitions is straightforward, but removal is complex</li><li>Excessive Partitions can impact cluster stability</li></ul><p><strong>Key Metrics to Watch</strong></p><ul><li>Consumer lag measurements</li><li>Replica synchronization status</li><li>Partition load distribution</li></ul><h3>Summary</h3><p>Proper Topic and Partition design is fundamental to a well-performing Kafka deployment. Consider your specific use case, plan your capacity requirements, and choose configurations that align with your performance needs.</p><p>Visit <a href="https://www.nootcode.com/problem-sets/message-queue-essentials">Message Queue Essentials</a> to actively practice more Kafka interview questions.</p><p><em>Originally published at </em><a href="https://www.nootcode.com/knowledge/en/kafka-what-is-topic-and-partition"><em>https://www.nootcode.com</em></a><em>.</em></p><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=b86a7653f8cc" width="1" height="1" alt="">]]></content:encoded>
        </item>
    </channel>
</rss>