DEV Community

Discussion on: Quasar Framework - a SSR+PWA app with dynamic data.

Collapse
 
yul profile image
Yuri Lopukhov

Thank you for this article!
Is there a need for extra replication lines in Database.configure?
Documentation for pouchdb claims that

localDB.sync(remoteDB);

is equivalent to

localDB.replicate.to(remoteDB);
localDB.replicate.from(remoteDB);

Or it doesn't work correct in some cases?

Collapse
 
tobymosque profile image
Tobias Mesquita

you're right, but that isn't what we're doing here.:

try {
  await this.replicate({ source: this.remote, target: this.local })
  await this.replicate({ source: this.local, target: this.remote })
} catch (err) {

}
this.local.sync(this.remote, {
  live: true,
  retry: true
})

is equivalent to:

let sync = function () {
  this.local.sync(this.remote, {
    live: true,
    retry: true
  })
}
this.remote.replicate.to(this.local).on('complete', function () {
  this.local.replicate.to(this.remote).on('complete', function () {
    sync()
  }).on('error', function (err) {
    sync()  
  })
}).on('error', function (err) {
  sync()
})

In that case, both initial/start replications aren't running in parralel, instead of that, we're running the replication from server to local firstly. The reason behide that is, we're assuming, in the case of conflict, the server would win. So when we run the replication from local to server, the chance to appear conflicts will be very small.

So, if everything goes well, every conflict will be resolved and everything will be in sync before we start the 2-way live replication, where the conflicts probably will be resolved as soon than appear.

If you think I'm being too cautious, I'm really open to suggestions.

Collapse
 
yul profile image
Yuri Lopukhov

Hmm, I think possibility of conflicts does not depend on the order of replication, if a user tries to update an outdated record, conflict is inevitable. But perhaps how conflicts are resolved does depend on this order, I will need to test my cases to figure this out I think.

Thread Thread
 
tobymosque profile image
Tobias Mesquita

TL;DR, I'm avoiding 409 responses.

All depends on your conflict resolution strategy. I usually prioritize the server for two reasons.

The first is that the documents may have been replicated to other devices, so the unique affected device is the current one.

secondly it's cheaper to solve locally, pouchdb will not throw a 409 and force you to send other web request. at this point, you can easily ignore/delete the local document or compare both.