How do you create a Kafka topic, and what do the partitions and replication.factor settings mean?
answer
- partitions = parallelism + ordering unit
- RF = copies across brokers
- RF <= live broker count
- kafka-topics.sh --create
- NewTopic(name, parts, rf)
basics
~10 sUse the kafka-topics.sh CLI or AdminClient. partitions sets how many parallel log slices the topic has; replication.factor sets how many brokers keep a copy of each partition for fault tolerance.
solid answer
~30 sA topic is created with the kafka-topics.sh --create command or programmatically via AdminClient.createTopics() passing a NewTopic. Two core settings: partitions controls the unit of parallelism and ordering — more partitions means more consumers can read concurrently, but ordering is only guaranteed within a partition. replication.factor controls how many broker copies exist of each partition (1 leader + N-1 followers); an RF of 3 tolerates 2 broker failures. RF cannot exceed the number of live brokers. Example CLI: kafka-topics.sh --bootstrap-server localhost:9092 --create --topic orders --partitions 6 --replication-factor 3. You can also pass per-topic config overrides at creation with --config retention.ms=604800000.
go deeper
Know the CLI create command and that partitions = parallelism, RF = copies.
Explain ISR, leader/follower, RF <= broker count, and per-key ordering tied to partition.
Discuss trade-offs of partition count (rebalance cost, file handles) and RF vs min.insync.replicas durability interplay.
Reason about capacity planning: partition counts per broker, replication overhead, and topic sprawl governance across a cluster.
## What a topic is A Kafka **topic** is a named, append-only log of records. Each topic is physically split into one or more **partitions** — independent ordered sequences of records, each stored as a set of segment files on disk. Records within a partition have a monotonically increasing **offset**; ordering is guaranteed only *within* a partition, never across partitions. ## partitions The `partitions` count is the topic's unit of parallelism. A consumer group can have at most one active consumer per partition, so 6 partitions means up to 6 consumers reading in parallel. The record key's hash (default partitioner) decides which partition a record lands in, so all records with the same key stay in one partition and keep their relative order. You can *increase* partitions later (`kafka-topics.sh --alter --partitions N`) but never decrease them, and increasing them re-shuffles which partition future keys hash to (breaking per-key ordering across the boundary). ## replication.factor Each partition has one **leader** replica and zero or more **follower** replicas, spread across different brokers. `replication.factor=3` means 3 copies total. Producers and consumers talk only to the leader; followers fetch from the leader to stay in sync (the **ISR**, in-sync replica set). If the leader broker dies, a follower in the ISR is promoted. RF=3 is the common production default — it survives losing 2 brokers. RF must be <= number of live brokers at creation time, or creation fails with InvalidReplicationFactorException. ## Creating a topic CLI: ``` kafka-topics.sh --bootstrap-server localhost:9092 \ --create --topic orders --partitions 6 --replication-factor 3 ``` Programmatically: ```java NewTopic t = new NewTopic("orders", 6, (short) 3); admin.createTopics(List.of(t)).all().get(); ``` ## Edge cases - If you omit `--partitions`/`--replication-factor`, the broker defaults `num.partitions` and `default.replication.factor` apply. - Creating a topic that already exists throws TopicExistsException. - You can pass per-topic overrides at creation: `--config min.insync.replicas=2 --config cleanup.policy=compact`.
- Can you decrease the partition count of a topic?No. Partitions can only be increased. Decreasing would require deleting records and would break key-to-partition mapping, so Kafka disallows it.
- What happens if you set replication.factor higher than the number of brokers?Topic creation fails with InvalidReplicationFactorException — RF cannot exceed the number of live brokers.
saying these in an interview costs you the question
- Saying ordering is guaranteed across the whole topic (it's only per-partition)
- Claiming partitions can be decreased
- Confusing replication.factor with min.insync.replicas
- Thinking consumers read from followers by default (they read from the leader)