In brief
The first decisions with Apache Kafka concern five things: how to name and split topics, which key to choose for partitioning, how long to keep events, how to describe and version their schema, and which delivery guarantees are needed. These choices are hard to change once a dozen or so consumers work on a topic. Installing the cluster itself is the smaller problem: you can buy it as a managed service or run it yourself.
Topics: one business fact, one owner
A topic in Kafka is a named stream of events of one kind. Good practice: one topic corresponds to one business fact and has one team that publishes it. A topic called “payments.posted” at a bank or “services.activated” at an operator is clear to anyone who joins the project a year later. A topic called “crm-integration-2” says nothing.
It is worth agreeing on a naming standard from the start, for example domain, fact, version, and keeping to it across the whole company. On top of that come basic questions about each topic:
- who publishes it and who is responsible for schema compatibility;
- whether the event contains personal data, and if so, who is allowed to read it;
- how long events should remain available;
- whether the topic keeps the history of events or only the latest state of each object.
Partitions and the key: ordering and parallelism
Kafka guarantees event ordering only within a single partition. Events with the same key land in the same partition, so the choice of key decides what will be processed in order. For payments the key is often the account number, for orders the order number, for sensors on a production line the machine identifier.
The number of partitions also sets the upper limit of parallelism: within one consumer group, each partition is read by one consumer at a time. A topic with six partitions can serve at most six parallel consumers in a group.
Two pitfalls appear most often:
- A hot key. One corporate client generates half the traffic and its partition becomes a bottleneck. A composite key or a separate topic for such traffic helps.
- Changing the number of partitions later. Partitions can be added, but this changes the assignment of keys to partitions, so ordering for existing keys is no longer preserved across the old and new layout. It is better to allow headroom at the start.
Streams with very high volume, for example events from an operator's network, are covered separately in the article Real-time network events.
Retention, compaction and replay
Kafka stores events for a set time or up to a set size per topic, regardless of whether anyone has already read them. This sets it apart from a classic queue and gives two possibilities: a new system can load the history from the beginning, and a consumer after an error can step back and process events again.
The second mode is log compaction. Kafka then keeps at least the latest event for each key and removes older versions. Such a topic works like a current state, for example a product catalog or a dictionary of branches, which every consumer can rebuild on its own side.
Retention is chosen to match real replay needs, not “forever, just in case”. Long retention costs disk space, and for personal data it makes it harder to meet the obligation to erase data. A simple calculation: a topic that receives a steady 2 MB/s with 7-day retention takes about 1.2 TB per copy, and with three replicas about 3.6 TB, before adding headroom for traffic peaks.
Schemas and event contracts
An event without a described schema is a contract nobody has signed. Renaming a field at the sender breaks consumers the sender does not even know about. That is why events are described in a format with a schema (Avro, Protobuf or JSON Schema) and schemas are kept in a registry that checks the compatibility of a new version with the previous one. A schema registry is not part of Apache Kafka itself, so you need to choose a separate product, for example Confluent Schema Registry or Apicurio Registry.
The simplest evolution rule: add new fields as optional, and do not remove fields or change their type without a new topic version. Compatibility is checked by the CI pipeline before a change reaches production, in the same way as in API contract tests.
Delivery guarantees and idempotency
By default, assume that a consumer will receive an event at least once, meaning sometimes twice, for example after a restart before the position (offset) is committed. The consumer must therefore be idempotent: processing the same payment twice must not post it twice. A unique event identifier and a record of processed identifiers on the consumer side are usually enough.
On the producer side, Kafka has an idempotent mode and transactions, which provide exactly-once semantics for “read from Kafka, write to Kafka” processing. For data whose loss is unacceptable, the producer sends with acknowledgment from all in-sync replicas (`acks=all`), and the topic is replicated across several brokers with a minimum number of in-sync replicas set.
Self-managed or a managed service
Since version 4.0, Kafka runs only in KRaft mode, with its own metadata management mechanism and no separate ZooKeeper cluster. Operations are simpler than they used to be, but still include upgrades, monitoring consumer lag, planning disk space and recovery after a failure.
A managed cloud service takes most of this work off the team. Your own cluster makes sense when data cannot leave the data center, the volume is large and steady, or the company already runs a platform, for example on Kubernetes. A broader comparison is in the article Kubernetes in the enterprise: when it makes sense. If Kafka is to replace an existing integration bus, start with the article ESB or event-driven architecture and the scenario replacing an ESB in payments.
Questions and answers
How many partitions should a topic have?
Enough to serve the target number of parallel consumers with headroom, but not hundreds “just in case”, because every partition means files on the broker and a cost during recovery. The starting point is the measured throughput of one consumer and the expected traffic peak. Choose the number once, with headroom, because adding partitions later changes the assignment of keys.
Can Kafka be a system's database?
Kafka durably stores events and lets you rebuild state from them, which is used by, among others, event sourcing. It does not replace a query database, though: it will not look up a customer by surname or compute a balance. Usually consumers build state in their own databases, and Kafka is the source of changes and history.
Do old systems have to be rewritten for Kafka?
Not always. Changes from the databases of old systems can be published through Change Data Capture, for example Debezium with Kafka Connect, without changing application code. The limitation is that such events describe changes in tables, not business facts, so they often need to be transformed before they reach a wide group of consumers.
Who in the company should run Kafka?
Ideally a platform team that is responsible for the cluster, naming standards, permissions, the schema registry and monitoring. The teams that publish events are responsible for their content. When every team runs its own cluster, the company quickly ends up with several incompatible installations and the same integration problem it had before Kafka.