Handling conflicts in CouchDB

1.8k Views Asked by At

Say I have a doc with two properties, "Start" and "End." One revision may have a time for Start and null for End, and vice versa. Rather than choosing a single revision as the winner, I want the final doc to contain the Start time from the revision where it is not null, and same for End.

Are there any best practices for handling this type of conflict resolution during a sync? Documentation I have found contains instructions for choosing a single revision as the winner, but I'd like to select values from multiple revs.

Examples specific to C#/MyCouch library would be great, but any general or other language advice is also much appreciated.

1

There are 1 best solutions below

0
On BEST ANSWER

You cannot specify a custom way of conflict resolution during replication (a.k.a. sync). CouchDB automatically chooses the winning revision, and you cannot influence that:

By default, CouchDB picks one arbitrary revision as the "winner", using a deterministic algorithm so that the same choice will be made on all peers.

You can wait for the replication to finish and handle conflicts afterwards, by performing application-specific merging of document revisions.

Looking into the documentation for Working with conflicting documents, I found the following pseudocode example:

  1. GET docid?conflicts=true
  2. For each member in the _conflicts array: GET docid?rev=xxx If any errors occur at this stage, restart from step 1. (There could be a race where someone else has already resolved this conflict and deleted that rev)
  3. Perform application-specific merging
  4. Write _bulk_docs with an update to the first rev and deletes of the other revs.