Producing a Kafka message with a Null Value (Tombstone) from the Console
System Design practice on Codemia
Work through 120+ system design problems with detailed solutions, from rate limiters to multi-region storage.
Apache Kafka supports a concept known as tombstone messages, which are used to represent the deletion of a key from the log in Kafka topics which support log compaction. A tombstone message is a message with a key that is not null, but has a null value. This can be useful in a setup where Kafka acts as a storage or caching mechanism that supports read and delete operations.
Basic Concepts
Before diving into how to produce a tombstone message from the Kafka console, it's essential to understand two key concepts: log compaction and key-value pairs in Kafka messages.
- Log Compaction: Log compaction ensures that Kafka retains at least the last known value for each key within a compacted topic. Old records with the same key are compacted to save space. A tombstone (a record with a key and a null value) marks a key for deletion, and when the compactor runs, it will remove all records with that key.
- Key-Value Pairs in Messages: Kafka messages consist of a key and a value. The key is used to decide the partition within a topic where the message will be placed. This is crucial for maintaining the order of messages with the same key.
Producing a Tombstone Message from the Console
Producing messages, including tombstones, from the Kafka console requires the Kafka command-line tools that come with a standard Kafka installation. The primary tool for this task is the kafka-console-producer.
Here’s how you can send a tombstone message:
- Start the Kafka Environment: Ensure that your Kafka and Zookeeper instances are running. These are typically started using the
zkServer.sh startandkafka-server-start.shscripts. - Create a Kafka Topic with Log Compaction: You need a topic configured with log compaction to demonstrate the tombstone. Create a topic with the following command:
- Produce a Tombstone Message: To produce a tombstone, you need to provide a key and a null value. Use the
kafka-console-producerwith theparse.keyandkey.separatoroptions:
You can then enter a message like someKey:null. The key here is someKey and the value is null, represented as a tombstone.
- Verify the Tombstone: To ensure that the tombstone has been added, you can consume messages from the topic using
kafka-console-consumer:
This should show someKey-null among the output, indicating a message with someKey and a null value.
Why Use Tombstone Messages?
Tombstone messages are useful for scenarios involving deleting entries in a stateful application or cache that uses Kafka as a backend store. This is critical in event sourcing techniques where state changes are captured as a series of events.
Summary Table
| Feature | Description |
| Log Compaction | Compacts old records in a topic based on keys. |
| Tombstone Message | A special Kafka message where the key has a non-null value and the value is null. |
| Use Case | Suitable for deleting entries in stateful applications or caches. |
| Kafka Tools for Tombstones | kafka-console-producer.sh and kafka-console-consumer.sh |
| Production of Tombstone Message | Done via console producers with key parsing and separators. |
| Verification of Tombstone | Consumable with key printing and custom separators. |
Conclusion
Using tombstone messages in Kafka is an efficient way to manage deletions in topics configured for log compaction. They are particularly useful in applications that require a consistent and compact view of the data, by ensuring that deletes are properly propagated and stored states are correctly maintained. Producing these messages from the Kafka console requires a good understanding of Kafka command-line tools and topic configuration parameters.
Related reading
- Producing and Consuming Avro messages from Kafka without Confluent components
- Program Running Pika Throwing AMQPConnectionError
- Proper way to programmatically stop an Alpakka Kafka stream
- Properly Configuring Kafka Connect S3 Sink TimeBasedPartitioner
- Providing rabbitmq.conf in a docker-compose file gives sed cannot rename /etc/rabbitmq/sedMaHqMa Device or resource busy
- Publish multiple messages to RabbitMQ from a file
- Publish to RabbitMQ queue with HTTP API
- Publishing to the default rabbitmq exchange using the http api

System Design Fundamentals
Build a strong foundation in designing scalable, reliable distributed systems.
View the courseTrack what you have practised
A free account saves your progress, solutions and study plan across every problem on Codemia.
System Design practice on Codemia
Work through 120+ system design problems with detailed solutions, from rate limiters to multi-region storage.