Send your first message
Messaging basics · Article 1 of 12 · 4 min read
Every Kafka skill starts here: getting one message in and reading it back out.
The short version
Kafka stores messages in named lists called topics. A producer writes messages to a topic and a consumer reads them. Kafka ships with a console producer and a console consumer, so you need no code yet.
Details
A broker is a running Kafka server. Start one in Docker:
docker run -d --name kafka -p 9092:9092 apache/kafka:4.1.0
Kafka keeps every message after it is read. Reading does not remove anything, so many consumers can read the same topic.
Try it yourself
Create a topic called greetings:
docker exec kafka /opt/kafka/bin/kafka-topics.sh \
--bootstrap-server localhost:9092 --create --topic greetings
Open a producer, type two lines and press Enter after each. Press Ctrl+C when you are done.
docker exec -it kafka /opt/kafka/bin/kafka-console-producer.sh \
--bootstrap-server localhost:9092 --topic greetings
>hello
>hello again
Read the topic from the start:
docker exec -it kafka /opt/kafka/bin/kafka-console-consumer.sh \
--bootstrap-server localhost:9092 --topic greetings --from-beginning
You should see:
hello
hello again
Explain it to the duck
Why does reading a message from Kafka not delete it?
Tested with: Kafka 4.1.0 in Docker.