<?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 Ahmet Zumberoglu on Medium]]></title>
        <description><![CDATA[Stories by Ahmet Zumberoglu on Medium]]></description>
        <link>https://medium.com/@ahmetzumber?source=rss-d5361271c1c6------2</link>
        <image>
            <url>https://cdn-images-1.medium.com/fit/c/150/150/1*idwrQ0A7-FqH7MghbmBq2g.jpeg</url>
            <title>Stories by Ahmet Zumberoglu on Medium</title>
            <link>https://medium.com/@ahmetzumber?source=rss-d5361271c1c6------2</link>
        </image>
        <generator>Medium</generator>
        <lastBuildDate>Sat, 28 Nov 2026 11:48:41 GMT</lastBuildDate>
        <atom:link href="https://medium.com/@ahmetzumber/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[Stateful Stream Processing with Java: The Symphony of Kafka Streams and RocksDB]]></title>
            <link>https://medium.com/@ahmetzumber/stateful-stream-processing-with-java-the-symphony-of-kafka-streams-and-rocksdb-36f14dea8935?source=rss-d5361271c1c6------2</link>
            <guid isPermaLink="false">https://medium.com/p/36f14dea8935</guid>
            <category><![CDATA[kafka-streams]]></category>
            <category><![CDATA[kafka]]></category>
            <category><![CDATA[java]]></category>
            <category><![CDATA[rocksdb]]></category>
            <dc:creator><![CDATA[Ahmet Zumberoglu]]></dc:creator>
            <pubDate>Mon, 29 Dec 2025 10:20:23 GMT</pubDate>
            <atom:updated>2025-12-29T10:20:23.940Z</atom:updated>
            <content:encoded><![CDATA[<p>In the microservices world, we often encounter various system-level challenges, and there are usually multiple approaches to solving each of them. In some scenarios, a single data source is not sufficient; instead, we need to combine data coming from different services to produce meaningful and consistent outputs. This is exactly where Kafka Streams emerges as a powerful solution for real-time and stateful data processing.</p><p><strong><em>Why we need Kafka Streams?</em></strong></p><p>In typical microservice architectures, data usually ends up in different Kafka topics, at different times, and sometimes in different formats. When we want to combine this data, we often end up writing extra services or storing everything in a database first and processing it later. Over time, this adds latency and makes the system harder to manage. Kafka Streams helps by processing these events in real time, directly as they flow through the system.</p><p><strong><em>Pub/Sub vs Streams</em></strong></p><p>You can solve a lot of problems with plain Kafka Consumers and Producers, no doubt about that. But once you start reading from multiple topics, doing joins or aggregations, things get messy pretty fast. Managing state, offsets, and failure cases becomes a real burden. Kafka Streams takes care of most of these details so you can focus more on the actual business logic.</p><p><strong><em>Stateful Processing and RocksDB</em></strong></p><p>Most real-life problems depend on what happened before. For example, a product’s stock level doesn’t make sense without knowing the orders and returns that came in earlier. Kafka Streams supports stateful processing for exactly these kinds of cases and stores the state locally using RocksDB. Since each instance manages its own state, you get good performance and easy horizontal scaling.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*yL37SLv4rixwLfpbeZXrHQ.png" /></figure><p>When you need to keep state in a Kafka Streams application, that state has to live somewhere. This is where RocksDB comes in. Kafka Streams uses RocksDB as an embedded key-value store, and each Streams instance has its own local RocksDB. Instead of calling a remote database over the network, the state is stored right on the same machine where the app is running.</p><p>RocksDB is disk-based, but that doesn’t mean it’s slow. Data is first kept in memory and then flushed to disk. This gives you good performance and also makes sure the state survives application restarts. Thanks to this setup, Kafka Streams can safely handle aggregations, joins, and windowed operations.</p><p><strong><em>What if Kafka Stream instance goes down?</em></strong></p><p>So what happens in a distributed systems if each instance keeps its own state? Kafka Streams handles this with changelog topics. Every change in RocksDB is also written to Kafka as an event. If an instance crashes, a new one can rebuild the state by reading from these changelog topics. So even though RocksDB is local, the state is still safe and recoverable thanks to Kafka.</p><pre>package com.example;<br><br>import com.example.config.Topics;<br>import com.example.model.*;<br>import com.example.serializer.Serdes;<br>import lombok.extern.slf4j.Slf4j;<br>import org.apache.kafka.streams.StreamsBuilder;<br>import org.apache.kafka.streams.kstream.*;<br>import java.util.ArrayList;<br><br>@Slf4j<br>public class ProductStream {<br>    private final StreamsBuilder streamBuilder;<br>    private final Topics topics;<br><br>    public ProductStream(StreamsBuilder streamBuilder, Topics topics) {<br>        this.streamBuilder = streamBuilder;<br>        this.topics = topics;<br>    }<br><br>    public void configure() {<br>        var productTable = streamBuilder<br>                        .stream(topics.getProducts(), Consumed.with(Serdes.String(), Serdes.Of(RawProduct.class)))<br>                        .filter((k, v) -&gt; v != null)<br>                        .mapValues(OutputProduct::fromRawProduct)<br>                        .toTable(Materialized.with(Serdes.String(), Serdes.Of(OutputProduct.class)));<br><br>        var productCategories = streamBuilder<br>                        .stream(topics.getProductCategories(), Consumed.with(Serdes.String(), Serdes.Of(RawCategory.class)))<br>                        .filter((k, v) -&gt; v != null)<br>                        .toTable(Materialized.with(Serdes.String(), Serdes.Of(RawCategory.class)));<br><br>        var productColors = streamBuilder<br>                        .stream(topics.getProductColors(), Consumed.with(Serdes.String(), Serdes.Of(RawProductColor.class)))<br>                        .filter((k, v) -&gt; v != null)<br>                        .selectKey((k, v) -&gt; v.getProductId().toString())<br>                        .groupByKey(Grouped.with(Serdes.String(), Serdes.Of(RawProductColor.class)))<br>                        .aggregate(ArrayList::new, RawProductColor::aggregate, Materialized.with(Serdes.String(), Serdes.OfList(RawProductColor.class)));<br><br>        var productSizes = streamBuilder<br>                        .stream(topics.getProductSizes(), Consumed.with(Serdes.String(), Serdes.Of(RawProductSize.class)))<br>                        .filter((k, v) -&gt; v != null)<br>                        .selectKey((k, v) -&gt; v.getProductId().toString())<br>                        .groupByKey(Grouped.with(Serdes.String(), Serdes.Of(RawProductSize.class)))<br>                        .aggregate(ArrayList::new, RawProductSize::aggregate, Materialized.with(Serdes.String(), Serdes.OfList(RawProductSize.class)));<br><br>        productTable<br>            .leftJoin(productCategories, (p) -&gt; p.getCategoryId().toString(), OutputProduct::setCategory, Materialized.with(Serdes.String(), Serdes.Of(OutputProduct.class)))<br>            .leftJoin(productColors, OutputProduct::setProductColors, Materialized.with(Serdes.String(), Serdes.Of(OutputProduct.class)))<br>            .leftJoin(productSizes, OutputProduct::setProductSizes, Materialized.with(Serdes.String(), Serdes.Of(OutputProduct.class)))<br>            .toStream()<br>            .peek((k, v) -&gt; log.info(&quot;Publishing: {} --&gt; {}&quot;, k, v))<br>            .to(topics.getOutputProducts(), Produced.with(Serdes.String(), Serdes.Of(OutputProduct.class)));<br>    }<br>}</pre><p>🔗 <strong>Source code: </strong><a href="https://github.com/ahmetzumber/product-stream">https://github.com/ahmetzumber/product-stream</a></p><p>In this example, the first step to produce the desired output using <strong>product</strong>, <strong>product_category</strong>, <strong>product_color</strong>, and <strong>product_variant</strong> data is consuming records from each input topic. While consuming the data, we can apply any filtering if needed, depending on the business rules. Key selection becomes even more important here, because all joins and aggregations across these sources rely entirely on matching keys.</p><p>When choosing the main table, we always go with the most unique entity — in this case, the <strong>variant_id</strong>. A single product can have multiple variants, such as different sizes or colors, and if we want each variant to be represented as a separate document, using variant_id is the only safe option. If we were to choose product_id as the main key instead, variants belonging to the same product could overwrite each other during grouping or aggregation. This would result in incomplete or incorrect product data in the final output.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*4Lz6j39pEBfhKETSzr9_kQ.png" /></figure><p>Once the business logic is clearly defined, designing the topology correctly becomes one of the most critical parts of building a Kafka Streams application. At this point, a few things really matter:</p><ul><li>picking the right key is crucial, because every join and aggregation depends on it</li><li>deciding which stream or table should be the main one — usually the one with the most unique identifier</li><li>avoiding situations where records overwrite each other because of a poor key choice</li><li>shaping joins and aggregations based on how the business actually works, not just what’s easiest to implement</li><li>thinking about late or out-of-order events and how they might affect your final result</li><li>keeping the topology as simple as possible, so it’s easier to reason about and debug later.</li></ul><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=36f14dea8935" width="1" height="1" alt="">]]></content:encoded>
        </item>
    </channel>
</rss>