Scrivio Interview library
ConceptFoundations6 min read · updated 12 November 2026

How do you run Kafka locally for development of a Spring Boot service today? Show the main settings of a single-node KRaft broker in Docker Compose and explain the listeners and advertised listeners settings.

Today we run a single Kafka broker in Docker in KRaft mode, because Kafka 4.0 removed ZooKeeper: one node with process.roles=broker,controller, started from the apache/kafka image with a node id, a controller quorum of one, listener settings and replication factors of 1 for the internal topics. listeners are the addresses the broker binds to, and advertised.listeners are the addresses it hands back to clients after the first connection, so they must be reachable from wherever the client runs. Spring Boot can start the compose file for us, or we use Testcontainers with @ServiceConnection at development time.

Why this question is asked

Every Kafka developer needs a broker on their machine, and the way to run one changed with Kafka 4.0. Older guides with a ZooKeeper container no longer apply, and the most common local problem, clients that connect once and then fail, comes from the listener settings that many people copy without understanding. Interviewers ask this because it shows whether we know current Kafka and whether we can debug our own environment. In this answer, we will recap what KRaft changed, write a single-node compose file setting by setting, explain listeners and advertised listeners with the bootstrap flow, and see how Spring Boot can start the broker for us.

1A Quick Recap: Kafka Without ZooKeeper

For most of its history, Kafka stored cluster metadata, such as which topics exist and which broker leads each partition, in a separate ZooKeeper cluster. KRaft, Kafka's own Raft-based consensus, moves that metadata into Kafka itself: a few nodes act as controllers that keep the metadata log, and the other nodes act as brokers that store data. Since Kafka 4.0, ZooKeeper mode is gone entirely.

In production, controllers and brokers usually run as separate nodes. For local development, one process can do both jobs, which is called combined mode and is set with process.roles=broker,controller. So a local Kafka is now a single container.

2A Single-Node Compose File

The official apache/kafka image reads broker settings from environment variables: each property is upper-cased, dots become underscores, and KAFKA_ is put in front, so node.id becomes KAFKA_NODE_ID. The image can even start with no variables using built-in single-node defaults, but writing them out shows what each one does.

YAML
services:
kafka:
image: apache/kafka:4.1.0
ports:
- "9092:9092"
environment:
KAFKA_NODE_ID: 1
KAFKA_PROCESS_ROLES: broker,controller
KAFKA_CONTROLLER_QUORUM_VOTERS: 1@localhost:9093 # node 1 is the only controller
KAFKA_LISTENERS: PLAINTEXT://:9092,CONTROLLER://:9093 # what the broker binds
KAFKA_ADVERTISED_LISTENERS: PLAINTEXT://localhost:9092 # what clients are told
KAFKA_CONTROLLER_LISTENER_NAMES: CONTROLLER
KAFKA_LISTENER_SECURITY_PROTOCOL_MAP: CONTROLLER:PLAINTEXT,PLAINTEXT:PLAINTEXT
KAFKA_OFFSETS_TOPIC_REPLICATION_FACTOR: 1 # only one broker exists
KAFKA_TRANSACTION_STATE_LOG_REPLICATION_FACTOR: 1
KAFKA_TRANSACTION_STATE_LOG_MIN_ISR: 1
KAFKA_GROUP_INITIAL_REBALANCE_DELAY_MS: 0 # faster group joins locally

Notice the replication factor lines. Kafka creates internal topics, __consumer_offsets for committed offsets and __transaction_state for transactions, with a replication factor of 3 by default. On a single broker that is impossible, so the topics cannot be created and consumer groups or transactions fail in confusing ways. Setting them to 1 is required for one node. The controller listener on port 9093 is only used inside the container, which is why it is not published as a port.

3Listeners and Advertised Listeners

These two settings confuse many people because they look alike. listeners says where the broker opens sockets: PLAINTEXT://:9092 means listen on port 9092 on all interfaces, under the listener name PLAINTEXT. advertised.listeners says what address the broker tells clients to use. They differ because of how Kafka clients connect.

A client first connects to any broker from its bootstrap.servers list and asks for metadata. The answer lists every broker and the address it advertises, and from then on the client connects to the broker that leads each partition using exactly those advertised addresses, not the bootstrap address. So the bootstrap address only has to work once; the advertised address has to work all the time, from the client's point of view.

Step throughBootstrap, then advertised addresses
Spring Boot appBroker
connect to bootstrap localhost:9092
metadata request
node 1 is at localhost:9092 (advertised)
connect to the advertised address for each leader
produce and fetch

5 messages in order. Play them, or step through them one at a time.

In our file the broker advertises localhost:9092. For a Spring Boot application running on the host, localhost:9092 reaches the published port of the container, so it works. For a client running in another container, localhost would mean that client's own container, and the connection would fail after bootstrap, which is why the follow-up question about two listeners matters.

4Letting Spring Boot Start the Broker

Spring Boot offers two ways to avoid starting Docker by hand. With the spring-boot-docker-compose dependency, Boot finds compose.yaml in the project, runs docker compose up when the application starts and can stop it again at shutdown. For some services Boot also creates the connection details automatically from the running containers; for Kafka, check your Boot version, and if it does not detect the broker, set spring.kafka.bootstrap-servers=localhost:9092.

The second way uses Testcontainers at development time. We write a small TestApplication in the test sources that starts our real application with an extra configuration declaring a KafkaContainer bean annotated with @ServiceConnection. Running TestApplication starts the container and wires the bootstrap servers automatically, with no compose file at all.

Java
@TestConfiguration(proxyBeanMethods = false)
class DevContainers {
@Bean @ServiceConnection
KafkaContainer kafka() { // org.testcontainers.kafka.KafkaContainer
return new KafkaContainer("apache/kafka:4.1.0");
}
}
public class TestApplication {
public static void main(String[] args) {
SpringApplication.from(OrderServiceApplication::main) // our real main class
.with(DevContainers.class)
.run(args); // broker starts, app connects to it
}
}

5Follow-up questions

Why might you want two listeners on a local broker?

Because clients reach the broker from two different networks. An application running on the host needs to be told localhost:9092, while an application or tool running in another container on the Docker network needs to be told the service name, such as kafka:29092. One advertised address cannot serve both, so we define two listeners with different names and ports, for example PLAINTEXT on 29092 advertised as kafka:29092 and PLAINTEXT_HOST on 9092 advertised as localhost:9092, map both to the PLAINTEXT protocol in listener.security.protocol.map, and choose one for broker-to-broker traffic with inter.broker.listener.name. A separate listener is also handy for testing SSL or SASL locally next to a plain one.

Do I still need a ZooKeeper container for older examples?

Not with Kafka 4.0 or later, which no longer supports ZooKeeper at all. An example that starts ZooKeeper is written for Kafka 3.x or older, and its settings, such as KAFKA_ZOOKEEPER_CONNECT, do not apply to a KRaft broker.

6Conclusion

In this answer, we ran a local Kafka as one KRaft node in combined broker and controller mode, since Kafka 4.0 removed ZooKeeper, using the apache/kafka image with a node id, a single-voter controller quorum, listener settings and replication factors of 1 for the internal topics. We saw that listeners are the bind addresses and advertised.listeners are what clients receive in metadata and must be able to reach. Finally, Spring Boot can start the broker through its Docker Compose support or through Testcontainers in a TestApplication with @ServiceConnection.

What to say in the interviewAbout 90 seconds aloud

Open with

Since Kafka 4.0 there is no ZooKeeper, so locally I run a single broker in KRaft mode, with one process acting as both broker and controller, process roles broker comma controller.

Then, if they want more

I use the apache/kafka image in Docker Compose with a node id of 1, a controller quorum voters setting that names node 1 on the controller port, listeners for PLAINTEXT on 9092 and CONTROLLER on 9093, the controller listener name, and the security protocol map.

On a single node I also set the replication factor of the internal topics, the consumer offsets topic and the transaction state log, to 1, otherwise groups and transactions fail. The image can also run with its built-in single-node defaults. The important distinction is between listeners and advertised listeners. Listeners are where the broker binds its sockets.

Advertised listeners are the addresses the broker returns in metadata after the client bootstraps, and the client uses those for all later connections. So advertised listeners must be reachable from wherever the client runs; localhost works for an app on my host but not for an app in another container.

For convenience, Spring Boot can start the compose file with the docker compose support, or I use Testcontainers in a TestApplication with a ServiceConnection KafkaContainer, so the bootstrap servers are wired automatically.

About this answer

Last updated
12 November 2026
Versions it names
Kafka 4.0
Checked
Its structure and format are checked automatically. Use it as a carefully prepared study note: where a detail matters, confirm it against the documentation below. What the checks cover
Where the question comes from
Written by Scrivio for practice. It is not taken from an interview.
Report an issue with this answer

Copy the line below and send it to the author with what you found. It carries the address of this page.

Issue with "How do you run Kafka locally for development of a Spring Boot service today? Show the...": https://scrivio-interview-library.netlify.app/spring-boot/kafka/ca5fc7d7f6/