Keyboard shortcuts

Press or to navigate between chapters

Press S or / to search in the book

Press ? to show this help

Press Esc to hide this help

KafkaContainer

A single-node Kafka broker running in KRaft mode — no ZooKeeper.

Default image: apache/kafka:4.0.0 Guest port: 9092

MethodOnEffect
KafkaContainer::new()builderPinned default image.
KafkaContainer::with_image(image)builderCaller-chosen image.
.start()builder → Result<KafkaGuard>Boots the container.
.bootstrap_servers()guardPLAINTEXT://host:port bootstrap address.
.stop()guardStops and removes the container, releases its port.

Defaults baked in

This module sets a full KRaft single-node env block so the broker is usable with zero configuration: KAFKA_NODE_ID=1, KAFKA_PROCESS_ROLES=broker,controller, a self-quorum KAFKA_CONTROLLER_QUORUM_VOTERS, and KAFKA_OFFSETS_TOPIC_REPLICATION_FACTOR=1 (a single-node broker can’t satisfy a replication factor greater than 1).

The heap fix: the apache/kafka image defaults KAFKA_HEAP_OPTS to -Xmx1G, which exceeds microsandbox’s ~450 MB default microVM RAM and aborts the JVM with an “insufficient memory” error. This module overrides it to -Xmx256M -Xms256M — a single-node KRaft dev broker runs comfortably in a 256 MB heap, and the override is harmless on the Docker backend, which isn’t memory-constrained here. If you’re wrapping a different Kafka-family image yourself and it aborts on boot under RIGHTSIZE_BACKEND=microsandbox, check its default heap flags before reaching for with_memory_limit — see Files & Resources.

The advertised-listener rewrite: same trick as RedpandaContainer, simpler (one listener instead of two) — a with_spec_customizer hook rewrites KAFKA_ADVERTISED_LISTENERS to PLAINTEXT://127.0.0.1:<mapped host port> right before create(), once the mapped port is actually known.

Complete example

use rightsize_modules::KafkaContainer;

#[tokio::test]
async fn kafka_boots_and_advertises_a_reachable_port() -> rightsize::Result<()> {
    let guard = KafkaContainer::new().start().await?;

    // guard.bootstrap_servers() is a PLAINTEXT://127.0.0.1:<port> address usable by
    // any Kafka-protocol client crate.
    println!("bootstrap: {}", guard.bootstrap_servers());

    guard.stop().await?;
    Ok(())
}

Backend notes

The KAFKA_HEAP_OPTS override above is required for this module to boot under microsandbox’s default RAM — without it, boot fails outright. No with_memory_limit call is needed on top of the heap override; the reduced heap already fits comfortably.