Couchdb online offline synchronisation

2019-08-12 13:28发布

问题:

I'm building a .NET application which will have a local CouchDB instance where all data is written to, and a central CouchDB where the local CouchDB is synchronised to. The difficulty is that there will be multiple clients which will synchronise with the database at different points (and could edit existing data, creating conflicts). The internet connection will not be consistent, so consequently the majority of data will be created/ edited while offline.

I believe that CouchDB handles this inherently, however I'm having difficulty figuring out whether I should be working with the _changes or _replicator functionality - or a combination. Would very much appreciate a few pointers on how to handle this scenario.

回答1:

Couchdb will detect conflicts for you, however it leaves the responsibility of handling those conflicts to you. Here are details on couchdb conflict model and some ways to design you application for replication.

_changes is not a good way to replicate because it only provides recent changes to a given document. So if you replicate using a changes feed to a database that is newly created you will loose some of the older changes for it. And you will have to manually perform the operations like updating documents that _replication does it for you. _replication also has some goodies like the ability to create a target database if it does not already exist.

Even if you are listening to continuous feed of changes not every update to the document is guaranteed to be returned.

Returns a sorted list of changes made to documents in the database, in time order of application, can be obtained from the database’s _changes resource. Only the most recent change for a given document is guaranteed to be provided, for example if a document has had fields added, and then deleted, an API client checking for changes will not necessarily receive the intermediate state of added documents.

So replication is the way to go if you plan on replicating the data across various devices.