简体   繁体   English

Node.js中数据同步的最佳实践

[英]best practice for data synchronization in nodejs

I'm trying to do some integration between two systems, and I've got some problems when it comes to synchronizing data. 我正在尝试在两个系统之间进行一些集成,但是在同步数据时遇到了一些问题。

I am using nodejs and the database is mongodb or firebase . 我正在使用nodejs ,数据库是mongodbfirebase

The scenario is described as below: 场景描述如下:

  • systemA with dbA 带有dbA的systemA
  • systemB with dbB 带有dbB的systemB

Do the following: 请执行下列操作:

  1. systemA sends a request (POST or PUT whatever) to systemB, the body is like this: systemA向systemB发送一个请求(POST或PUT等等), body如下:

    \n{ {\n  ..... .....\n  fieldA1: valueA1, fieldA1:valueA1,\n  fieldA2: valueA2 fieldA2:valueA2\n  ..... .....\n} }\n
  2. then systemB needs to update several fields(fieldB1, fieldB2) in dbB according to the systemA data, like this: 然后,systemB需要根据systemA数据更新dbB中的几个字段(fieldB1,fieldB2),如下所示:

    \nfieldB1: valueA1 栏位B1:值A1\n
    \nfieldB2: valueA2 fieldB2:valueA2\n
  3. after BOTH fieldB1 and fieldB2 are updated successfully, then execute more logic BOTHfieldB1fieldB2被成功更新,然后执行多个逻辑

I'm using async to control asynchronous process. 我正在使用async来控制异步过程。 My code to implement these 3 steps: 我的代码实现了这3个步骤:

async.waterfall([
  function (callback) {
    //get valueA1 of fieldA1 and valueA2 of fieldA2
  },

  async.parallel([
    function (callback) {
      //set valueA1 to fieldB1
    },

    function (callback) {
      //set valueA2 to fieldB2
    }
  ], function (err, result) {
      //more logic here
  })
], function (err, result) {
  //more logic here
})

Since fieldB1 and fieldB2 should be updated at the same time , either situation when failing to update fieldB1 or fieldB2 will lead to data inconsistency, which is not the correct result. 由于fieldB1fieldB2失败应在同一时间进行更新,这两种情况下,当更新fieldB1fieldB2会导致数据不一致,这是不正确的结果。

However, async.parallel cannot guarantee that any update failure will rollback or prevent the others' update right ? 但是, async.parallel无法保证任何更新失败都会回滚或阻止其他人的更新吗? Is there any way to idealy keep the data consistency of BOTH when updating fieldB1 and fieldB2 ? 有没有什么办法idealy保留两个更新时的数据一致性fieldB1fieldB2

I find it difficult to map your code to the Firebase API. 我发现很难将您的代码映射到Firebase API。 But what you're describing sounds like its achievable by either using transactions or multi-location updates . 但是,您所描述的内容听起来似乎可以通过使用事务 或多位置更新来实现

I covered these type of updates in-depth in the past in: How to write denormalized data in Firebase 过去, 深入介绍了这些类型的更新: 如何在Firebase中写入非规范化数据

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

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