Consuming and Producing Messages with Keys

Learn what a Kafka message key is and how to produce and consume messages with keys using the CLI.

We have produced and then consumed messages which only contained values. However, a message can also contain a key. A Kafka message key is usually short compared to the message value and can be of any type we would like, such as a number, a string, etc. We can use message keys if we need related messages to be processed in the same order in which they were received.

Before we begin, let’s create the topic we will work with. Connect to the terminal, and the start-kafka.sh and topic creation commands will run automatically. Note that we may see a few WARN messages about the broker not being available before the topic is created—that is normal and it happens because the topic creation command is launched before the Kafka server is up and running.

Get hands-on with 1200+ tech skills courses.