$ run as a normal user# run as root (or with sudo)
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 1Step by step
- 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.
- Choose copies —
--replication-factor 3andmin.insync.replicas=2: a write withacks=allsucceeds while one broker is down, and no data is lost. On a one-broker test system use 1 and 1. - Choose retention —
retention.ms(7 days = 604800000) orretention.bytesper partition. For "latest value per key" topics usecleanup.policy=compact. - Describe the topic (quick answer, line 3) — each partition shows its
Leader,ReplicasandIsr(in-sync replicas). All replicas should be in the ISR. - Send test messages with a key (line 4) — messages with the same key go to the same partition, so their order is kept.
- Read them (line 5) —
--from-beginningreads old messages; with--group test-clithe consumer joins a group and commits its position. - Check consumer lag — how far each group is behind:
/opt/kafka/bin/kafka-consumer-groups.sh --bootstrap-server $BS --describe --group billingLAG = messages not yet processed. Growing lag means the consumers are too slow or stopped.
- 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- In production — create topics as code (Terraform provider, Strimzi
KafkaTopicin Kubernetes) and turn offauto.create.topics.enable, so a typo does not create a new topic.
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.
Common problems
| Problem | Cause | Fix |
|---|---|---|
InvalidReplicationFactorException | Fewer brokers than the replication factor. | Use a factor ≤ the number of brokers. |
NOT_ENOUGH_REPLICAS on produce | Fewer in-sync replicas than min.insync.replicas. | Bring the broker back; check disk and network. |
| Consumer gets nothing | No --from-beginning, or the group already read everything. | Add it, or use a new group name. |
| Connection errors to an address you did not use | advertised.listeners points to an internal name. | Fix the broker's advertised listener for clients. |
| Order of messages mixed up | Messages without a key spread over partitions. | Send a key for related messages (for example the order ID). |
| Lag grows all the time | Consumers too slow or fewer than needed. | More consumer instances (up to the number of partitions); faster processing. |
Go further
- Command pages: kafka-topics.
- Next how-tos: Check a RabbitMQ queue, Monitor a Linux server with Prometheus, Run an app with Docker Compose.
- Learn it: Data services at work — Kafka. Full guide: Kafka.
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.