Configure Kafka Streams state stores and RocksDB optimization for high-performance streaming applications

Advanced 45 min May 16, 2026 686 views
Ubuntu 24.04 Debian 12 AlmaLinux 9 Rocky Linux 9

Configure Kafka Streams state stores with RocksDB optimization for high-performance streaming applications. Learn custom state store configurations, RocksDB tuning parameters, and monitoring techniques for production-grade stream processing.

Prerequisites

  • Java 11 or higher installed
  • Apache Kafka cluster running
  • Maven build tool
  • Minimum 4GB RAM available

What this solves

Kafka Streams applications rely on state stores for maintaining intermediate data during stream processing operations like aggregations, joins, and windowing. The default RocksDB state store configuration often becomes a performance bottleneck under high-throughput workloads. This tutorial shows you how to configure custom state stores, optimize RocksDB parameters, and monitor performance metrics to achieve production-grade streaming performance with lower latency and higher throughput.

Prerequisites and system setup

Update system packages

Start by updating your package manager to ensure you get the latest versions of all dependencies.

sudo apt update && sudo apt upgrade -y
sudo dnf update -y

Install Java runtime environment

Kafka Streams requires Java 11 or later. Install OpenJDK which provides excellent performance for streaming applications.

sudo apt install -y openjdk-17-jdk openjdk-17-jre
sudo dnf install -y java-17-openjdk java-17-openjdk-devel

Verify the Java installation:

java -version

Download and install Apache Kafka

Download the latest Kafka distribution with Scala 2.13 binaries for optimal performance.

cd /opt
sudo wget https://downloads.apache.org/kafka/2.8.2/kafka_2.13-2.8.2.tgz
sudo tar -xzf kafka_2.13-2.8.2.tgz
sudo mv kafka_2.13-2.8.2 kafka
sudo chown -R $USER:$USER /opt/kafka

Add Kafka binaries to your PATH:

export KAFKA_HOME=/opt/kafka
export PATH=$PATH:$KAFKA_HOME/bin
source ~/.bashrc

Start Kafka cluster

Start ZooKeeper and Kafka broker services for local development and testing.

cd /opt/kafka
# Start ZooKeeper
bin/zookeeper-server-start.sh -daemon config/zookeeper.properties
# Start Kafka broker
bin/kafka-server-start.sh -daemon config/server.properties

Verify the cluster is running:

bin/kafka-broker-api-versions.sh --bootstrap-server localhost:9092

Configure custom state store topology

Create Maven project structure

Set up a Maven project for your Kafka Streams application with RocksDB dependencies.

mkdir -p kafka-streams-optimization/src/main/java/com/example
cd kafka-streams-optimization

Create the Maven POM file with required dependencies:


Implement custom RocksDB state store configuration

Create a custom state store configuration class that optimizes RocksDB parameters for high-performance streaming.

package com.example;

import org.apache.kafka.streams.state.RocksDBConfigSetter;
import org.rocksdb.*;
import java.util.Map;

public class OptimizedRocksDBConfig implements RocksDBConfigSetter {
    
    @Override
    public void setConfig(String storeName, Options options, Map

Create Kafka Streams application with optimized state stores

Implement a streaming application that uses the custom RocksDB configuration for state stores.

package com.example;

import org.apache.kafka.common.serialization.Serdes;
import org.apache.kafka.streams.KafkaStreams;
import org.apache.kafka.streams.StreamsBuilder;
import org.apache.kafka.streams.StreamsConfig;
import org.apache.kafka.streams.kstream.*;
import org.apache.kafka.streams.state.Stores;

import java.time.Duration;
import java.util.Properties;

public class StreamsApplication {
    
    public static void main(String[] args) {
        Properties props = new Properties();
        props.put(StreamsConfig.APPLICATION_ID_CONFIG, "optimized-streams-app");
        props.put(StreamsConfig.BOOTSTRAP_SERVERS_CONFIG, "localhost:9092");
        props.put(StreamsConfig.DEFAULT_KEY_SERDE_CLASS_CONFIG, Serdes.String().getClass());
        props.put(StreamsConfig.DEFAULT_VALUE_SERDE_CLASS_CONFIG, Serdes.String().getClass());
        
        // Configure RocksDB optimization
        props.put(StreamsConfig.ROCKSDB_CONFIG_SETTER_CLASS_CONFIG, OptimizedRocksDBConfig.class);
        
        // Optimize processing threads
        props.put(StreamsConfig.NUM_STREAM_THREADS_CONFIG, Runtime.getRuntime().availableProcessors());
        
        // Configure commit interval for performance
        props.put(StreamsConfig.COMMIT_INTERVAL_MS_CONFIG, 10000);
        
        // Enable exactly-once semantics
        props.put(StreamsConfig.PROCESSING_GUARANTEE_CONFIG, StreamsConfig.EXACTLY_ONCE_V2);
        
        StreamsBuilder builder = new StreamsBuilder();
        
        // Example: Word count with optimized state store
        KStream

Create topics and compile application

Create the required Kafka topics and compile your streaming application.

# Create input and output topics
kafka-topics.sh --create --topic input-topic --bootstrap-server localhost:9092 --partitions 4 --replication-factor 1
kafka-topics.sh --create --topic output-topic --bootstrap-server localhost:9092 --partitions 4 --replication-factor 1

# Compile the application
mvn clean compile exec:java -Dexec.mainClass="com.example.StreamsApplication"

Advanced RocksDB tuning parameters

Configure memory-based optimizations

Create an advanced RocksDB configuration that optimizes memory usage patterns for different workload types.

package com.example;

import org.apache.kafka.streams.state.RocksDBConfigSetter;
import org.rocksdb.*;
import java.util.Map;

public class AdvancedRocksDBConfig implements RocksDBConfigSetter {
    
    private static final long BLOCK_CACHE_SIZE = 128 * 1024 * 1024L; // 128MB
    private static final long WRITE_BUFFER_SIZE = 64 * 1024 * 1024L; // 64MB
    private static final int MAX_WRITE_BUFFER_NUMBER = 6;
    
    @Override
    public void setConfig(String storeName, Options options, Map

Implement workload-specific state store configurations

Create different state store configurations optimized for specific streaming patterns like aggregations versus joins.

package com.example;

import org.apache.kafka.streams.state.RocksDBConfigSetter;
import org.rocksdb.*;
import java.util.Map;

public class WorkloadSpecificConfig implements RocksDBConfigSetter {
    
    @Override
    public void setConfig(String storeName, Options options, Map

Monitor RocksDB performance metrics

Create RocksDB metrics collection

Implement a metrics collector that exposes RocksDB statistics for monitoring and alerting.

package com.example;

import org.apache.kafka.streams.KafkaStreams;
import org.apache.kafka.streams.state.QueryableStoreTypes;
import org.apache.kafka.streams.state.ReadOnlyKeyValueStore;
import org.rocksdb.RocksDB;
import org.rocksdb.Statistics;
import org.rocksdb.TickerType;

import java.util.HashMap;
import java.util.Map;
import java.util.concurrent.Executors;
import java.util.concurrent.ScheduledExecutorService;
import java.util.concurrent.TimeUnit;

public class RocksDBMetricsCollector {
    
    private final KafkaStreams streams;
    private final ScheduledExecutorService scheduler;
    private final Map

Automated install script

Run this to automate the entire setup

¿Prefiere no gestionarlo usted mismo?

Gestionamos la infraestructura de empresas que dependen del tiempo de actividad. Totalmente gestionada, con un contacto fijo que conoce su entorno.

Tiene un contacto fijo que conoce su entorno

Róterdam 04:07 · accesible por mensaje, sin formulario de tickets