Get Started Free

Database Write Aside

Applications which write directly to a database may want to produce an associated Event to the Event Streaming Platform for each write operation allowing downstream Event Processing Applications to be notified and consume the Event.


How do I update a value in a database and create an associated event with the least amount of effort?



Write to a database, then write to Kafka. Perform the write to Kafka as the last step in a database transaction to ensure an atomic dual commit (aborting the transaction if the write to Kafka fails).


//Enable transactions

   //insert into the DB
   sql = db.prepareStatement("insert into values (?)");

   //insert into Kafka
   producer.send(event.key(), event.value());

   //commit to the DB
} catch (SQLException e ) {


In its default form, this pattern guarantees dual-write for most use cases. However, should the database transaction fail at commit time (say, because the database server has crashed) the write to Kafka cannot be rolled back unless transactions have been enabled. For many use cases, this eventuality will be tolerable as the dual-write can be retried once the failure is fixed, and most event consumers will implement idempotence anyway. However, application programmers need to be aware that there is no firm guarantee.

Transactional messaging systems like Kafka can be used to provide stronger guarantees so long as all event consumers have the transactions feature enabled.


Confluent Cloud is a fully managed Apache Kafka service available on all three major clouds. Try it for free today.

Try it for free