简体   繁体   English

我需要将数据保存在 mongodb 中,然后以原子方式将这些数据发送到 kafka

[英]I need to save data in mongodb and send this data to kafka all atomically

I have spring boot application which persists data to mongodb and sends this data to kafka.我有 spring 启动应用程序,它将数据保存到 mongodb 并将此数据发送到 kafka。 I want this two processes to run atomically.我希望这两个进程以原子方式运行。 That means if data is persisted to mongo then it should be sent to kafka.这意味着如果数据被持久化到 mongo,那么它应该被发送到 kafka。 How can I do it?我该怎么做?

With Kafka itself you can't.对于卡夫卡本身,你不能。

Kafka offers transactions , but they are restricted to write to multiple partitions in Kafka atomically. Kafka 提供transactions ,但它们仅限于以原子方式写入 Kafka 中的多个分区。 They are designed with stream processing in mind, so consuming from one topic and producing to another in one go - but a Kafka transaction cannot know whether a write command to mongo succeeded.它们在设计时考虑了 stream 处理,因此从一个主题消费并在一个 go 中生产另一个主题 - 但 Kafka 事务无法知道对 mongo 的写入命令是否成功。

The usecase you have is something that appears regularly though.您拥有的用例是经常出现的东西。 Usually you would use the outbox pattern .通常你会使用发件箱模式 The answer is to only modify one of the two resources (the database or Apache Kafka) and drive the update of the second one based on that in an eventually consistent manner.答案是只修改两个资源之一(数据库或Apache Kafka),并在此基础上以最终一致的方式驱动第二个资源的更新。

If you really need atomic writes, I believe it is possible to do so by relying on the ACID guarantees Mongo >= 4.2 gives you instead of Kafka's transactional guarantees.如果您确实需要原子写入,我相信可以通过依赖 Mongo >= 4.2 为您提供的 ACID 保证而不是 Kafka 的事务保证来做到这一点。 But this would mean you need to manage the Kafka offsets in Mongo.但这意味着您需要在 Mongo 中管理 Kafka 偏移量。

If you have "Kafka the definitive guide, 2nd edition", there is a small chapter with more details about what exactly Kafka transactions can and cannot do and possible workarounds.如果您有“Kafka 权威指南,第 2 版”,则有一小章详细介绍了 Kafka 事务究竟能做什么和不能做什么以及可能的解决方法。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM