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:
systemA sends a request (POST or PUT whatever) to systemB, the
body
is like this:{ ..... fieldA1: valueA1, fieldA2: valueA2 ..... }
then systemB needs to update several fields(fieldB1, fieldB2) in dbB according to the systemA data, like this:
fieldB1: valueA1
fieldB2: valueA2
after BOTH
fieldB1
andfieldB2
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
?