<?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 Manjinder Singh on Medium]]></title>
        <description><![CDATA[Stories by Manjinder Singh on Medium]]></description>
        <link>https://medium.com/@msinghw3?source=rss-1cd2e423dd83------2</link>
        <image>
            <url>https://cdn-images-1.medium.com/fit/c/150/150/1*xLLxCRFfBxMHvLcowtUeMA.jpeg</url>
            <title>Stories by Manjinder Singh on Medium</title>
            <link>https://medium.com/@msinghw3?source=rss-1cd2e423dd83------2</link>
        </image>
        <generator>Medium</generator>
        <lastBuildDate>Fri, 04 Dec 2026 19:43:04 GMT</lastBuildDate>
        <atom:link href="https://medium.com/@msinghw3/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[ Kafka Producer — What Really Happens When You Call send()]]></title>
            <link>https://medium.com/@msinghw3/kafka-producer-what-really-happens-when-you-call-send-eeac8e472266?source=rss-1cd2e423dd83------2</link>
            <guid isPermaLink="false">https://medium.com/p/eeac8e472266</guid>
            <category><![CDATA[event-driven-architecture]]></category>
            <category><![CDATA[data-pipeline]]></category>
            <category><![CDATA[java]]></category>
            <category><![CDATA[distributed-systems]]></category>
            <category><![CDATA[kafka]]></category>
            <dc:creator><![CDATA[Manjinder Singh]]></dc:creator>
            <pubDate>Wed, 22 Apr 2026 21:05:14 GMT</pubDate>
            <atom:updated>2026-04-22T21:05:14.339Z</atom:updated>
            <content:encoded><![CDATA[<figure><img alt="" src="https://cdn-images-1.medium.com/max/410/1*J_ZQD18_NfU51gq1nROGlQ.png" /></figure><blockquote>“When you call <em>send()</em>, Kafka doesn’t send anything over the network.<br> It just queues work internally.”</blockquote><p>When developers write:</p><pre>producer.send(&quot;topicName&quot;, &quot;key&quot;, &quot;value&quot;);</pre><p>It <em>looks</em> like the message is instantly sent to Kafka.</p><p>But here’s the truth 👇</p><p>Before any network call happens, the message goes through multiple internal stages inside the producer.</p><h3>1. SERIALIZER — Converting Data to Bytes</h3><p>Our message/event is generally a Java Object but Kafka understands only bytes. That’s where Serializer is required.</p><p>The data type of event could be anything: String, Integer, JSON, Custom Java Object (POJO),</p><p>So, the very first thing the producer does is convert your data into bytes.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/775/1*9uu3_Vn3KtvjvpAP2pEiHw.png" /></figure><p>Every Serializer implements this interface</p><pre>public interface Serializer&lt;T&gt; { <br>    byte[] serialize(String topic, T data); <br>} </pre><p>There are many built-in Serializer from which we can choose based on our business requirement. Some of them are:</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/328/1*aetI_-Md5NFSHJL9tYzrIQ.png" /></figure><h3><strong>2. PARTITIONER </strong>— Deciding the Partition where message goes</h3><p>Once the message is converted into bytes, the next question is:</p><blockquote><em>👉 </em><strong><em>Which partition should this message go to?</em></strong></blockquote><p>Kafka topics are divided into partitions, and every message must belong to exactly one partition. Ordering in Kafka is guaranteed <strong>only within a partition</strong></p><p>1️⃣ Partition is explicitly provided</p><pre>@Override <br>public CompletableFuture&lt;SendResult&lt;K, V&gt;&gt; send(String topic, Integer partition, K key, @Nullable V data){ <br>    ProducerRecord&lt;K, V&gt; producerRecord = new ProducerRecord&lt;&gt;(topic, partition, key, data); <br>    return observeSend(producerRecord); <br>} </pre><p>2️⃣<strong> </strong>Key-based Partitioning</p><pre>@Override <br>public CompletableFuture&lt;SendResult&lt;K, V&gt;&gt; send(String topic, K key, @Nullable V data) { <br>    ProducerRecord&lt;K, V&gt; producerRecord = new ProducerRecord&lt;&gt;(topic, key, data); <br>    return observeSend(producerRecord); <br>} </pre><p><strong>“<em>partition = hash(keyBytes) % numberOfPartitions</em>”</strong></p><p>3️⃣<strong> </strong>Key is not provided</p><pre>@Override <br>public CompletableFuture&lt;SendResult&lt;K, V&gt;&gt; send(String topic, @Nullable V data) { <br>    ProducerRecord&lt;K, V&gt; producerRecord = new ProducerRecord&lt;&gt;(topic, data); <br>    return observeSend(producerRecord); <br>} </pre><p>In this case we have two options to choose a partition:</p><ul><li><strong>Round Robin Strategy : </strong>Pre Kafka 2.4</li><li><strong>Sticky Partitioning Strategy :</strong> From Kafka 2.4 onwards</li></ul><p><em>“We will understand their difference in next section”</em></p><figure><img alt="" src="https://cdn-images-1.medium.com/max/793/1*mZFkDD4yt9fPXQf4JkES2A.png" /></figure><h3><strong>3. RECORD ACCUMULATOR </strong>— Where Batching Happens</h3><p>Until this point:</p><p>✅ Message is serialized into bytes<br>✅ Partition is decided</p><p>The <strong>Record Accumulator</strong> is an <strong>in-memory buffer inside the Kafka Producer</strong>.</p><ul><li>Producer does not send messages immediately to Broker</li><li>Instead, it collects and groups messages into<strong> </strong>batches and then send them to the broker.</li></ul><blockquote>And the Record Accumulator is where these batches are built.</blockquote><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*oxGuPqYTO0qxPjs9ILeJ9A.png" /></figure><blockquote>Now let’s understand, with an example, why Sticky partitioning performs better than round-robin when key is not provided.</blockquote><p>Example: <br> <br>Topic: order-events, Key = null, Value = E1<br>Topic: order-events, Key = null, Value = E2<br>Topic: order-events, Key = null, Value = E3<br>Topic: order-events, Key = null, Value = E4<br>Topic: order-events, Key = null, Value = E5</p><p>Number of Partitions for order-events topic : [P0, P1, P2]</p><h4><strong>a. Round Robin Partitioning Strategy</strong></h4><p>Let’s say the producer is using <strong>Round Robin</strong> (no key provided).</p><p>Events are distributed evenly:</p><pre>E1 → P0  <br>E2 → P1  <br>E3 → P2  <br>E4 → P0  <br>E5 → P1</pre><ul><li>Partition P0 → [E1, E4]</li><li>Partition P1 → [E2, E5]</li><li>Partition P2 → [E3]</li></ul><figure><img alt="" src="https://cdn-images-1.medium.com/max/390/1*Q_Tvlth2juWKGaZybPLrBg.png" /></figure><p><strong>Problem 1:</strong> <strong>Fragmented Batching</strong></p><ul><li>Messages are spread across partitions. So, instead of 1 big batch of 5 events we have 3 smaller batches</li></ul><p><strong>Problem 2: More Network Requests</strong></p><p>Kafka sends one request per partition batch.</p><ul><li>Batch P0 → 1 request</li><li>Batch P1 → 1 request</li><li>Batch P2 → 1 request</li></ul><p>3 network calls instead of 1</p><p><strong>Problem 3: Lower Throughput Efficiency</strong></p><p>Even though Kafka is batching:</p><ul><li>Batches are not fully utilized</li><li>Network overhead increases</li><li>Throughput optimization is reduced</li></ul><h4><strong>a. Sticky Partitioning Strategy</strong></h4><p>When events come <strong>without a key</strong>, instead of distributing them randomly every time…</p><p>Kafka <strong>sticks to one partition for a while</strong></p><p>🔄 How does it work?</p><p>📌 <strong>Event 1</strong></p><ul><li>Event <strong>E1</strong> comes (no key)</li><li>Producer picks a <strong>random partition</strong>, say <strong>P1</strong></li><li>Stores this in memory:</li></ul><pre>order-events → P1</pre><ul><li>Sends E1 to <strong>Record Accumulator → Batch of P1</strong></li></ul><h3>📌 Event 2, E3, E4…</h3><ul><li>New events arrive (no key)</li><li>Producer checks sticky mapping:</li></ul><pre>order-events → P1</pre><p>👉 Instead of choosing a new partition<br> 👉 It <strong>keeps sending events to P1</strong></p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*t8b6FvaV9GyVOO0_zvlrNw.png" /></figure><p>Below is a tabular comparison of both Partitioning strategies:</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*Np44_VyEQZ1LzJIcW7oKXg.png" /></figure><p><em>“Sticky partitioning improves batching efficiency, but may lead to uneven distribution in the short term.”</em></p><h4>⚙️ How Does Kafka Control a Batch?</h4><p>At this point, we know the producer does not send messages one by one.</p><p>Instead, it keeps collecting records inside the <strong>Record Accumulator</strong> and builds <strong>topic-partition-wise batches</strong>.</p><p>Now the question is:</p><blockquote><strong>How long should Kafka keep collecting records before sending a batch?</strong></blockquote><p>Kafka mainly uses two important producer configs to control this:</p><p>1️⃣ batch.size — How Big Can a Batch Grow?</p><pre>batch.size=16384</pre><p>Default value = <strong>16384 bytes = 16 KB</strong></p><p>This means Kafka allocates up to <strong>16 KB of memory </strong>for one partition batch</p><p><strong>Important point:</strong><em>batch.size is the maximum batch buffer size per partition, not the number of messages.</em></p><p>So Kafka does <strong>not</strong> think in terms of “16 messages” or “20 messages”<br>It thinks in terms of <strong>bytes</strong>.</p><p>2️⃣ linger.ms — How Long Should Kafka Wait?</p><pre>linger.ms=10</pre><p>This means that producer will wait up to <strong>10 milliseconds</strong> for more records to arrive before sending the batch.</p><h3>4. COMPRESSION — Sending More Data in Less Space</h3><p>Before Kafka sends this batch over the network, it applies <strong>compression. </strong>Kafka does <strong>not compress individual messages, </strong>rather it compresses the <strong>entire batch.</strong></p><pre>Batch before compression = 64 KB  <br>Batch after compression  = 18 KB</pre><p>Kafka allows to choose different compression algorithms like: <strong>snappy, gzip, lz4, zstd</strong></p><p>⚡ Why Compression is important?</p><h4>Network Efficiency</h4><ul><li>Less data sent over the network</li><li>Faster transmission</li><li>Reduced bandwidth usage</li></ul><h4>Disk Storage</h4><ul><li>Kafka stores messages in <strong>compressed form</strong></li><li>Saves disk space on brokers</li></ul><h4>Throughput</h4><ul><li>More data transferred per second</li><li>Better overall system performance</li></ul><h3>5. SENDER THREAD - The One That Talks to Kafka</h3><p>Up until now:</p><p>✅ Messages were serialized<br>✅ Partition was selected<br>✅ Batches were created<br>✅ Batches were compressed</p><p>But one thing still hasn’t happened…. <strong>No network call has been made yet.</strong></p><blockquote><strong>Application thread NEVER sends data to Kafka brokers</strong></blockquote><p>When you call:</p><pre>producer.send(&quot;topic&quot;, key, value);</pre><p>It only writes the record to the <strong>Record Accumulator</strong></p><p>And then… it returns immediately (non-blocking)</p><p><strong>Who actually sends the data?</strong></p><p>A background thread called the <strong><em>Sender Thread</em></strong></p><p>This is what makes Kafka:</p><ul><li>Asynchronous</li><li>High throughput</li><li>Non-blocking</li></ul><p>🧩 <strong>Responsibilities of Sender Thread</strong></p><p>1️⃣ Check for Ready Batches</p><p>2️⃣ Group Batches by Broker Leader<br> Each partition has a <strong>leader broker</strong>. Example:</p><pre>Batch 1: Topic A-P0 → Leader: Broker1  <br>Batch 2: Topic A-P2 → Leader: Broker1  <br>Batch 3: Topic A-P1 → Leader: Broker2  </pre><p>Instead of sending separate requests, Kafka groups all batches targeting the same broker thus reducing network calls.</p><p>Batch[1,2] : Broker 1 (only 1 network call)<br>Batch[3] : Broker 2</p><p>So, optimization happens at two levels:</p><ul><li>Batching → reduces number of messages</li><li>Grouping by broker → reduces number of requests</li></ul><p>3️⃣ Send Request (NetworkClient)</p><p>4️⃣ Process Response (Acknowledgements)</p><p>Broker responds with:</p><ul><li>Success (ACK)</li><li>Failure (error)</li></ul><p>This depends on producer config:</p><pre>acks=all | 1 | 0</pre><p>5️⃣ Handle Retries</p><p>6️⃣ Complete Future &amp; Callbacks</p><p>“Each producer instance has <strong>only one Sender thread”</strong></p><h4>⚙️ Can We Control how many Sender Threads Kafka uses?</h4><p>👉 Short answer: <strong>NO<br></strong>Even though you can’t control the thread count… You <strong>can control how much work it does concurrently</strong></p><p>This is where an important config comes in:</p><pre>max.in.flight.requests.per.connection=5</pre><p>“<strong><em>It means: How many requests can be “in progress” (sent but not yet acknowledged)</em></strong>”</p><h3>🌐 How does this work with multiple brokers?</h3><p>Kafka Producer maintains <strong>one TCP connection per broker</strong>.</p><p><strong>Example:</strong></p><ul><li>You have <strong>3 brokers</strong></li><li>Producer creates <strong>3 TCP connections</strong></li></ul><pre>Connection 1 → Broker 1  <br>Connection 2 → Broker 2  <br>Connection 3 → Broker 3</pre><p><strong>🚀 Now apply the config</strong></p><pre>max.in.flight.requests.per.connection = 5</pre><p>So, each connection can have <strong>5 in-flight requests</strong></p><p>👉 Total in-flight requests across cluster:</p><pre>3 brokers × 5 = 15 requests</pre><blockquote>🤔 Final Thought for You</blockquote><blockquote>Now that you understand how the Kafka Producer works internally, here’s a question to think about:</blockquote><blockquote><strong>What happens if ?</strong></blockquote><blockquote>max.in.flight.requests.per.connection &gt; 1<br>retries &gt; 0</blockquote><blockquote><strong>📌 Scenario</strong></blockquote><blockquote>Request <strong>A</strong> is sent → in-flight</blockquote><blockquote>Request <strong>B</strong> is sent → in-flight</blockquote><blockquote>Now:</blockquote><blockquote>❌ Request <strong>A</strong> fails</blockquote><blockquote>✅ Request <strong>B</strong> succeeds and is written to the partition</blockquote><blockquote>Then:</blockquote><blockquote>🔁 Request <strong>A</strong> is retried</blockquote><blockquote>✅ Successfully written <strong>after B</strong></blockquote><blockquote>🚨 Result?</blockquote><blockquote>👉 Messages are written <strong>out of order</strong></blockquote><blockquote>Expected Order: A → B <br>Actual Order: B → A</blockquote><blockquote>🧠 Question for You</blockquote><blockquote>How would you configure your Kafka producer to:</blockquote><blockquote>- Maintain ordering</blockquote><blockquote>- While still achieving good throughput?</blockquote><blockquote>(Think about acks, retries, and max.in.flight.requests.per.connection)</blockquote><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=eeac8e472266" width="1" height="1" alt="">]]></content:encoded>
        </item>
        <item>
            <title><![CDATA[Everything Clicked When I Learned Kafka Partitions !]]></title>
            <link>https://medium.com/@msinghw3/everything-clicked-when-i-learned-kafka-partitions-b599be4ff0c6?source=rss-1cd2e423dd83------2</link>
            <guid isPermaLink="false">https://medium.com/p/b599be4ff0c6</guid>
            <category><![CDATA[spring-boot]]></category>
            <category><![CDATA[java]]></category>
            <category><![CDATA[distributed-systems]]></category>
            <category><![CDATA[kafka]]></category>
            <category><![CDATA[event-driven-architecture]]></category>
            <dc:creator><![CDATA[Manjinder Singh]]></dc:creator>
            <pubDate>Sat, 18 Apr 2026 19:08:52 GMT</pubDate>
            <atom:updated>2026-04-18T19:08:52.686Z</atom:updated>
            <content:encoded><![CDATA[<figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*giAkjznHc9lSIQXIu19S9g.png" /></figure><h3>🔹 <strong>What is a Partition ?</strong></h3><blockquote>A partition is a <strong>physical, ordered, append-only log</strong> that belongs to a Kafka topic. A topic is just a logical abstraction — partitions are where the real data lives.</blockquote><p>Partitions are the foundation of Kafka scalability. They enable :</p><ul><li>Parallel Processing</li><li>Horizontal Scaling</li><li>Load Distribution</li></ul><p><em>Let’s breakdown physical, ordered and append-only keywords :</em></p><h4>1. Physical</h4><ul><li>Events are <strong>actually stored on disk</strong></li><li>Each partition is backed by <strong>files on the broker machine</strong></li><li>Kafka is <em>not just in-memory</em> → it’s durable</li></ul><p>Think of a partition as a <strong>file on disk where events are stored sequentially</strong></p><h4>2. Ordered</h4><ul><li>Events are written <strong>sequentially</strong></li><li>Each event gets a unique <strong>offset</strong></li></ul><p>Example:</p><pre>Offset 0 → Order Created<br>Offset 1 → Payment Initiated<br>Offset 2 → Payment Completed</pre><p>👉 Consumers always read <strong>in increasing offset order.</strong></p><h4>2.1 How Offsets Work</h4><p>Each partition maintains its own <strong>offset counter</strong></p><ul><li>Offset starts from 0</li><li>Increments for every new event</li><li>Unique <strong>only within that partition</strong></li><li><em>Offsets are </em><strong><em>NOT global across partitions</em></strong></li></ul><h4>2.2 Ordering Guarantees</h4><p>Ordering in Kafka is not global — it is only guaranteed at the partition level.</p><p><strong>Within a Partition</strong> → ORDER IS GUARANTEED ✅</p><pre>Partition 0:<br>Offset 0 → Event A<br>Offset 1 → Event B<br>Offset 2 → Event C</pre><p>👉 Kafka guarantees: A → B → C (always in order)</p><p><strong>Across Partitions</strong> → ORDER IS NOT GUARANTEED ❌</p><pre>Partition 0: Event A (Offset 0)<br>Partition 1: Event X (Offset 0)</pre><p>We <strong>cannot say</strong>:</p><ul><li>A happened before X</li><li>or X happened before A</li></ul><p>Because:</p><ul><li>Each partition has its <strong>own timeline</strong></li></ul><h4>3. Append-Only</h4><ul><li>Kafka <strong>never updates or deletes events in place</strong></li><li>New events are always <strong>appended at the end</strong></li></ul><p>👉 This makes Kafka:</p><ul><li>Fast (sequential disk writes)</li><li>Reliable (immutable log)</li><li>Replayable (you can re-read history)</li></ul><p>Imagine This Scenario 🧠</p><p>You are a consumer service. You tell Kafka:</p><blockquote><em>“</em>I want to read events from <strong>Partition P0 between Offset 450 to 800<em>”</em></strong></blockquote><p>Now imagine if kafka stored everything in one massive file.</p><pre>order-events-0.log (size = 5GB)</pre><blockquote>Kafka would need to search through a huge file — inefficient. ❌</blockquote><p><strong>This is where concept of Segment and Indexing comes into picture</strong></p><h3>🔹 What is a Segment ?</h3><p>A <strong>segment</strong> is a chunk of a partition log. Partition log file is further divided into multiple smaller files called segments.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/459/1*VaXv9LbuST9UKZ0XdqZmLQ.png" /></figure><p>With the help of segments, instead of scanning a huge log file, kafka can directly jump to the correct segment instead of scanning the entire log.</p><p><strong>⚠️ But There’s Still a Problem</strong><br>We can control the segment size. Let’s say if we keep the segment size as 1GB. Sequential search in 1GB segment file will be slow. So, we still don’t have an efficient way to jump to an exact offset inside the segment.</p><blockquote>🚀 Solution: <strong>Index Files</strong></blockquote><h3>🔹 What are Index files ?</h3><figure><img alt="" src="https://cdn-images-1.medium.com/max/484/1*I9tqLDyu5KUrD01hBJQaMQ.png" /></figure><p>Each segment has an <strong>index file. </strong>It does not store full data, rather it stores mapping of offsets to byte position for faster lookup.</p><p>Key points about Indexing :</p><ul><li><strong><em>Not every offset is indexed : </em></strong><em>Kafka does not store an index entry for each message.</em></li><li><strong><em>Sparse indexing is used: </em></strong><em>Instead of indexing every record, Kafka creates an index entry after every N bytes written to the log file.</em></li><li><strong><em>This saves memory and also helps in faster lookup.</em></strong></li></ul><p>🧠 Understanding Indexing with an Example</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*asXY0Y6D5XD7-l-PE1WrYQ.png" /></figure><p><strong>Step 1: </strong>Kafka writes events <strong>sequentially</strong> into the log file.</p><p><strong>Step 2: </strong>When does Kafka create an index entry?</p><ul><li>We are given <strong>Index Interval = 4096 bytes</strong></li><li>It means <em>Kafka will create an index entry </em><strong><em>only after ~4096 bytes of data are written</em></strong></li></ul><p><strong>Step 3:</strong> Applying the rule</p><ul><li>After Offset 3 → Total = <strong>3800 bytes</strong> ❌ (still less than 4096)</li><li>After Offset 4 → Total = <strong>4300 bytes</strong> ✅ (crosses 4096)</li></ul><p>So, Kafka creates <strong>one index entry at Offset 4.</strong></p><h4>🔍 How Lookup Works</h4><ol><li>Kafka finds the closest index entry</li><li>Jumps to that position in the log</li><li>Performs a small sequential scan</li></ol><h3>🎯 How Kafka Decides Which Partition to Send an Event To</h3><p>1️⃣ Key-Based Partitioning</p><p>Events with the <strong>same key always go to the same partition. </strong>This ensures:</p><p>✔️ <strong>Ordering is guaranteed for that key</strong><br>✔️ All related events stay together</p><p><strong>Publish Example</strong></p><pre>{<br>  &quot;topic&quot;: &quot;order-events&quot;,<br>  &quot;key&quot;: &quot;orderId&quot;,<br>  &quot;event&quot;: orderJson<br>}</pre><p><strong>How it works ?</strong></p><pre>partition = hash(key) % number_of_partitions</pre><p><strong>Example:</strong></p><pre>E0 → hash(&quot;123&quot;) % 3 = Partition 1  <br>E1 → hash(&quot;456&quot;) % 3 = Partition 2  <br>E2 → hash(&quot;123&quot;) % 3 = Partition 1</pre><p>👉 Notice:</p><ul><li>Same key &quot;123&quot; → always goes to <strong>Partition 1</strong></li><li>So ordering is preserved for that event.</li></ul><p>2️⃣ Round Robin Partitioning</p><p>Events are distributed <strong>evenly across partitions</strong></p><p>✔️ Better load balancing<br> ❌ No ordering guarantee</p><pre>{<br>  &quot;topic&quot;: &quot;order-events&quot;,<br>  &quot;key&quot;: null,<br>  &quot;event&quot;: orderJson<br>}</pre><pre>E0 → Partition 0  <br>E1 → Partition 1  <br>E2 → Partition 2  <br>E3 → Partition 0  </pre><p>3️⃣ Custom Partitioning</p><p>You can define your <strong>own logic</strong> to decide partitions</p><pre>If country = &quot;India&quot; → Partition 0  <br>If country = &quot;US&quot; → Partition 1  </pre><blockquote><strong>Important takeaway :</strong> Partitioning strategy directly impacts ordering, scalability, and system design decisions.</blockquote><blockquote>“We’ve now seen how Kafka efficiently stores and retrieves data. But a bigger question remains —</blockquote><blockquote><strong>Who manages all this data? Who reads it? And how does Kafka scale across machines?</strong></blockquote><blockquote>In the next part, we’ll answer these questions by diving into Kafka brokers, clusters, the controller, consumers, and consumer groups, and understand how Kafka evolved from ZooKeeper to KRaft.”</blockquote><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=b599be4ff0c6" width="1" height="1" alt="">]]></content:encoded>
        </item>
        <item>
            <title><![CDATA[Understanding Event Streaming and Kafka Architecture]]></title>
            <link>https://medium.com/@msinghw3/understanding-event-streaming-and-kafka-architecture-a74e954e52ba?source=rss-1cd2e423dd83------2</link>
            <guid isPermaLink="false">https://medium.com/p/a74e954e52ba</guid>
            <category><![CDATA[kafka-architecture]]></category>
            <category><![CDATA[distributed-systems]]></category>
            <category><![CDATA[kafka]]></category>
            <category><![CDATA[event-driven-architecture]]></category>
            <category><![CDATA[system-design-interview]]></category>
            <dc:creator><![CDATA[Manjinder Singh]]></dc:creator>
            <pubDate>Mon, 13 Apr 2026 19:15:15 GMT</pubDate>
            <atom:updated>2026-04-13T19:15:15.396Z</atom:updated>
            <content:encoded><![CDATA[<figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*VeOK0FQbmW4dvNHHOvhMyQ.png" /></figure><p>When teams adopt <strong>Event-Driven Architecture (EDA)</strong>, they usually implement one of two messaging models:</p><ol><li><strong>Publish / Subscribe (Pub-Sub)</strong></li><li><strong>Event Streaming</strong></li></ol><p>Both models allow services to communicate <strong>asynchronously,</strong> but they behave very differently when it comes to <strong>message storage, durability, and replayability</strong>.</p><h3>1️⃣ Publish–Subscribe (Pub/Sub)</h3><p>In the <strong>Pub/Sub model</strong>, producers publish events to a messaging system, and those events are immediately delivered to <strong>active subscribers</strong>. Once the message is delivered, the system <strong>does not retain the event history</strong>.</p><h4>Key Characteristics</h4><ul><li>If a new consumer joins later, it <strong>cannot read previously published messages</strong>.</li><li>Messages are delivered only to currently <strong>active consumers.</strong></li><li>Events are typically <strong>not stored long-term. </strong>It is designed for <strong>real-time delivery </strong>of events.</li></ul><blockquote>Example : <strong>RabbitMQ</strong></blockquote><h3>2️⃣ Event Streaming</h3><p>Event streaming takes a different approach. Instead of deleting messages after delivery, events are <strong>persisted in an append-only log</strong>.<br>These events remain available for a configurable retention period and<strong> </strong>sometimes they can even be stored indefinitely.</p><h4>Key Characteristics</h4><ul><li>Multiple consumers can read the same data independently</li><li>Consumers track their own <strong>read position (offset).</strong></li><li>Events can be <strong>replayed for debugging, recovery, or analytics.</strong></li><li>Historical data remains available for <strong>new consumers</strong></li></ul><blockquote>Example : <strong>Apache Kafka</strong></blockquote><h3>What is Kafka?</h3><p>Apache Kafka is a <strong>distributed event streaming platform</strong> designed to handle large volumes of real-time data.</p><p>It allows systems to :</p><p>• Publish streams of events<br>• Store them durably<br>• Process them in real time<br>• Replay them whenever need.</p><h4>Core Components of Kafka</h4><p>Now that we understand <strong>event streaming and why Kafka is used</strong>, let’s look at the core components that make Kafka scalable and reliable.</p><p>At a high level, Kafka works with these fundamental concepts:</p><ul><li>Producers</li><li>Topics</li><li>Partitions</li><li>Offsets</li><li>Broker</li><li>Consumer &amp; Consumer Groups</li><li>Kafka Cluster</li><li>Controller</li><li>Kraft/Zookeeper</li></ul><h4>1. Producer</h4><p>A <strong>producer</strong> is an application that <strong>publishes events to Kafka</strong>.</p><p>For example:</p><ul><li>Order Service → publishes OrderCreated</li><li>Payment Service → publishes PaymentCompleted</li><li>Inventory Service → publishes StockUpdated</li></ul><p>The producer simply <strong>sends events to a Kafka topic</strong>. It does not care who consumes the event.</p><p>The producer’s responsibility is only to:</p><ul><li>Create the event</li><li>Choose a topic</li><li>Send the event to Kafka</li></ul><p>Kafka then takes care of <strong>storing and delivering the event to consumers</strong>.</p><h4>2. Topic</h4><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*J0uVRv5Gh-cqhrNlVNUezg.png" /></figure><p>A <strong>topic</strong> is a logical category or stream where events are stored. You can think of a topic like a <strong>folder that groups similar events together</strong>.</p><p>Example topics:</p><ul><li>order-events</li><li>payment-events</li><li>user-events</li><li>notification-events</li></ul><p>Multiple producers can write to the <strong>same topic</strong>, and multiple consumers can read from it independently.</p><p>For example:</p><p>order-events topic may receive events from:</p><ul><li>Order Service</li><li>Checkout Service</li><li>Mobile App</li></ul><p>And it may be consumed by:</p><ul><li>Payment Service</li><li>Inventory Service</li><li>Notification Service</li></ul><p>Topics help to organise event streams in Kafka.</p><h4>Conclusion</h4><p>We covered the first two core components of Kafka. These components form the foundation of how events are <strong>published and organized</strong> in Kafka.</p><blockquote>👉 In the next article of this series, we will explore:</blockquote><blockquote>• How Kafka partitions enable parallel processing<br>• How segments inside partitions store events on disk<br>• How index files allow Kafka to locate events efficiently<br>• Different partitioning strategies used by producers</blockquote><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=a74e954e52ba" width="1" height="1" alt="">]]></content:encoded>
        </item>
        <item>
            <title><![CDATA[Event-Driven Architecture]]></title>
            <link>https://medium.com/@msinghw3/event-driven-architecture-911e51c3fbee?source=rss-1cd2e423dd83------2</link>
            <guid isPermaLink="false">https://medium.com/p/911e51c3fbee</guid>
            <category><![CDATA[java]]></category>
            <category><![CDATA[kafka]]></category>
            <category><![CDATA[event-driven-architecture]]></category>
            <category><![CDATA[ecommerce]]></category>
            <category><![CDATA[microservices]]></category>
            <dc:creator><![CDATA[Manjinder Singh]]></dc:creator>
            <pubDate>Sat, 11 Apr 2026 19:04:04 GMT</pubDate>
            <atom:updated>2026-04-11T19:04:04.921Z</atom:updated>
            <content:encoded><![CDATA[<figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*pHcu8u0tIIoYyPviTY_ufg.png" /></figure><p>Modern software systems are typically built using one of three architectural styles: <strong>monolithic, microservices, or serverless</strong>.</p><p>In this article, we will focus on <strong>microservice architectures</strong>, where multiple independent services collaborate to complete a workflow.</p><p>But <strong>how those services communicate</strong> determines whether the system will scale smoothly… or collapse under load.</p><p>Two common approaches exist:</p><p>1️⃣ Synchronous communication (REST chains)<strong><br></strong>2️⃣ Event-Driven Architecture (EDA)</p><h3>What is Event-Driven Architecture ?</h3><p><strong>Event-Driven Architecture (EDA)</strong> is a design style where services communicate by <strong>publishing and consuming events </strong>instead of calling each other directly.</p><blockquote><strong>Key Idea :</strong> Services do not call each other directly.</blockquote><p>Instead, they publish events, and other services decide whether they want to react to them. This removes tight dependencies between services.</p><pre>Service A → Event → Broker → Service B </pre><h3>What is an Event ?</h3><p>An <strong>event</strong> represents a <strong>fact that something already happened in the system</strong>.</p><p>Examples:</p><pre>OrderCreated<br>PaymentCompleted<br>UserRegistered<br>ItemShipped</pre><p>Events are always <strong>past-tense facts</strong>, not commands.</p><h3>How Services Communicate in a Typical REST Microservice System</h3><p>Let’s consider a simple<strong> e-commerce order </strong>workflow.<strong> </strong>When a user places an order:</p><ol><li>Order Service receives request</li><li>Order Service calls Inventory Service to check availability of item</li><li>Order Service calls Payment Service</li><li>Order Service calls Notification Service</li></ol><figure><img alt="" src="https://cdn-images-1.medium.com/max/658/1*cD--dsrFA2lDy3fn0sE0Ig.png" /></figure><h3>Problems with Synchronous Service Chains</h3><p>This design works well for small systems. But as systems grow, several problems appear. Below are a few of them.</p><blockquote><strong>1️⃣ Availability Problem</strong></blockquote><p>All services must be available <strong>at the same time</strong>. If <strong>one service is down</strong>, the entire request fails.</p><pre>OrderService → PaymentService ❌ down<br>Result: Order fails</pre><blockquote><strong>2️⃣ Latency Accumulation</strong></blockquote><p>Total request time becomes the <strong>sum of all service latencies</strong>. As services increase, latency grows.</p><pre>Inventory Service : 80ms<br>Payment Service   : 120ms<br>Notification      : 50ms<br>-------------------------<br>Total             : 250ms</pre><blockquote><strong>3️⃣ Cascading Failures</strong></blockquote><p>One slow service can cause a chain reaction. <strong>Example:</strong></p><pre>PaymentService slow<br>↓<br>OrderService waits<br>↓<br>Thread pool fills<br>↓<br>Entire system slows down</pre><blockquote>4️⃣ Tight Coupling</blockquote><p>OrderService must know:</p><ul><li>Payment API</li><li>Inventory API</li><li>Notification API</li><li>Response formats</li></ul><p>This creates <strong>tight dependencies</strong>.</p><blockquote>5️⃣ Scaling Limitations</blockquote><p>If <strong>notification traffic increases</strong>, we still need to scale <strong>OrderService</strong>, even though the extra load is unrelated.</p><h3>How Event-Driven Architecture Solve above problems</h3><p>Now let’s redesign the same system using <strong>EDA</strong>.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/666/1*Qp6SWbVOgU-6XsJviFNx6Q.png" /></figure><p>Is the above diagram flow of data:</p><p>1️⃣ User places order User sends request to OrderService</p><p>2️⃣ OrderService receives the request OrderService:</p><ul><li>Checks real-time inventory availability</li><li>If items are available:</li><li>Saves order in database</li><li>Sets OrderStatus = PENDING The response is returned immediately. The user does not wait for payment or notifications.</li></ul><p>3️⃣ OrderService publishes OrderCreatedevent to Event Router / Kafka</p><p>4️⃣ Event Router distributes the event Services listening to this event consume it. Example consumers:</p><ul><li>Payment Service</li><li>Inventory Service</li></ul><p>5️⃣ Services process the event asynchronously and in parallel. Both services process the event independently.</p><p><strong>Inventory Service:</strong></p><pre>Reserve items<br>Publish: InventoryReserved event</pre><p><strong>Payment Service:</strong></p><pre>Process payment<br>Publish: PaymentSuccess event</pre><p>6️⃣ OrderService listens for completion events</p><p>OrderService consumes:</p><p>• PaymentSuccess<br>• InventoryReserved</p><p>Once both events are received:</p><pre>Update Order Status = COMPLETED</pre><p>7️⃣ Notification Service listens to <strong>OrderCompleted event </strong>and sends notifications via <strong><em>Email and SMS.</em></strong></p><h3>Challenges of Event-Driven Systems</h3><p>EDA introduces its own complexities. Below listed are few</p><ul><li><strong>Eventual Consistency : </strong>Data may take time to become consistent across services.</li><li><strong>Duplicate Events : </strong>Most brokers guarantee <strong>at-least-once delivery,</strong> however, there is a possibility of duplicate events. Consumers must handle duplicates.</li><li><strong>Ordering Issues : </strong>Events may arrive <strong>out of order.</strong></li><li><strong>Schema Evolution : </strong>Changing event structure can break consumers. Versioning is necessary.</li><li><strong>Poison Messages : </strong>Bad messages can block queues. Dead-letter queues are required</li><li><strong>Operational Monitoring </strong>Teams must monitor <strong><em>consumer lag, throughput, partition health, broker storage</em></strong></li></ul><h3>When Should You Use Event-Driven Architecture?</h3><p>EDA works best when:</p><h4>1. One event triggers multiple independent actions</h4><pre>OrderPlaced<br>↓<br>Reserve Inventory<br>Charge Payment<br>Send Email<br>Update Analytics</pre><h4>2. Long Running Workflows</h4><pre>Order → Payment → Shipment → Delivery</pre><p>These steps can take time and may fail midway.</p><h4>3. Eventual Consistency is Acceptable</h4><p>If small delays are acceptable between services.</p><h4>4. Real-Time Data Processing</h4><p>Examples:</p><ul><li>analytics pipelines</li><li>fraud detection</li><li>recommendation systems</li></ul><h3>When NOT to Use EDA</h3><p>Some operations require <strong>strong consistency</strong>.</p><p>Example:</p><pre>Transfer money between bank accounts</pre><p>These should use <strong>synchronous transactions</strong>.</p><blockquote>Rule of thumb:</blockquote><pre>Critical operations → Synchronous<br>Non-critical workflows → Event-Driven</pre><blockquote>Event-Driven Architecture allows systems to scale independently, remain resilient under load, and evolve without tight dependencies.</blockquote><blockquote>However, it also introduces new challenges that engineers must design for carefully.</blockquote><blockquote>In the next article, we will dive deeper into <strong>Apache Kafka and how it powers modern event-driven systems.</strong></blockquote><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=911e51c3fbee" width="1" height="1" alt="">]]></content:encoded>
        </item>
    </channel>
</rss>