Kafka
Message Production
Null Value
Tombstone
Console Commands

Produce a Kafka tombstone message (null value) from the console

Codemia Knowledge Hub ·

A tombstone in Kafka is a record with a non-null key and a null value. On a topic with log compaction, it marks that key for deletion: when the compactor runs, it removes all records with that key. Producing one from the console needs care, because the console producer reads values as text.

Log compaction and keys

Two ideas make tombstones work:

  1. Log compaction: Kafka keeps at least the last known value for each key in a compacted topic and compacts older records with the same key to save space. A tombstone tells the compactor to remove the key entirely.
  2. Keys: every Kafka message has a key and a value. The key decides which partition the message goes to, which keeps messages with the same key in order.

This is why tombstones matter when Kafka acts as a store or cache with read and delete operations, and in event sourcing, where state changes are recorded as a series of events.

Producing a tombstone from the console

You need the command-line tools that ship with a standard Kafka installation, mainly kafka-console-producer.

1. Start Kafka

Make sure Kafka and ZooKeeper are running. They are typically started with the zkServer.sh start and kafka-server-start.sh scripts.

2. Create a compacted topic

bash
   kafka-topics.sh --create --bootstrap-server localhost:9092 --partitions 1 --replication-factor 1 --topic your_topic_name --config cleanup.policy=compact

3. Start the producer with key parsing

Tell the console producer to split each line into a key and a value:

bash
   kafka-console-producer.sh --broker-list localhost:9092 --topic your_topic_name --property "parse.key=true" --property "key.separator=:"

Each line you type is now sent as key:value. Be aware that typing someKey:null sends the four-character string null as the value, which is not a tombstone. A real tombstone needs a producer that can send an actual null value. Newer console producers (KIP-810) have a null.marker property: a value that exactly matches the marker is sent as null.

bash
   kafka-console-producer.sh --bootstrap-server localhost:9092 --topic your_topic_name --property "parse.key=true" --property "key.separator=:" --property "null.marker=NULL"

Typing someKey:NULL now produces a tombstone for someKey. On older versions without null.marker, use a client written against the producer API.

4. Check the result

Read the topic back with keys printed:

bash
   kafka-console-consumer.sh --bootstrap-server localhost:9092 --topic your_topic_name --from-beginning --property print.key=true --property key.separator="-"

A record for someKey shows up as someKey-null. Since the console consumer prints a null value as null, this output looks the same for a real tombstone and for the string null, so it does not by itself prove you produced a tombstone. Add --property null.literal=TOMBSTONE to the consumer and real null values print as TOMBSTONE instead.

Quick reference

FeatureDescription
Log CompactionCompacts old records in a topic based on keys.
Tombstone MessageA special Kafka message where the key has a non-null value and the value is null.
Use CaseSuitable for deleting entries in stateful applications or caches.
Kafka Tools for Tombstoneskafka-console-producer.sh and kafka-console-consumer.sh
Production of Tombstone MessageDone via console producers with key parsing and separators.
Verification of TombstoneConsumable with key printing and custom separators.

Course
Beginner
27 lessons
10 hours
System Design Fundamentals

Build a strong foundation in designing scalable, reliable distributed systems.

View the course
System Design practice on Codemia

Work through 120+ system design problems with detailed solutions, from rate limiters to multi-region storage.

Practice system design