Reduce memory use of the JVM services

The services now run on Java 17, with a periodic G1 GC that returns unused
heap to the OS when idle. Previously Java 11 kept the committed heap, so
events-processors held 5.5 GB while its live data was ~250 MB.

All RocksDB state stores now share one bounded block cache and write buffer
(ROCKSDB_TOTAL_MEMORY_MB, default 256 MB), instead of each of the ~45 stores
having its own.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
This commit is contained in:
Lazzexus 2026-11-03 10:31:04 +01:00
parent e1e59fe66c
commit ba9cbe5afe
7 changed files with 78 additions and 7 deletions

View file

@ -29,7 +29,11 @@ services:
# merge repeated map-chunk updates for 10s before forwarding them (128MB cache per topology)
KAFKA_STREAMS_COMMIT_INTERVAL_MS: "10000"
KAFKA_STREAMS_CACHE_MAX_BYTES: "134217728"
JAVA_OPTS: "-Xmx4g"
# Map chunk bursts (e.g. `/kafkatorio chunks 30`) need a large heap for a while. G1 hands
# unused heap back to the OS when idle (G1PeriodicGCInterval).
JAVA_OPTS: "-Xmx3g -XX:G1PeriodicGCInterval=60000 -XX:MaxHeapFreeRatio=30 -XX:MinHeapFreeRatio=10"
# off-heap memory shared by all RocksDB state stores
ROCKSDB_TOTAL_MEMORY_MB: "256"
networks: [ kafkatorio-nw ]
depends_on: [ infra-kafka ]
volumes:
@ -46,7 +50,7 @@ services:
restart: unless-stopped
environment:
KAFKA_BOOTSTRAP_SERVERS: "kafka:29092"
JAVA_OPTS: "-Xmx256m"
JAVA_OPTS: "-Xmx256m -XX:G1PeriodicGCInterval=60000"
SOCKET_SERVER_HOST: "0.0.0.0"
SOCKET_SERVER_PORT: "9002"
ports:
@ -65,7 +69,7 @@ services:
environment:
KAFKA_BOOTSTRAP_SERVERS: "kafka:29092"
KAFKA_STREAMS_NUM_THREADS: "3"
JAVA_OPTS: "-Xmx1536m"
JAVA_OPTS: "-Xmx768m -XX:G1PeriodicGCInterval=60000 -XX:MaxHeapFreeRatio=30 -XX:MinHeapFreeRatio=10"
WEB_SERVER_PORT: "80"
networks: [ kafkatorio-nw ]
depends_on: [ infra-kafka ]

View file

@ -0,0 +1,65 @@
package dev.adamko.kafkatorio.processor.core
import org.apache.kafka.streams.state.RocksDBConfigSetter
import org.rocksdb.BlockBasedTableConfig
import org.rocksdb.Cache
import org.rocksdb.LRUCache
import org.rocksdb.Options
import org.rocksdb.RocksDB
import org.rocksdb.WriteBufferManager
/**
* Make all RocksDB state stores share one fixed amount of off-heap memory.
*
* By default every store (and Kafkatorio has dozens: one per store, per partition) gets its own
* block cache and memtables, so memory use grows with the number of stores. Here the block cache
* and the memtables of every store come out of one shared cache.
*
* Based on the Kafka Streams docs, "Bounded memory usage".
*
* Enable with the Kafka Streams config `rocksdb.config.setter`.
*/
class BoundedMemoryRocksDBConfig : RocksDBConfigSetter {
override fun setConfig(storeName: String, options: Options, configs: Map<String, Any>) {
val tableConfig = options.tableFormatConfig() as BlockBasedTableConfig
tableConfig.setBlockCache(cache)
// count index and filter blocks towards the cache, but keep them in it
tableConfig.setCacheIndexAndFilterBlocks(true)
tableConfig.setCacheIndexAndFilterBlocksWithHighPriority(true)
tableConfig.setPinTopLevelIndexAndFilter(true)
options.setTableFormatConfig(tableConfig)
options.setWriteBufferManager(writeBufferManager)
options.setMaxWriteBufferNumber(MEMTABLES_PER_STORE)
options.setWriteBufferSize(MEMTABLE_SIZE)
}
override fun close(storeName: String, options: Options) {
// the cache and write buffer manager are shared by all stores, so don't close them
}
companion object {
private const val MB = 1024L * 1024L
/** Total off-heap memory for all stores, including the memtables */
private val TOTAL_MEMORY = (System.getenv("ROCKSDB_TOTAL_MEMORY_MB")?.toLongOrNull() ?: 256L) * MB
/** How much of [TOTAL_MEMORY] may be used by memtables (writes not yet flushed to disk) */
private val TOTAL_MEMTABLE_MEMORY = TOTAL_MEMORY / 4
private const val INDEX_FILTER_BLOCK_RATIO = 0.1
private const val MEMTABLES_PER_STORE = 2
private const val MEMTABLE_SIZE = 8 * MB
init {
// the shared cache is created before any store, so the native library might not be loaded yet
RocksDB.loadLibrary()
}
private val cache: Cache = LRUCache(TOTAL_MEMORY, -1, false, INDEX_FILTER_BLOCK_RATIO)
private val writeBufferManager = WriteBufferManager(TOTAL_MEMTABLE_MEMORY, cache)
}
}

View file

@ -27,6 +27,8 @@ kafka-streams-config:
# commit.interval.ms, or transactions time out, get fenced, and the work is redone.
producer.transaction.timeout.ms: ${KAFKA_STREAMS_TRANSACTION_TIMEOUT_MS:-60000}
processing.guarantee: exactly_once_v2
# all RocksDB state stores share one bounded amount of memory (ROCKSDB_TOTAL_MEMORY_MB, default 256)
rocksdb.config.setter: dev.adamko.kafkatorio.processor.core.BoundedMemoryRocksDBConfig
auto.offset.reset: earliest
default.key.serde: org.apache.kafka.common.serialization.Serdes$StringSerde
default.value.serde: org.apache.kafka.common.serialization.Serdes$StringSerde

View file

@ -1,6 +1,6 @@
# syntax=docker/dockerfile:1
FROM eclipse-temurin:11-jdk-jammy
FROM eclipse-temurin:17-jdk-jammy
ARG KAFKATORIO_VERSION
ARG APP_FILES="build/"

View file

@ -1,6 +1,6 @@
# syntax=docker/dockerfile:1
FROM eclipse-temurin:11-jdk-jammy
FROM eclipse-temurin:17-jdk-jammy
ARG KAFKATORIO_VERSION
ARG APP_FILES="build/"

View file

@ -1,6 +1,6 @@
# syntax=docker/dockerfile:1
FROM eclipse-temurin:11-jdk-jammy
FROM eclipse-temurin:17-jdk-jammy
ARG KAFKATORIO_VERSION
ARG APP_FILES="build/"

View file

@ -1,6 +1,6 @@
# syntax=docker/dockerfile:1
FROM eclipse-temurin:11-jdk-jammy
FROM eclipse-temurin:17-jdk-jammy
ARG KAFKATORIO_VERSION