简体   繁体   中英

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 .

The scenario is described as below:

  • systemA with dbA
  • systemB with dbB

Do the following:

  1. systemA sends a request (POST or PUT whatever) to systemB, the body is like this:

    \n{ \n  ..... \n  fieldA1: valueA1, \n  fieldA2: valueA2 \n  ..... \n} \n
  2. then systemB needs to update several fields(fieldB1, fieldB2) in dbB according to the systemA data, like this:

    \nfieldB1: valueA1 \n
    \nfieldB2: valueA2 \n
  3. after BOTH fieldB1 and fieldB2 are updated successfully, then execute more logic

I'm using async to control asynchronous process. My code to implement these 3 steps:

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.

However, async.parallel cannot guarantee that any update failure will rollback or prevent the others' update right ? Is there any way to idealy keep the data consistency of BOTH when updating fieldB1 and fieldB2 ?

I find it difficult to map your code to the 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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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