A complete, production-grade collection of Apache Kafka Streams design patterns and architectural recipes. Modernized for Java 17+ (compatible with Java 21) and Kafka Streams 3.8.0, covering everything from fundamental stateless transformations to advanced topics like Exactly-Once Semantics (EOS V2), low-level Processor APIs (KIP-405), Foreign Key Table Joins (KIP-213), Window Suppression, Versioned State Stores (KIP-889), and RocksDB off-heap performance tuning.
- Java: 17 LTS / 21 LTS
- Apache Kafka Streams:
3.8.0(KRaft Mode, EOS V2) - Logging: SLF4J 2.x + Logback Classic (CVE-free)
- Testing: JUnit 5 + AssertJ +
TopologyTestDriver - Serialization: Generic Jackson
JsonSerde<T> - Infrastructure: Single-node KRaft Kafka Broker + AKHQ Web UI + Redis + MariaDB via Docker Compose
All patterns are organized by package under src/main/java/com/learning/kafkastreaming/.
BasicOperations.java: Stateless stream transformations (filter,map,flatMapValues,selectKey,peek,foreach).BranchAndMerge.java: Fluent stream splitting and predicate routing usingsplit().branch().defaultBranch()(KIP-632) and stream merging.
CountAndReduce.java: Stateful counting and custom reduction on grouped streams.AggregationPatterns.java: Global unbounded aggregations, Session Windows (inactivity gaps with session mergers), and Hopping Windows.SlidingWindowsPattern.java: Continuous sliding window aggregations (SlidingWindows.ofTimeDifferenceAndGrace) for real-time anomaly/fraud detection.WindowSuppression.java: Suppressing intermediate windowed updates withSuppressed.untilWindowCloses(...)to emit only final aggregated results.KTableOperations.java: Primary-key upsert streams, filtering tables, and table-to-stream changelogs.
StreamStreamJoin.java: Inner, Left, and Outer windowed joins (JoinWindows.ofTimeDifferenceWithNoGrace) between twoKStreams.StreamTableJoin.java: Co-partitionedKStream-KTablejoins and broadcastKStream-GlobalKTablelookups.TableTableJoin.java: Continuous stateful primary-key joins between twoKTables.ForeignKeyJoin.java: Non-primary key foreign joins (KIP-213) (e.g.OrdersreferencingCustomersbycustomerId).
ProcessorApiExample.java: Modern typedProcessor<KIn, VIn, KOut, VOut>API (KIP-405) withRecord<K, V>and wall-clock timePunctuatorscheduling.HeaderPropagationProcessor.java: Inspecting, tracing, and mutating Kafka Record Headers across topology stages.VersionedStateStoreExample.java: Point-in-time temporal queries using Kafka 3.5+ versioned key-value stores (Stores.persistentVersionedKeyValueStore).RocksDBConfigTuning.java: CustomRocksDBConfigSettertuning block caches (LRU), write buffers, LZ4 compression, and compaction parallelism.ExactlyOnceProcessing.java: Configuring Exactly-Once Semantics V2 (processing.guarantee=exactly_once_v2).ErrorHandling.java: Deserialization exception handlers (LogAndContinue), Dead Letter Queue (DLQ) routing, andStreamsUncaughtExceptionHandlerthread recovery.InteractiveQueries.java: Exposing materialized state stores for direct RPC queries viaReadOnlyKeyValueStore.CustomPartitioner.java: CustomStreamPartitioner(KIP-699) and custom payloadTimestampExtractor.
TopologyTestDriverTest.java: Fast, cluster-free topology testing with JUnit 5 and AssertJ.AggregationTopologyTest.java: Validating windowed aggregations and directWindowStorestate verification.ForeignKeyJoinTopologyTest.java: Verifying foreign key table join updates.WindowSuppressionTopologyTest.java: Verifying intermediate record suppression and window close events.
- Chapter 2 (Analytics): Windowed aggregations writing 5-second summaries to MariaDB JDBC sink.
- Chapter 3 (Alerts): Real-time threshold monitoring and high-volume anomaly detection.
- Chapter 4 (Leaderboards): Updating game scores in real-time and pushing to a Redis Sorted Set.
- Chapter 5 (Predictions): Stream enrichment using external HTTP/NLP sentiment prediction.
- Chapter 6 (Views): Keeping track of topics with maximum views using hopping windows and Redis.
Start Kafka in KRaft mode (no ZooKeeper), AKHQ Web UI, Redis, and MariaDB:
docker compose up -d- AKHQ Web Console: http://localhost:8080
- Kafka Bootstrap:
localhost:9092 - Redis:
localhost:6379 - MariaDB:
localhost:3306
You can compile and run all unit tests without any running Kafka cluster:
cd KafkaStreamsStreamingPatterns
mvn clean testRun any example using Maven:
mvn exec:java -Dexec.mainClass="com.learning.kafkastreaming.basics.BasicOperations"
mvn exec:java -Dexec.mainClass="com.learning.kafkastreaming.joins.ForeignKeyJoin"
mvn exec:java -Dexec.mainClass="com.learning.kafkastreaming.stateful.WindowSuppression"