Skip to content

Apache Kafka

Scope: designing topics, producers and consumers for Apache Kafka 4.x, which runs only in KRaft mode (ZooKeeper mode was removed in Kafka 4.0). Shared messaging rules aren't repeated here: publish events together with database changes through a transactional outbox (DATA-4, general/data.md), make operations safe to retry and evolve contracts compatibly (API-1, API-2, general/api-design.md), retry with backoff (OPS-3, general/operations-reliability.md), and say which event-driven pattern you mean (STYLE-8 to STYLE-10, general/architecture-styles.md). For a queue-based broker, see RabbitMQ. Features that only Confluent's products provide are marked as such.

Entries are sorted by score from 1 to 10 (criteria): 9โ€“10 start here or strongly recommended, 6โ€“8 recommended, 1โ€“5 further reading or conditional.

Adopted references

  • 9 Apache Kafka documentation (Apache Kafka 4.4): the baseline. The Design chapter, the producer, consumer and topic configuration references, Operations and Security matter most for application work.
  • 8 Design (Apache Kafka docs): how producers, consumers, delivery semantics, transactions, share groups, replication and log compaction work, and the guarantees each gives.
  • 7 Security (Apache Kafka docs): encryption and authentication with TLS, SASL mechanisms, and authorization with ACLs.
  • 7 Basic Kafka Operations (Apache Kafka docs): creating and changing topics, consumer groups, expanding clusters and increasing the replication factor.
  • 7 Schema Evolution and Compatibility (Confluent docs): the compatibility types of Confluent Schema Registry, the changes each allows for Avro, Protobuf and JSON Schema, and the order in which to upgrade clients. Schema Registry is a Confluent product under the Confluent Community License.

Key rules

Topics and partitions

Producers and durability

  • 8 KAFKA-5 For data you can't afford to lose, use a replication factor of 3, min.insync.replicas=2 and producer acks=all, and leave unclean leader election off. min.insync.replicas defaults to 1, so an acks=all write can succeed when only one replica is in sync, and is lost if that replica then fails. (Topic configs: min.insync.replicas, Design: availability and durability guarantees)
  • 7 KAFKA-6 Keep producer idempotence on, and set enable.idempotence=true explicitly, so that conflicting settings fail when the producer is created instead of silently turning it off. Idempotence, on by default, stops retries from writing duplicates; it needs acks=all, retries above 0 and at most 5 in-flight requests per connection. (Producer configs: enable.idempotence, acks)

Consumers

Schemas

Security

  • 8 KAFKA-14 Authenticate every connection to the brokers with TLS client certificates or SASL over TLS, turn on the StandardAuthorizer, and give each application its own principal with only the ACLs it needs. Kafka accepts unauthenticated, unencrypted clients unless you configure security, and SASL/PLAIN and SCRAM should only be used over TLS. Don't set allow.everyone.if.no.acl.found=true. (Apache Kafka, Security overview, Authentication using SASL, Authorization and ACLs)

Operations

  • 7 KAFKA-15 Run production clusters with dedicated KRaft controller nodes, usually three or five, rather than nodes that are both broker and controller. Combined mode should be avoided in critical environments. Clusters still on ZooKeeper must migrate to KRaft, using Kafka 3.9 as the bridge release, before they can upgrade to 4.x. (Apache Kafka, KRaft: deploying considerations, KRaft: controllers, Upgrading to 4.0)
  • 7 KAFKA-16 Monitor consumer lag for every consumer group, and the brokers' under-replicated and under-min-ISR partition counts, and alert when lag keeps growing or those counts aren't 0. (Apache Kafka, Monitoring, Checking consumer position)

How to enforce

Tools that check the key rules above automatically, in CI, the IDE or a commit hook. "Partly" means the tool covers only some of what the rule asks.

  • 7 Schema Registry Maven plugin (Confluent): in CI, test-compatibility checks the project's schemas against a Schema Registry, test-local-compatibility checks them against earlier local versions without a registry, and register registers them. Checks KAFKA-12, and partly KAFKA-13.
  • 7 buf breaking: for Protobuf schemas kept in the repository, compares them in CI with a previous version, such as the main branch, and reports changes that break the wire format or generated code. Partly checks KAFKA-12.
  • 5 CreateTopicPolicy (Apache Kafka): a policy class you write and set in create.topic.policy.class.name; the controller runs it for every topic creation and rejects topics whose replication factor, min.insync.replicas or retention is outside the range you allow. Partly checks KAFKA-3, KAFKA-4 and KAFKA-5.

Tutorials and further reading

  • 6 Apache Kafka 101 (Confluent Developer): a free video course on events, topics, partitions, brokers, replication, producers and consumers.
  • 6 Event Streaming Patterns (Confluent Developer): a catalogue of patterns such as Dead Letter Stream, Idempotent Reader and Schema Compatibility, with Kafka examples.
  • 6 Quick Start (Apache Kafka docs): start a local Kafka server from the download or the Docker image, create a topic, write and read events, and process them with Kafka Connect and Kafka Streams.