In Kafka, how to handle deleted rows from source table that are already reflected in Kafka topic?

Viewed 1113

I am using a JDBC source connector with mode timestamp+incrementing to fetch table from Postgres, using Kafka Connect. The updates in data are reflected in Kafka topic but the deletion of records has no effect. So, my questions are:

  1. Is there some way to handle deleted records?
  2. How to handle records that are deleted but still present in kafka topic?
2 Answers

The recommendation is to either 1) adjust your source database to be append/update only, as well, either via a boolean or timestamp that is filtered out when Kafka Connect queries the table.

If your database is running out of space, then you can delete old records, which should already have been processed by Kafka

Option 2) Use CDC tools to capture delete events immediately rather than missing them in a period table scan. Debezium is a popular option for Postgres

A Kafka topic can be seen as an "append-only" log. It keeps all meesages for as long as you like but Kafka is not built to delete individual messages out of a topic.

In the scenario you are describing it is common that the downstream application (consuming the topic) handles the information on a deleted record.

As an alternative you could set the cleanup.policy of your topic to compact which means it will eventually keep only the latest value for each key. If you now define the key of a message as the primary key of the Postgres table, your topic will eventually delete the record when you produce a message with the same key and a null value into the topic. However,

  1. I am not sure if your connector is flexible to do this

  2. Depending on what you do with the data in the kafka topic, this could still not be a solution to your problem as the downstream application will still read both record, the original one and the null message as the deleted record.

Related