Contents

A topic is a named, append-only log. Producers write to it; consumers read from it. Topics are identified by name and can have multiple partitions.

A partition is an ordered, immutable sequence of records, each identified by a monotonically increasing offset. Each partition lives on a single broker at any time.

A good starting point: 1–3 partitions per broker per topic. For high-throughput topics, multiply by the expected max consumers in the group.

Each partition has one leader and zero or more followers (determined by the replication factor).

Replication FactorFault ToleranceRecommended for
1None (data lost if broker dies)Dev/test only
2Tolerates 1 broker failureNon-critical data
3Tolerates 2 broker failuresProduction (standard)

Kafka retains messages for a configurable period regardless of whether they were consumed. Two retention modes:

# Create topic with 1-day retention kafka-topics.sh --create \ --topic user-events \ --partitions 6 \ --replication-factor 3 \ --config retention.ms=86400000 \ --bootstrap-server localhost:9092

Log compaction is an alternative to time-based deletion. Instead of deleting old data by time, Kafka keeps only the latest record for each key. Older records with the same key are removed. This is ideal for maintaining a changelog or snapshot of the latest state per entity.

# Enable log compaction on a topic kafka-topics.sh --create \ --topic user-profiles \ --partitions 3 \ --replication-factor 3 \ --config cleanup.policy=compact \ --bootstrap-server localhost:9092 Compaction is asynchronous and does not guarantee immediate removal. Records with null values (tombstones) signal deletion of a key.
import org.apache.kafka.clients.admin.*; import java.util.*; Properties props = new Properties(); props.put(AdminClientConfig.BOOTSTRAP_SERVERS_CONFIG, "localhost:9092"); try (AdminClient admin = AdminClient.create(props)) { // Create a topic NewTopic newTopic = new NewTopic("orders", 6, (short) 3); newTopic.configs(Map.of("retention.ms", "604800000")); // 7 days admin.createTopics(List.of(newTopic)).all().get(); // List topics Set<String> topics = admin.listTopics().names().get(); System.out.println("Topics: " + topics); // Describe a topic Map<String, TopicDescription> desc = admin.describeTopics(List.of("orders")).all().get(); desc.forEach((name, td) -> System.out.println(name + ": " + td)); // Delete a topic admin.deleteTopics(List.of("old-topic")).all().get(); }