Advanced
Searching inHow-to guides
How to search
  • Type the task in your own words (find port, open firewall, disk full) or paste an error message in quotes.
  • Choose "Quick answer" to see only the commands, or an area to stay in one field.
  • "two words" finds the exact phrase.
  • Plain words find names, texts and examples.
  • ↑ ↓ to move · Enter to open · Esc to close · / to search
Try:

How-to 191Data · Kafka

How to create and test a Kafka topic

A new service needs a Kafka topic: how many partitions, how many copies, how long to keep the data? And after creating it, does a message really go in and come out? This how-to creates a topic with safe settings, sends and reads test messages with the console tools, checks consumer lag, and changes settings later — all with the scripts that ship with Kafka.

  • 5 Lessons
  • 3 Commands

$ run as a normal user# run as root (or with sudo)

191.1

The quick answer

export BS=kafka1.example.com:9092
/opt/kafka/bin/kafka-topics.sh --bootstrap-server $BS --create --topic orders --partitions 6 --replication-factor 3 --config min.insync.replicas=2 --config retention.ms=604800000
/opt/kafka/bin/kafka-topics.sh --bootstrap-server $BS --describe --topic orders
echo 'o-1001:{"sku":"A-100","qty":1}' | /opt/kafka/bin/kafka-console-producer.sh --bootstrap-server $BS --topic orders --property parse.key=true --property key.separator=:
/opt/kafka/bin/kafka-console-consumer.sh --bootstrap-server $BS --topic orders --from-beginning --property print.key=true --max-messages 1

191.2

Step by step

  1. Choose partitions — the maximum number of consumers in one group that work in parallel. 6–12 is a good start; you can add partitions later but never remove them, and adding them changes which partition a key goes to.
  2. Choose copies — --replication-factor 3 and min.insync.replicas=2: a write with acks=all succeeds while one broker is down, and no data is lost. On a one-broker test system use 1 and 1.
  3. Choose retention — retention.ms (7 days = 604800000) or retention.bytes per partition. For "latest value per key" topics use cleanup.policy=compact.
  4. Describe the topic (quick answer, line 3) — each partition shows its Leader, Replicas and Isr (in-sync replicas). All replicas should be in the ISR.
  5. Send test messages with a key (line 4) — messages with the same key go to the same partition, so their order is kept.
  6. Read them (line 5) — --from-beginning reads old messages; with --group test-cli the consumer joins a group and commits its position.
  7. Check consumer lag — how far each group is behind:
/opt/kafka/bin/kafka-consumer-groups.sh --bootstrap-server $BS --describe --group billing

LAG = messages not yet processed. Growing lag means the consumers are too slow or stopped.

  1. Change settings later:
/opt/kafka/bin/kafka-configs.sh --bootstrap-server $BS --alter --entity-type topics --entity-name orders --add-config retention.ms=1209600000
/opt/kafka/bin/kafka-topics.sh --bootstrap-server $BS --alter --topic orders --partitions 12
  1. In production — create topics as code (Terraform provider, Strimzi KafkaTopic in Kubernetes) and turn off auto.create.topics.enable, so a typo does not create a new topic.

191.3

Check that it worked

--describe shows 6 partitions, each with 3 replicas all in the ISR; the consumer prints o-1001 {"sku":"A-100","qty":1}; kafka-consumer-groups.sh --describe shows the group with LAG 0 after it has read everything.

191.4

Common problems

ProblemCauseFix
InvalidReplicationFactorExceptionFewer brokers than the replication factor.Use a factor ≤ the number of brokers.
NOT_ENOUGH_REPLICAS on produceFewer in-sync replicas than min.insync.replicas.Bring the broker back; check disk and network.
Consumer gets nothingNo --from-beginning, or the group already read everything.Add it, or use a new group name.
Connection errors to an address you did not useadvertised.listeners points to an internal name.Fix the broker's advertised listener for clients.
Order of messages mixed upMessages without a key spread over partitions.Send a key for related messages (for example the order ID).
Lag grows all the timeConsumers too slow or fewer than needed.More consumer instances (up to the number of partitions); faster processing.

191.5

Go further

1Why choose replication factor 3 and min.insync.replicas 2?

Writes still succeed with one broker down, and every acknowledged message is stored on at least two brokers.

2What does consumer lag tell you?

How many messages a consumer group has not processed yet; growing lag means the consumers cannot keep up.

All commands in this chapter