ReviseAlgo Logo

Kafka Streams

What is Kafka Streams? Stream Processing vs Batch Processing

Lightweight client library for real-time stream transformation.

Introduction

Apache Kafka Streams is a client library for building real-time, stateful applications and microservices where the input and output data are stored in Kafka clusters. Unlike traditional big data processing frameworks, Kafka Streams is a standard Java/JVM library that runs directly inside your application process, requiring no dedicated computing cluster.

Why It Matters

Traditional batch systems process data in large, high-latency blocks, which is inadequate for real-time needs like fraud detection or live inventory tracking. While low-level Producer and Consumer APIs allow message transfer, they lack built-in mechanisms for stateful processing, stream joins, windowing, and fault-tolerant local aggregations. Kafka Streams provides these capabilities out-of-the-box in a highly scalable, fault-tolerant manner.

Real-World Analogy

Think of batch processing like a postal delivery truck that visits a farm once a day, collects all the harvested fruit, and drives it to the packaging warehouse. Think of stream processing (Kafka Streams) like a conveyor belt running continuously from the trees directly through sorting arms. Each sorting arm (processor) inspects, cleans, and redirects fruit in real-time as it slides past, without ever stopping the flow.

How It Works

Kafka Streams reads from one or more input topics, applies transformations defined by a developer-written topology, and writes the resulting records to output topics. It automatically handles partition assignments, coordinate group coordination, and recovery behind the scenes.

  1. Topology Definition: Developers use the DSL (Domain Specific Language) to define a graph of processing nodes.
  2. Task Assignment: The library creates Tasks based on the number of partitions in the input topics.
  3. Event Loop Polling: Stream threads continuously poll messages, process them through the topology, and commit progress.

Internal Architecture

Kafka Streams leverages partitions to structure parallelism. If an input topic has 6 partitions, you can run up to 6 tasks in parallel. These tasks can be distributed across multiple application instances. For stateful operations, tasks store data locally in RocksDB instances, and replicate state updates to internal Kafka changelog topics for fault tolerance.

Visual Explanation

Below is an ASCII representation of the Kafka Streams architecture, showing how partitions are mapped to local tasks in the client JVM:

[Kafka Topic Partitions] ───> [Partition 0]   [Partition 1]   [Partition 2]
                                    │               │               │
[Client Application JVM] ───────────┼───────────────┼───────────────┼───────────
  Stream Threads                    ▼               ▼               ▼
                                 [Task 0]        [Task 1]        [Task 2]
                                 (RocksDB)       (RocksDB)       (RocksDB)
            

Practical Example

Here is a basic Java snippet illustrating how to define a stateless filtering pipeline using Kafka Streams:

Common Mistakes

  • Assuming a dedicated cluster is required: Developers often confuse Kafka Streams with Apache Flink or Apache Spark, setting up external processing infrastructure instead of deploying it as standard application service instances.
  • Reusing APPLICATION_ID_CONFIG: Reusing application IDs across separate stream applications causes partition group assignment conflicts and offset commitment errors.

Quick Quiz

Q1: How does Kafka Streams scale and coordinate parallel processing across threads or instances?

It leverages Kafka consumer group protocols to automatically balance tasks and partition assignments.

Q2: Where does Kafka Streams store its local state for stateful operations?

In local RocksDB instances embedded directly in the application's process directory.

Scenario-Based Challenge

The Challenge: Transitioning from batch to streaming

Your company runs hourly cron jobs to process clickstream logs, resulting in delays. You are asked to transition this to a low-overhead, real-time pipeline. Why would you choose Kafka Streams over Spark Streaming for this?

Solution Tip: Spark Streaming requires a dedicated cluster (YARN/Mesos/Kubernetes) and adds operational complexity. Kafka Streams is a lightweight library that can be packaged directly inside your existing Spring Boot or JVM microservices and deployed using standard container pipelines, eliminating cluster overhead.

Debugging Exercise

Your application crashes during start up with a TopologyException: Invalid topology: Topic input-topic has already been registered.

The Fix: Check your StreamsBuilder setup. You are calling builder.stream("input-topic") multiple times in separate parts of your codebase, which violates topology uniqueness rules. Store the returned KStream instance in a variable and reuse it rather than calling builder.stream() repeatedly.

Interview Questions

1. Is Kafka Streams a push-based or pull-based system?

It is pull-based. It runs standard consumer client threads under the hood that continuously poll records from the Kafka brokers.

2. What happens to the local state (RocksDB) if a container restarts or crashes?

On startup, Kafka Streams checks for local checkpoints. If they are absent or corrupted, the task automatically rebuilds the state store by reading the topic's backup changelog topic from the brokers.

Production Considerations

RocksDB stores data off-heap. You must configure JVM settings to leave sufficient memory for the operating system and RocksDB caches to prevent Out Of Memory (OOM) kills.