Add keda: Spring Boot 4.2.0 Kafka consumers under KEDA autoscaling (partition cap, lag maths, rebalances, SIGTERM vs SIGKILL)

Co-Authored-By: Claude Sonnet 5.5 <[email protected]>
Claude-Session: https://claude.ai/code/session_017ic4b69H5rTewUKPyhGS8r
This commit is contained in:
Claude
2026-12-02 00:07:44 +05:30
parent 5facfdc212
commit 0b31b49571
24 changed files with 1212 additions and 0 deletions
+12
View File
@@ -0,0 +1,12 @@
# environment
openjdk version "25.0.5.1" 2026-11-17 LTS
OpenJDK Runtime Environment Temurin-25.0.5.1+2 (build 25.0.5.1+2-LTS)
OpenJDK 64-Bit Server VM Temurin-25.0.5.1+2 (build 25.0.5.1+2-LTS, mixed mode, sharing)
Apache Maven 3.9.11 (3e54c93a704957b63ee3494413a2b544fd3d825b)
# resolved dependencies
org.apache.kafka:kafka-clients:jar:4.4.0:compile -- module kafka.clients (auto)
org.apache.kafka:kafka-clients:jar:test-fixtures:4.4.0:test -- module kafka.clients (auto)
org.apache.kafka:kafka-clients:jar:test:4.4.0:test -- module kafka.clients (auto)
org.springframework.boot:spring-boot-starter-kafka:jar:4.2.0:compile -- module spring.boot.starter.kafka [auto]
org.springframework.kafka:spring-kafka-test:jar:4.2.0:test -- module spring.kafka.test [auto]
org.springframework.kafka:spring-kafka:jar:4.2.0:compile -- module spring.kafka [auto]
+31
View File
@@ -0,0 +1,31 @@
Topic 'cap' has 6 partitions. N consumers join one group; who gets what?
N=1 consumers:
c1 -> partitions [0, 1, 2, 3, 4, 5]
idle consumers: 0
N=3 consumers:
c1 -> partitions [0, 1]
c2 -> partitions [2, 3]
c3 -> partitions [4, 5]
idle consumers: 0
N=6 consumers:
c1 -> partitions [1]
c2 -> partitions [2]
c3 -> partitions [3]
c4 -> partitions [4]
c5 -> partitions [5]
c6 -> partitions [0]
idle consumers: 0
N=8 consumers:
c1 -> partitions [0]
c2 -> partitions [1]
c3 -> partitions [2]
c4 -> partitions [3]
c5 -> partitions [4]
c6 -> partitions [5]
c7 -> partitions []
c8 -> partitions []
idle consumers: 2
+17
View File
@@ -0,0 +1,17 @@
Lag is read from the broker (log end offset - committed offset, summed).
Replica counts come from KedaMath, a model of the scaler + HPA (KEDA is not run here).
A) uniform: {0=100, 1=100, 2=100, 3=100, 4=100, 5=100} total=600
lagThreshold=100 -> total=600 metric=600 desired=6
lagThreshold=50 (wants 12, 6 partitions) -> total=600 metric=300 desired=6
same, allowIdleConsumers=true -> total=600 metric=600 desired=12
B) skewed (hot key): {0=600, 1=0, 2=0, 3=0, 4=0, 5=0} total=600
default -> total=600 desired=6 (5 of 6 pods would sit idle)
limitToPartitionsWithLag=true -> total=600 desired=1
C) remainder edge: {0=150, 1=100, 2=100, 3=100, 4=100, 5=100} total=650
650 / 100 = 6 (integer) is not > 6, so the clamp is skipped -> metric=650 desired=7 on 6 partitions
D) activation (minReplicaCount=0, activationLagThreshold=0):
lag=0 -> active=false desired=0; lag=1 -> active=true desired=1
+15
View File
@@ -0,0 +1,15 @@
1 pod owns 6 partitions; KEDA adds 2 pods. How many partitions does pod 1 give up?
classic + RangeAssignor (eager)
final: A=[0, 1] B=[2, 3] C=[4, 5]
partitions A had to give up: 6 of 6; time until last assignment change: ~189 ms
classic + CooperativeStickyAssignor
final: A=[0, 1] B=[2, 4] C=[3, 5]
partitions A had to give up: 4 of 6; time until last assignment change: ~3197 ms
group.protocol=consumer (KIP-848)
final: A=[0, 1] B=[3, 4] C=[2, 5]
partitions A had to give up: 4 of 6; time until last assignment change: ~5120 ms
Timing varies per run; the revoked-partition counts are what the test asserts.
+15
View File
@@ -0,0 +1,15 @@
A consumer JVM processes records (50 ms each) and commits every 20. We stop it after it has
processed 30 records (20 committed + 10 in flight). A standby consumer is already in the group.
session.timeout.ms=6000 (the broker's minimum).
SIGTERM (what Kubernetes sends on scale-down):
child had processed 31, committed 20 at the moment of the signal
standby's first record: offset 20 after 108 ms
records processed twice (child processed 31, standby restarted at 20): 11
SIGKILL (pod crash, node loss):
child had processed 30, committed 20 at the moment of the signal
standby's first record: offset 20 after 5481 ms
records processed twice (child processed 30, standby restarted at 20): 10
Both replay the uncommitted tail (at-least-once). Only SIGKILL also waits out session.timeout.ms.
@@ -0,0 +1,8 @@
The real Spring Boot app (OrderListener, 50 ms of work per record, max.poll.records=20).
200 records on one partition. Close the context after >= 25 records were processed.
pod A: context.close() took ~740 ms; processed=40; committed offset=40
pod B (same group): processed=160 until lag=0
records processed twice across A and B: 0
Spring's container finished the in-flight batch and committed it before stopping.