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.
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
- 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.
- 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.
- 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:
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
| Method | Use Case | Pros | Cons |
| Log Compaction | Maintaining latest key states | Reduces disk usage; Preserves ordering | Not for immediate, selective deletions |
| Retention Policy | Controlling log size and age | Simple to implement; Automates cleanup | Non-selective; May result in data loss |
| Tombstone Messages | Deleting keys in compaction-supported topics | Selective deletion of keys; Clean state transition | Requires 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
- Replacing ListenableFuture with CompletableFuture in Kafka producer/consumer
- Replenish event sourced aggregate with kafka as event store
- Replicating messages from one Kafka topic to another kafka topic
- Replication factor 3 larger than available brokers 1 when starting the kafka
- ReplicationFactor vs replicas in kafka
- Reproduce RabbitMQ network partition scenario
- Request messages between two timestamps from Kafka
- Rereading message from Kafka topic by refusing acknowledgement

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.