Kafka
Key Deletion
Tombstone
Data Management
Software Bugs

Kafka not deleting key with tombstone

System Design practice on Codemia

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

Practice system design

Apache Kafka, a distributed event streaming platform, enables processing, storing, and integrating large volumes of data in real-time. One of its core mechanisms is handling data deletion and compacting logs to maintain efficiency and performance. Kafka utilizes the concept of a tombstone message, which is commonly used to signal the deletion of a key in a compacted topic. However, under certain circumstances, the tombstone does not work as expected, leading to peculiarities in how data is managed.

Understanding Tombstones in Kafka

In Kafka, when keys are deleted or updated, a special kind of message called a tombstone is used. A tombstone is a message with a key that carries a null value. The presence of this message tells Kafka to delete the key from the store in subsequent compactions of the log. This is essential in scenarios where the state of a key-value pair changes over time, or entries need to be removed entirely to reclaim storage space or maintain data accuracy.

Common Reasons Kafka Might Not Delete a Key with a Tombstone

There are a few scenarios where Kafka might not successfully delete a key despite receiving a tombstone message:

  1. Log Compaction Lag:
    Kafka runs log compaction as a background process. If this process lags or is configured to run infrequently, a tombstone might not immediately result in the deletion of the associated key.
  2. Minimum Compaction Lag Time:
    Configuration parameters such as min.compaction.lag.ms define the minimum amount of time a message must remain uncompacted in the log. If a tombstone is created, but the log has not been compacted past this minimum time, the key will not be deleted.
  3. Producer Misconfiguration:
    If the producer sending the tombstone does not correctly specify the key, or if there is a mismatch in the key format (e.g., encoding issues), the tombstone will not match and delete the intended key.
  4. Tombstone Retention Policy:
    The retention time for tombstones (delete.retention.ms) might be misconfigured. If tombstones are cleaned up or expire before compaction happens, the keys they were meant to delete will remain.

Technical Example

Consider a compacted topic in Kafka where each message key should reflect a user's last login time. If a user is deleted, a producer sends a tombstone (a message with the user's key and a null value) to record the deletion:

kafka
ProducerRecord<String, String> tombstone = new ProducerRecord<>("user-logins", "user123", null);
producer.send(tombstone);

If min.compaction.lag.ms is set very high, this tombstone might remain in the log without causing the deletion of user123, keeping outdated information accessible for longer than necessary.

Key Summary Points

IssueDescriptionImpactSolution
Log Compaction LagCompaction process is slow or infrequentDelayed deletion of keysAdjust log.cleaner.frequency and similar settings
Minimum Compaction Lag Timemin.compaction.lag.ms is set too highPrevents immediate deletion of keysLower min.compaction.lag.ms settings
Producer MisconfigurationIncorrect key format or mismatchTombstone fails to delete the intended keyEnsure correct key formatting and consistency
Tombstone Retention Policydelete.retention.ms is too shortTombstones are deleted before affecting compactionIncrease delete.retention.ms setting

Best Practices for Managing Tombstones

  • Monitor and tune compaction settings: Regular monitoring of Kafka's performance and tuning the compaction settings according to the traffic and size of the data can help in managing tombstones efficiently.
  • Consistency in key formats: Maintaining consistency in how keys are formatted and used across producers ensures that tombstones are correctly aligned with the intended keys.
  • Proper consumer handling: Consumers should be designed to handle null values as signals for key deletions to appropriately manage their local state.

Conclusion

Tombstones play a crucial role in managing state in Kafka's compacted topics but require careful configuration and handling to function as intended. Understanding and correctly implementing their functionality helps optimize Kafka deployments for reliability and efficiency in data management. Proper setup, continuous monitoring, and adjustments in configurations can mitigate issues related to tombstones not deleting keys as expected, ensuring the system remains robust and performant.


Related reading
Course
Beginner
27 lessons
10 hours
System Design Fundamentals

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

View the course
Track 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.

Practice system design

All Rights Reserved.