apache-kafkamicroservicesevent-driven

Kafka in microservices for providing transactions


Can Kafka be used to provide transactions between microservices?

Let's say we build a simple online store with 2 microservices: order and storage.

While placing an order, you want to also update the quantity of a product in the storage. But when this action fails, you do not want to place an order. Also when the placing of the order fails, you want to rollback update of the storage.

Can this be easily achieved by using kafka? Would it be the best possible solution to provide consistency across microservices? And if so, should all communication between microservices be done using Kafka?


Solution

  • Yes, Kafka has transactions.

    The producer can begin and end that transaction, but the consumer also needs to set its read isolation level to detect transaction markers.

    https://developer.confluent.io/learn/kafka-transactions-and-guarantees/

    There are other patterns to solve the same problem like Distributed Sagas and Outbox Pattern

    https://developers.redhat.com/blog/2018/10/01/patterns-for-distributed-transactions-within-a-microservices-architecture

    https://www.infoq.com/articles/saga-orchestration-outbox/