Kafka
Message Removal
Data Processing
Streaming Platforms
Kafka Topics

Removing one message from a topic in Kafka

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 is a distributed streaming platform that efficiently handles real-time data feeds. One of its primary uses is to store and process streams of records, which are organised into topics. Each message in a Kafka topic has an offset, which is a unique identifier for each message within that partition. Due to Kafka's immutable log architecture, directly removing specific messages (or updating) at the individual level is not natively supported for various integrity, performance, and architectural reasons. However, there are ways to handle cases when a message needs to be "removed" or disregarded.

The Immutability of Kafka Topics

Kafka topics are designed to be append-only logs. This immutability aids in achieving high throughput and durability, which are essential for a distributed messaging system. The design ensures that once data is written to a partition, it cannot be changed. This is why you cannot simply delete a single message from a Kafka topic.

Methods for Handling Message Deletion in Kafka

  1. Log Compaction: Kafka offers a feature called log compaction, which helps in removing obsolete records. This doesn't remove messages on demand but ensures that only the latest value for each key is kept in the log. This is beneficial in scenarios where only the most current state is necessary, such as in databases or caching systems.
    Log compaction works by retaining the last known message for each key in the partition. Over time, as newer messages with the same key arrive, older messages are dropped during a compaction cycle.
  2. Time-based Retention: Another approach to managing storage and indirectly removing messages is using Kafka's time-based or size-based retention policies. Topics can be configured to retain messages only for a specified time or until the log grows to a certain size. Messages older than the retention period or from logs that exceed the size limit are automatically purged.
    However, this is a non-selective process and does not target specific messages.
  3. Using Tombstone Messages: In a scenario where message values are updated or keys are deleted, a common pattern is to use tombstone messages. A tombstone message is a record with a null value (but a normal key), which indicates that the key should be deleted when log compaction occurs.

Example: Implementing Tombstone Messages

When a key-value pair is no longer relevant, and you want to mark it for removal during the next compaction cycle, you can produce a tombstone message like this:

java
producer.send(new ProducerRecord<>("topicName", key, null));

This message will signal that the key has no more relevant values, and it will be removed in the compaction.

Challenge with Deleting Individual Messages

The main challenge in removing an individual message is that Kafka’s design prioritizes consistency and fault-tolerance over mutability of data. Editing or removing specific messages can potentially break the ordering of messages and undermine the reliability attributes of the system.

Summary Table: Methods of Managing Kafka Messages

MethodUse CaseProsCons
Log CompactionMaintaining latest key statesReduces disk usage; Preserves orderingNot for immediate, selective deletions
Retention PolicyControlling log size and ageSimple to implement; Automates cleanupNon-selective; May result in data loss
Tombstone MessagesDeleting keys in compaction-supported topicsSelective deletion of keys; Clean state transitionRequires subsequent compaction to take effect

Conclusion

While Kafka doesn’t allow the removal of an individual message directly due to its immutable design, it provides mechanisms like log compaction, retention policies, and tombstone messages to manage state effectively within these constraints. When working with Kafka, understanding and leveraging these features can help maintain the integrity and manageability of your data streams efficiently.


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.