事务发件箱模式与微服务中的 ChainedKafkaTransactionManager

Transactional outbox pattern vs ChainedKafkaTransactionManager in Microservices

使用 Spring-Kafkas ChainedKafkaTransactionManager 我看不出在 Spring 引导微服务上下文中实现事务发件箱模式有任何意义。

将消息生产者(即KafkaTemplate的send方法)和DB操作放在同一个事务块中,正好解决了发件箱模式应该解决的问题: 如果在事务代码中引发任何异常,则既不会提交数据库操作,也不会在消费者端读取消息(配置 read_committed)

这样我就不需要额外的 table 也不需要任何类型的 CDC 代码。总之,Spring Kafka 事务同步方式对我来说似乎比任何事务发件箱模式的实现都更容易使用和实现。

我错过了什么吗?

public ChainedKafkaTransactionManager chainedTransactionManager(
                        JpaTransactionManager transactionManager,
                        KafkaTransactionManager kafkaTransactionManager) {
        ChainedKafkaTransactionManager chainedKafkaTransactionManager = 
            new ChainedKafkaTransactionManager<>(transactionManager, 
                                                 kafkaTransactionManager);
        
        return chainedKafkaTransactionManager;
    }

    @Bean
    @Primary
    public JpaTransactionManager transactionManager(EntityManagerFactory 
        entityManagerFactory) {
        JpaTransactionManager jpaTransactionManager = 
                    new JpaTransactionManager(entityManagerFactory);
    
        return jpaTransactionManager;
    }

    @Bean
    public KafkaTransactionManager<Object, Object> 
             kafkaTransactionManager(ProducerFactory producerFactory) {
        KafkaTransactionManager kafkaTransactionManager = 
           new KafkaTransactionManager<>(producerFactory);
        
        return kafkaTransactionManager;
    }


    @Transactional(value = "chainedTransactionManager")
    public Customer createCustomer(Customer customer) {
        customer = customerRepository.save(customer);
        kafkaTemplate.send("customer-created-topic","Customer created");
          
        return customer;
    }

我认为它不会为您提供相同级别的安全性。如果 Kafka 提交和 DB 提交之间出现问题怎么办。

https://medium.com/dev-genius/transactional-integration-kafka-with-database-7eb5fc270bdc

如果您尝试更新的数据在 kafka 外部,则您得到的保证较弱。

Note that exactly-once semantics is guaranteed within the scope of Kafka Streams’ internal processing only; for example, if the event streaming app written in Streams makes an RPC call to update some remote stores, or if it uses a customized client to directly read or write to a Kafka topic, the resulting side effects would not be guaranteed exactly once.

https://www.confluent.fr/blog/exactly-once-semantics-are-possible-heres-how-apache-kafka-does-it/