# Kafka without ZooKeeper, for new clusters only

> Kafka 3.3 marked KRaft production ready, which ends a long plan to delete a dependency. A weekend with a three-node quorum on a laptop, and a careful reading of what "production ready" was not claiming yet.

- Author: Deyan Peev (https://deyanpeev.com)
- Published: 2022-12-10
- Tags: Apache Kafka, Distributed Systems, Apache ZooKeeper, Docker, Consensus
- URL: https://deyanpeev.com/posts/kafka-without-zookeeper-for-new-clusters-only

Apache Kafka has been on my list of things to understand properly for years,
filed next to Spark under "read about, never run". What moved it up the list was
KIP-833, which marked KRaft as production ready in the 3.3 release. With that,
removing ZooKeeper from Kafka stopped being a roadmap item and became something
you could choose.

A small piece of trivia first, because it caught me out while downloading. 3.3.0
was never announced. A significant bug turned up after the artifacts had already
been pushed to Apache and Maven Central, so they skipped the announcement and
3.3.1 went out with the fix instead. If you go looking, 3.3.1 is the release,
and 3.3.0 is the one you're told not to use.

# ZooKeeper's job in a Kafka cluster

Kafka's data, the topics themselves, was never in ZooKeeper. What lived there
was the metadata about the cluster: which topics exist, how many partitions they
have, which broker leads each one, which replicas are in sync, the configuration
overrides, the ACLs. ZooKeeper also ran the controller election that decides
which broker is in charge of reacting to all of that.

So operating Kafka meant operating two distributed systems. You had a second
quorum with its own odd-numbered node count, failure modes, version
compatibility matrix, monitoring and place in the runbook, and all of it in
service of a system whose entire pitch is that it is a replicated log you can
rely on.

There was a scaling consequence too, the kind you see in incidents, not in
diagrams. When the controller failed over, the new controller had to load the
cluster's metadata out of ZooKeeper before it could do anything. That load is
proportional to how much metadata there is, so recovery time grew with the
number of partitions.

# What KRaft does instead

The metadata moves into Kafka, specifically into an internal topic,
`__cluster_metadata`, replicated across a quorum of controller nodes by a Raft
implementation built for the purpose.

It's the model Kafka already sells to everybody else. Metadata stops being a
tree of state you fetch and becomes a log of events with offsets, and brokers
follow that log the way a consumer follows a topic. A broker that has been away
doesn't reload the world, it asks for what happened after the offset it last
saw. A new controller does not rebuild from a cold read, because the quorum
already has the log.

When you set one up, nodes now have roles:

```properties
process.roles=broker,controller
node.id=1
controller.quorum.voters=1@kafka-1:9093,2@kafka-2:9093,3@kafka-3:9093
listeners=PLAINTEXT://:9092,CONTROLLER://:9093
controller.listener.names=CONTROLLER
```

Running controller and broker in the same process is convenient on a laptop.
It's not what you would do for anything real, where the controllers are their
own small nodes.

The surprising step, if you come from a ZooKeeper cluster, is that storage now
has to be initialised with a cluster ID before a broker will start:

```bash
KAFKA_CLUSTER_ID="$(bin/kafka-storage.sh random-uuid)"

bin/kafka-storage.sh format \
  --config config/kraft/server.properties \
  --cluster-id "$KAFKA_CLUSTER_ID"

bin/kafka-server-start.sh config/kraft/server.properties
```

There's no external registry to derive identity from any more, so the cluster
has to be given one. Miss that and the broker refuses to start, with a message
that is, to be fair, perfectly clear.

# What "production ready" was not claiming

The KIP states the limits plainly.

KRaft is production ready **for new clusters**. There is no migration path from
an existing ZooKeeper-based cluster in this release. That work is planned for a
later 3.x release, which will act as a bridge between the two worlds. So if you
already run Kafka there's nothing to act on yet, you wait for that release.

There are also features that KRaft mode didn't yet support at 3.3, and any one
of them rules it out if you depend on it:

- JBOD configurations with multiple storage directories per broker.
- Delegation tokens.
- Changing certain dynamic configurations on a standalone controller.

The direction beyond that is clear enough. The plan on paper ends with ZooKeeper
support removed entirely in Kafka 4.0. But "the plan says it will go" and "you
can move today" are separated by a good deal of release engineering.

So, as of December, if you're standing up a new cluster and you don't need JBOD
or delegation tokens, KRaft is a reasonable choice and it removes an entire
system from what you have to operate. If you have an existing cluster, nothing
about your Monday changes.

# Kafka already had the log

I don't run Kafka in production and this was a weekend with three containers on
a laptop, so I'm not going to pretend to have opinions about it at scale.

What I'll keep is how they fixed it. A system that outsources its consensus to
another system inherits that system's operations, its failure modes and its
upgrade schedule, and the cost shows up years later in the runbook and in
recovery times, long after the original decision. Kafka already had the
primitive it needed. What it sells to its users is an ordered, replicated,
offset-addressable log. Its own metadata was a state-synchronisation problem
until somebody modelled it as a log, and then most of the hard parts became
something the project already knew how to do well.
