Files

keda: autoscaling Spring Boot Kafka consumers

Companion module for the ankurm.com post Autoscaling Spring Boot Kafka Consumers on Kubernetes with KEDA. It answers one question with running code: what does the consumer do when something else decides how many copies of it exist?

Verified against

Component Version Source
JDK Temurin 25.0.5.1 (LTS) output/00-versions.txt
Spring Boot 4.2.0 Maven Central
Spring for Apache Kafka 4.2.0 output/00-versions.txt
Apache Kafka (clients and the embedded KRaft broker) 4.4.0 output/00-versions.txt
KEDA (documentation and scaler source read) 2.21 / v2.21.1 keda.sh docs, apache_kafka_scaler.go

What was run, and what was not

Part Status
Consumer app, group lag, rebalances, SIGTERM vs SIGKILL, Spring graceful shutdown Run against a real in-JVM Kafka broker; output in output/
KedaMath (scaler + HPA arithmetic) A model written from the KEDA docs and source, run against real broker lag. KEDA itself was not run
k8s/*.yaml Not applied to any cluster (the build sandbox has no Docker daemon). YAML syntax checked only

Run it

export JAVA_HOME=/path/to/jdk-25
./scripts/run-all.sh        # regenerates output/*.txt; the tests assert the same numbers

Timing figures (rebalance milliseconds, the SIGKILL takeover time) drift between runs; the counts are asserted.

Layout

Path What it is
src/main/java/com/ankurm/keda/OrderListener.java The consumer: one listener thread per pod
src/main/java/com/ankurm/keda/GroupLag.java Group lag as the KEDA Kafka scaler defines it
src/main/java/com/ankurm/keda/KedaMath.java Model of lagThreshold, partition cap, activation, HPA ceiling
src/main/resources/application.yml Graceful shutdown, cooperative rebalancing, manual commits
src/test/java/com/ankurm/keda/KedaExperimentsTest.java The five experiments
src/test/java/com/ankurm/keda/CrashableConsumer.java Child-JVM consumer that is really SIGTERMed / SIGKILLed
k8s/deployment.yaml, k8s/scaledobject.yaml Manifests (not executed)

Captured output

File Shows
output/00-versions.txt JDK, Maven and resolved Kafka artifacts
output/01-partition-cap.txt 6 partitions, 1/3/6/8 consumers: the 7th and 8th get nothing
output/02-lag-to-replicas.txt Real lag from the broker turned into replica counts; skew; remainder edge; activation
output/03-rebalance-cost.txt Partitions a pod gives up when KEDA adds two pods: eager vs cooperative vs KIP-848
output/04-sigterm-vs-sigkill.txt Takeover time and replayed records after SIGTERM and SIGKILL
output/05-spring-graceful-shutdown.txt Spring Boot context close mid-batch: no record processed twice

MIT licensed (see the repository root).