GraalVM native image

Compile your StoatFlow app to a GraalVM native binary — a one-line opt-in. The framework ships its own reflection/JNI/FFM metadata; you choose the GC, optionally PGO, and add metadata only for your own serde value classes and config classes.

A StoatFlow application compiles to a GraalVM native image — a standalone binary with fast start-up, a small footprint, and (with the right GC) steady-state CPU and tail latency on par with the JVM. The opt-in is one line, because the framework already ships the native-image metadata for everything it touches; the only per-app work is the metadata for your own serde value classes and configuration classes.

This is the most advanced part of the build. Set up a working JVM build first (Project setup); for most deployments the JVM container image is the simpler, better-trodden path. Reach for native when cold-start time or per-instance memory is the binding constraint — and for any pod of 256 MiB or less: a native image runs a stateless app in 128 MiB and a stateful one in 256 MiB.

One-line opt-in

A StoatFlow app doesn't start from a blank GraalVM configuration. The framework bundles its native-image metadata, discovered automatically by classpath. Turning native image on applies GraalVM's build plugin, the curated --initialize-at-run-time flags RocksDB and the Kafka SASL client need, and the nativeDockerBuild task:

// build.gradle.kts
stoatflow {
    mainClass.set("com.example.MainKt")
    nativeImage {
        enabled.set(true)
        gc.set("G1")        // Oracle GraalVM; omit for Serial GC (stateless only)
    }
}

Build it

The containerized build is the recommended path for any image you'll actually run. It compiles the native binary inside Docker from your prebuilt fat JAR, against a generic, repo-independent Dockerfile.native the build stages for you — so it pins the build-host glibc, wires the GC and the reproducible SOURCE_DATE_EPOCH, stages the heap entrypoint, and applies the runtimeTarget:

./gradlew nativeDockerBuild
# → <imageName>:native  +  <imageName>:<version>-native

# Quick local smoke-compile (host GraalVM, no Docker):
./gradlew nativeCompile

The containerized build stages a build context (build/stoatflow-native/ for Gradle) holding the prebuilt app.jar, the shared native-image.args argfile, the heap entrypoint, and a pgo/ directory, then runs native-image @native-image.args … against the bundled Dockerfile.native. Detected platform: linux/arm64 on Apple Silicon, otherwise linux/amd64.

The pipeline, stage by stage:

Why build in Docker? A native binary links the build host's glibc and needs that version (or newer) at runtime. The bundled Dockerfile.native builds on a pinned Oracle GraalVM image (native-image:25-ol9, Oracle Linux 9 / glibc 2.34) and runs on a distroless cc-debian12 base (glibc 2.36), so the binary doesn't crash-loop on a GLIBC_2.xx not found mismatch — and you get G1 + PGO, which Community Edition lacks. A raw host nativeCompile is best treated as a does-it-compile smoke check.
Native compilation is resource-hungry: budget 8 GB+ RAM and 15–20 minutes. The containerized build needs only Docker — no local GraalVM install.

Runtime target

runtimeTarget selects the final image stage:

  • runtime (default) — a cc-debian12:debug distroless base with a busybox shell so the container-aware heap entrypoint can run the launcher's half of the memory plan (recommended). It applies the JVM image's rules — the heap from the memory profile's brackets or STOATFLOW_HEAP_MB, the max(32 MiB, 5 % of M) headroom, -XX:MaxDirectMemorySize, -Djdk.nio.maxCachedBufferSize=262144, MALLOC_ARENA_MAX=2 — with two of its own, since a native image has no metaspace and no JIT: its own native memory is max(32 MiB, min(M / 8, 256 MiB)) instead of the JVM's max(160 MiB, min(M / 4, 512 MiB)), and the brackets' heap floor is 48 MiB instead of 64. It passes the result to the native binary as runtime options. Its log line starts StoatFlow native heap sizing:; with no memory total to read it falls back to -XX:MaximumHeapSizePercent=75. The docker { } block's memoryProfile and heapMb knobs (Maven: stoatflow.docker.memoryProfile, stoatflow.docker.heapMb) bake STOATFLOW_MEMORY_PROFILE and STOATFLOW_HEAP_MB into the native image as build args; both override at run time. See Docker → Container-aware heap sizing.
  • runtime-lean — a minimal cc-debian12 distroless base, no shell, no entrypoint: SubstrateVM sizes the heap itself. Smallest possible image. The engine still plans RocksDB from the container's limit and whatever heap SubstrateVM took; the direct-memory, NIO-cache and allocator bounds are yours to set.
stoatflow { nativeImage { runtimeTarget.set("runtime-lean") } }

Small pods

A native image is the choice for the smallest containers. Measured at about 50 records/s in local Docker, a stateless native app stayed at 16–35 MiB resident and a stateful one at 45–55 MiB, where the JVM build of the same apps took 160–215 MiB, most of it the JVM's own G1, metaspace and code cache. With the lean memory profile — memoryProfile.set("lean") in the Gradle docker { } block, stoatflow.docker.memoryProfile=lean in Maven, or STOATFLOW_MEMORY_PROFILE=lean at deploy time — the smallest documented pods are:

ApplicationNative imageJVM
Stateless128 MiB256 MiB
Stateful256 MiB512 MiB

The runtime target's entrypoint splits a 128 MiB stateless pod into 32 MiB for the image's own memory, a 48 MiB heap and a 32 MiB headroom, and a 256 MiB stateful one into 32, 64 and 32 MiB with a 128 MiB RocksDB pool. Below these sizes the engine WARNs at start. The plan at each size is in Defaults → Small pods, what to expect and tune in Tuning → Small pods.

The garbage collector matters most

The single most important native-image decision is the GC — far more than the foreign-function path or anything exotic.

  • GraalVM Community Edition ships only Serial GC for native image. Serial is fine for a stateless transform, but it cannot carry an allocation-heavy stateful (RocksDB) workload — under load a stateful app on Serial falls behind the input and never catches up (we measured a 112× end-to-end latency blowup).
  • Oracle GraalVM (free for production under the GFTC licence, and what the containerized build already uses) unlocks G1 and PGO. Switching Serial → G1 — one flag — takes a stateful app straight back to JVM parity.

The rule: stateless → Serial is acceptable; stateful → use G1. Set gc to G1 and the containerized build passes it through to native-image.

Memory is tunable, not free. Under Serial GC a native image is tiny. Under G1 the heap reservation is sized from -Xmx, so a tiny-heap stateless app can use more RSS than on the JVM. For those, pin a tight heap with STOATFLOW_HEAP_MB (an env override, no rebuild) to recover the win. Stateful apps, whose live footprint is larger, already land below the JVM's total memory. See Tuning for how GC interacts with commit cadence. The memory report works in a native image too, with one gap: a native image publishes no heap occupancy after GC, so the heap verdict reads unknown and fits stays empty — size the heap from the JVM build's report, or by the cache with the what-if.

The RocksDB backend: AUTO (FFM ⇄ JNI)

StoatFlow talks to RocksDB's C API through two interchangeable backends, and the faster one inverts between runtimes:

  • On the JVM, FFM wins. The JIT intrinsifies its critical(false) downcalls down to nearly a direct C call.
  • Under native image, JNI wins. SubstrateVM has no JIT, so that intrinsification never fires and a plain JNI call ends up cheaper (~8% less CPU, better P99 in our measurements).

So the default backend is AUTO: FFM on the JVM, JNI under native image. You don't configure anything — each runtime is routed to its faster path automatically, and the brittle FFM-under-native machinery is off the default path entirely.

Where reachability metadata lives

GraalVM's closed-world analysis needs metadata for everything reached via reflection, JNI, resources, and FFM foreign downcalls. StoatFlow ships this metadata inside the module JARs — not in per-app config files:

ModuleMetadata pathCovers
stoatflow-coreMETA-INF/native-image/io.stoatflow/core/Kafka serde reflection, the Kafka client, RocksDB (both the JNI bindings and the FFM foreign downcalls), JCTools, zstd
stoatflow-runtimeMETA-INF/native-image/io.stoatflow/runtime/the configuration layer and StoatFlow's own config classes, Logback, Micrometer/HdrHistogram, and the rest of the runtime stack

Any app that depends on stoatflow-runtime (and transitively stoatflow-core) inherits the correct metadata automatically through GraalVM's classpath discovery. You copy or generate nothing for the StoatFlow surface — only for your own types, next.

Custom serde metadata

The closed-world compiler still needs to know about your types — the value classes you serialize/deserialize — because anything reflective, JNI, or FFM must be declared up front, or it fails the first time that code path runs (never at build time).

The reliable way to generate it is the GraalVM tracing agent: run your app (or its tests) on the JVM with the agent attached, exercise every code path, and it writes the metadata. Drop the result under your module's resources so GraalVM discovers it by classpath:

# Run your app/tests on the JVM with the tracing agent, driving real traffic:
java -agentlib:native-image-agent=config-output-dir=src/main/resources/META-INF/native-image/com.example/word-count \
     --enable-native-access=ALL-UNNAMED -jar build/libs/word-count-all.jar
Drive every path before you trust the binary. Closed-world compilation moves failures from build time to first-record time. A MissingReflectionRegistrationError only surfaces when its code path first executes — so exercise your serdes, punctuators, joins, and error paths under the agent.

Your own configuration classes

StoatFlow's own config classes are registered. The classes your application binds its own sections to (Custom configuration) are read by reflection, so each needs a reflection entry in the application's src/main/resources/META-INF/native-image/<group>/<artifact>/reachability-metadata.json — a Kotlin data class and a Java record alike (a record's components are read through its accessors). List every class the bind reaches, nested ones included:

{
  "reflection": [
    {
      "type": "com.example.PaymentsDb",
      "allDeclaredConstructors": true,
      "allDeclaredMethods": true,
      "allDeclaredFields": true,
      "allPublicConstructors": true,
      "allPublicMethods": true,
      "allPublicFields": true
    }
  ]
}

In a native image, a bind that reaches a class with no entry fails, naming the class — it never binds silently to nothing.

Snares & gotchas

The discoveries that cost an afternoon each:

  • Avro + Schema Registry is the worst offender. Confluent's stack instantiates classes by name from its own config defaults (e.g. NullContextNameStrategy), Jackson-deserializes the registry's REST entities, and native image disables URL protocols by default — so the schema fetch fails until you add --enable-url-protocols=http,https. The tracing agent catches the config-default classes in one pass; it's the surer route for anything Avro.
  • Metadata only counts under a name GraalVM reads. The current format is reachability-metadata.json; the legacy files are reflect-config.json, jni-config.json, resource-config.json and their siblings. A file under any other name — a reflection-config.json, say — is silently ignored, with every registration in it. Keep your metadata in reachability-metadata.json.
  • PGO profiles only dump on clean shutdown. A streaming app runs forever, so extracting a .iprof takes plumbing — the bundled native entrypoint forwards SIGTERM to the child process so kill -TERM 1 drains and dumps the profile. (See the deep-dive.)

Profile-Guided Optimisation (PGO)

PGO is the CPU lever — a release-time, two-pass build (compile an instrumented binary, run it under representative load to collect a .iprof, rebuild with the profile). It's purely a CPU optimisation (latency/throughput are already at parity under G1) and trims the residual AOT-vs-JIT gap by another ~13–17%. It's Oracle-GraalVM-only and adds real build time, so treat it as a release step, not a per-change one. The build context includes a pgo/ directory where the collected profile is staged. The full recipe — instrumenting, extracting the profile from a long-running server, and rebuilding — is in the engineering deep-dive.

What the build passes to native-image

The static flags come from a single bundled native-image.args (identical for Gradle, Maven, and the containerized build); --gc and PGO are layered on dynamically. You write none of these by hand:

  • -H:+JNI, -H:+ReportExceptionStackTraces, -H:+AddAllCharsets, the Kotlin resource includes, --no-fallback.
  • --initialize-at-run-time for org.rocksdb, org.rocksdb.RocksDB, org.rocksdb.util.Environment and the Kafka SASL client authenticator — these load native libraries or read runtime state and must initialize when the binary starts, not when it's compiled. The FFM RocksDB bindings need the same; stoatflow-core declares that in its own bundled metadata.

If your own code (or a dependency) needs run-time initialization, add it via additionalInitializeAtRunTime (Gradle) / extend the native-maven-plugin <buildArgs> (Maven); drop a curated entry with removeInitializeAtRunTime. The full enumeration is in the Gradle plugin reference. The typical trigger is a build-time error: Classes that should be initialized at run time got initialized during image building — add the named class and rebuild.

Verify it runs

After building, run the image and check the same health and metrics endpoints as the JVM runtime:

docker run -d --name sf-native \
  -e KAFKA_BOOTSTRAP_SERVERS=host.docker.internal:9092 \
  -e STOATFLOW_LICENCE_KEY="sfl1...your licence from the onboarding email..." \
  -e STOATFLOW_LICENCE_ENVIRONMENT=prd-eu \
  -p 8080:8080 \
  my-app:native

curl -s localhost:8080/health/ready
curl -s localhost:8080/info

If the process exits at startup with Classes that should be initialized at run time got initialized during image building or a MissingReflectionRegistrationError, the fix is metadata: add the class to additionalInitializeAtRunTime, or register the reflective access via the tracing agent, then rebuild.

What's covered — and what isn't yet

Covered today (benchmarked under load): String, Protobuf, and Avro + Schema Registry serdes; stateless and stateful (RocksDB) topologies; exactly-once. With Oracle GraalVM + G1 + PGO, the apps we measured run within −4% to +10% CPU of the JVM, latency equal-or-better everywhere, and memory below the JVM on the stateful apps.

Not yet mapped — these pull in reflection/JNI surface that still needs native metadata:

  • Kafka client security — SASL (SCRAM, GSSAPI/Kerberos), OAuth bearer, and mTLS.
  • Compression codecs beyond zstd — snappy, lz4, gzip.

If you hit a MissingReflectionRegistrationError on one of these, the tracing agent is your first move — and let us know, so we can fold the metadata into the framework.

Knob reference

CapabilityGradle (stoatflow { nativeImage { … } })Maven (<properties> / build)
Enableenabled.set(true)activate -Pstoatflow-native
Garbage collectorgc.set("G1")<stoatflow.nativeImage.gc>G1</…>
Runtime image stageruntimeTarget.set("runtime-lean")<stoatflow.nativeImage.runtimeTarget>…</…>
Add --initialize-at-run-timeadditionalInitializeAtRunTime.add("…")extend the native-maven-plugin <buildArgs>
Drop a curated --initialize-at-run-timeremoveInitializeAtRunTime.add("…")<stoatflow.nativeImage.removeInitializeAtRunTime>…</…> (CSV)

Further reading