Delete message after consuming it in KAFKA

I am using apache kafka to produce and consume a file 5GB in size. I want to know if there is a way where the message from the topic is automatically removed after it is consumed. Do I have any way to keep track of consumed messages? I don't want to delete it manually.


Solution 1:

In Kafka, the responsibility of what has been consumed is the responsibility of the consumer and this is also one of the main reasons why Kafka has such great horizontal scalability.

Using the high level consumer API will automatically do this for you by committing consumed offsets in Zookeeper (or a more recent configuration option is using by a special Kafka topic to keep track of consumed messages).

The simple consumer API make you deal with how and where to keep track of consumed messages yourself.

Purging of messages in Kafka is done automatically by either specifying a retention time for a topic or by defining a disk quota for it so for your case of one 5GB file, this file will be deleted after the retention period you define has passed, regardless of if it has been consumed or not.

Solution 2:

You cannot delete a Kafka message on consumption

Kafka does not have a mechanism to directly delete a message when it is consumed.

The closest thing I found at an attempt to do this is this trick but it is untested and by design it will not work on the most recent messages:

A potential trick to do this is to use a combination of (a) a compacted topic and (b) a custom partitioner (c) a pair of interceptors.

The process would follow:

  1. Use a producer interceptor to add a GUID to the end of the key before it is written.
  2. Use a custom partitioner to ignore the GUID for the purposes of partitioning
  3. Use a compacted topic so you can then delete any individual message you need via producer.send(key+GUID, null)
  4. Use a consumer interceptor to remove the GUID on read.

But you should not need this capability.

Have 1 or more consumers, and want a message to be consumed only once in total by them?
Put them in the same consumer group.

Want to avoid too many messages filling up the disk?
Set up retention in terms of disk space and or time.

Solution 3:

As per my Knowledge you can Delete the consumed data form the logs by reducing the Storage time. Default time for the log is set for 168 hours and then the Data is automatically removed from the Kafka-Topic which you created. So, my suggestion is to reduce the go to the server.properties which is located in the config folder and the change the 168 to a minimum time. so their is no data after the specific amount of time which you have set for the log.retention.hours.So your issue will be solved.

log.retention.hours=168

Keep coding