Best Practices and Tutorials

Document Conflicts & Resolution in Couchbase Mobile 2.0

14 MIN READ

Document conflicts can occur in distributed environments that support data synchronization, wherein a document can be updated concurrently by one or more writers. This is especially common in mobile environments where unreliable network connections may result in concurrent changes from multiple devices not being synchronized in a timely fashion.

It can also occur on the client-side, for instance, if a document is being updated locally while changes to the document are being pulled down to the client from a remote server. Hence, there is a need to resolve these document conflicts.  Couchbase Mobile 2.0, introduces “Automatic Conflict Resolution” or a “Conflict-free” mode wherein conflicts are automatically handled and there are effectively no conflicting document revisions in the Couchbase database.

As an app developer, you probably wouldn’t ever have to do anything specific on the app side in order to deal with conflicts because the system’s default conflict resolver will automatically handle things for you. However, if you need to, you have the option of being notified of conflicts and can take appropriate action. This post will discuss the fundamentals of how document conflicts are automatically handled in Couchbase Mobile 2.0 using the “automatic conflict resolution” or “conflict-free ” mode.

 

Assumptions

This post assumes that you are familiar with the architecture of Couchbase Mobile stack that includes the Couchbase Lite embedded database for your mobile clients, the Sync Gateway and Couchbase Server. If you are new to the platform, I would recommend catching with these resources.

Background

If you haven’t done so, it would be worthwhile reading this earlier demystifying conflict resolution post that discusses the fundamentals of how conflict resolution is handled on “non-conflict-free” mode in Couchbase Mobile including an overview Multi-Version Concurrency Control (MVCC) system.

Document Revision Tree

To understand the conflict resolution process, you would have to a basic understanding of how documents are stored. In Couchbase Mobile, every document is assigned a unique system-generated revision ID or revID. This ID is in addition to the document ID which remains the same across document revisions. Every change to a document, be it a modification or a delete is treated as a new revision to the document and is hence, assigned a new revision ID.

In addition, every document revision has a “generationID” associated with it. The first time a document is created, a revision gets created with a “generationID” of 1. Every subsequent revision update will increment this number.

Every time a change (edit or delete) is to be made to an existing document, the writer must include the revision ID of the current revision of the document that is being updated. A new revision is created for the change and added as a child node to the current revision that was being updated, resulting in a Revision Tree for the document.

NOTE: The “revID” must be treated as an opaque object by the app and the app must not try to generate the “revID”.

How Document Conflicts Occur

At a high level, a conflict occurs when changes are made to the same parent revision of a document by multiple writers.

Conflicts At Couchbase Lite

The illustration below describes a scenario where an app is updating a document revision in memory while the same revision is being updated as a result of a pull replication.

Conflicts At Sync Gateway

The illustration below describes a scenario where two clients push up changes to the same revision of the document.

Conflict Resolution in Couchbase Mobile 2.0

In “Conflict-Free” mode, the Sync Gateway essentially rejects all conflicting updates. The conflicts are automatically resolved at the time the document is created, updated or deleted.
So the implication of this is that there are no conflicting documents stored in the database and is effectively, “Conflict Free”. We will take a look at how this works.

Automatic Conflict Resolution in Couchbase Lite 2.0

Every document in Couchbase Lite 2.0 is associated with a “conflict resolver” that is executed if a conflict occurs when an attempt is made to save or delete a document. The conflict resolver function chooses a “winner” among the conflicting revisions and this winner is added as a child to the document revision tree.

There are two concurrency control policies supported by Couchbase Lite.

Concurrency Control Policy

Last Write Always Wins (Default):

This is also the default conflict resolver policy. In this case, the last update to the database always wins.

When you call saveDocument with no Concurrency Control argument, this is the policy that is in effect by default.

This is an example of the call in swift.

 let savedDocument = try saveDocument(docToSave)

The examples below should clarify the implications of this policy

  •  For example, our save / update request succeeds even though the document in the database was updated since the last time we read the document. The document in the database could have been updated by a different thread in the app as a result of some external trigger such as a pull replication or a data fetch from a remote server. The implication is that any changes that were made to the document between the time we read the document and updated it would be overwritten.
  • As below, our save / update request succeeds even though the document in the database was deleted since the last time we read the document. The document in the database could have been updated by a different thread in the app as a result of some external trigger such as a pull replication or a data fetch from a remote server. The implication is that a deleted document could be resurrected as a result of the save.
Fail On Conflict

While we anticipate that the default concurrency control policy of “last write wins” should work for most cases, you can override the default behavior by specifying that you wish to be notified in case of a conflict while saving the document. This can be done by including the optional ConcurrentControl argument as part of the save request.

A return value false with a ConcurrencyControl policy failOnConflict indicates that the document saved failed as a result of a conflict. We will now examine how you can handle conflict errors.

Handling Fail on Conflicts

How you handle conflicts depends on the application semantics. Here are examples of how you can handle it in swift (which can be easily mapped to other languages)

  • Option 1: Merge the conflicting versions of the document and save
do {
    var success = false
    repeat {
        if try db.saveDocument(docToSave, concurrencyControl: .failOnConflict) {
            // save was a success. docToSave is the new saved revision. Proceed as usual
            success = true
        }
        else {
            // Failure on save with failOnConflict mode
            // Handle conflict appropriately
            // First, fetch the current saved version of doc
            if let currDoc = db.document(withID: docToSave.id) {
                // Option 1 : Merge the contents of doc1b and currDoc
                docToSave = mergeDocument(newDoc: docToSave, currentDoc: currDoc)
            }
            else {
                // Doc was deleted. Handle appropriately
            }
        }
        
    } while (!success)
}
catch {
    print("Some other error in saving (error)")
}

This is an example of how you would merge the conflicting revisions. Again, how you would do it is entirely up to your application. This is intended to be a reference implementation on how you could handle it.


/// Merges two conflicting versions of CBL document using following criteria
/// - Newly added properties are added 
/// - If we are updating an existing property, that is ignored a property that already exists
///
/// - Parameter 
///   - newDoc: The new version of document to be saved
///   - currentDoc : The current saved version of the document
func mergeDocument(newDoc: Document, currentDoc: Document) -> MutableDocument {
    // 1. Create a resolved mutable document based on the current saved document
    let resolved = currentDoc.toMutable()
    
    // 2. Add the additional properties that don't already exist
    for key in newDoc.keys {
        if !resolved.contains(key) {
            resolved.setValue(newDoc.value(forKey: key), forKey: key)
        }
    }
    return resolved
}
  • Option 2: Force your save to win

This option is effectively the same as the default concurrency policy of “writes always win”. Except that in this case, you examine the contents of the currently saved document and then make a determination on whether to force a save using a saveDocument.
Note that you could still run the risk of a race condition in that the document could be updated again before you save it.

do {
    if try db.saveDocument(docToSave, concurrencyControl: .failOnConflict) {
        // save was a success. docToSave is the new saved revision. Proceed as usual
    }
    else {
        // Failure on save with failOnConflict mode
        // Handle conflict appropriately
        if let currDoc = db.document(withID: docToSave.id) {
            // Option 2: Examine contents of current saved version of doc
            // Force write of document
            try db.saveDocument(docToSave)
        }
        else {
            // Doc was deleted.
            // Option 2: Attempt to do a force write of document to override the delete
            try db.saveDocument(docToSave)
        }
    }
}
catch {
    print("Some other error in saving (error)")
}
  • Option 3: Skip the save

In this case, you could examine the contents of the currently saved document and then determine that you would rather keep the currently saved version of the document.

do {
    let docToSave = MutableDocument(id: "docId")
    if !(try db.saveDocument(docToSave, concurrencyControl: .failOnConflict)) {
        // Fetch the current saved version of doc
        if let currDoc = db.document(withID: docToSave.id) {
            // Option 3 : Examine contents of doc
            // Current saved doc looks fine. Do nothing
        }
        else {
            // Doc was deleted. Handle appropriately
        }
    }
    else {
        // save was a success. docToSave is the new saved revision. Proceed as usual
    }
}
catch {
    print("Some other error in saving (error)")
}

Implications on Creating Documents with the same Id

Since the default conflict resolution policy on saveDocument is that the last write always wins, if you attempt to create a document again with a docId that already exists in the database, it will go ahead and update the existing document by adding a new revision to it.

Hence, if you want to ensure that you do not inadvertently update an existing document, you must specify the “ConcurrenyControl argument with failOnConflict. This will then return an error that you can handle appropriately.

This is very much similar to the Option 3 specified earlier except that in this case, you don’t have to examine the current contents of the document. A conflict failure implies that a document with Id already exists.

This is an example in Swift

do {
    let docToSave = MutableDocument(id: "docId")
    if !(try db.saveDocument(docToSave, concurrencyControl: .failOnConflict)) {
        // Document with Id already exists. Handle appropriately
    }
    else {
        // save was a success. docToSave is the new saved revision. Proceed as usual
    }
}
catch {
    print("Some other error in saving (error)")
}

Conflict-Free Mode in Sync Gateway 2.0

In this mode, the sync gateway rejects revisions that cause a conflict with an HTTP 409 Error, effectively ensuring that there are no conflicting revisions in the database. Conflicts are handled on the client-side during a pull replication.

Push Replication

  1. The client pushes up the revision changes to Sync Gateway.
  2. Sync Gateway detects that the incoming revision is in conflict with the current saved revision on the server (i.e. the incoming revision’s ancestor is not the active revision on the server)
  3. Sync Gateway rejects the revision change with a 409 Error. Couchbase Lite does not really do anything other than log the error. The conflict is subsequently resolved during a pull replication.

Pull Replication

During a pull replication, if the client detects a conflict, the conflict is resolved using the following deterministic criteria

  • Deletes always win. An example of this case is as shown below.

  • The most recent change (highest generation ID) wins or the revision with max revID wins if the generations are the same. i.e. – the revID that sorts higher in a simple ASCII comparison. An example of this is shown below.

 

Next, we examine the cases during Pull Replication.

Server Branch is the Winner

  1. Client pulls the revision changes from the Sync Gateway
  2. Client detects that the incoming revision is in conflict with the current saved revision (i.e. the incoming revision and saved revision share a common parent)
  3. Client calls the conflict resolver function which determines the winner between the current saved revision and the server revision.
    1. Since the incoming revision (Rev2-B) from the server is more recent than the local revision (Rev2-A), the server revision is selected as the winner. Note that this would also be the case if the revision on the server was deleted.
  4. The server branch is grafted to the local revision tree (and the local branch is tombstoned)
  5. Subsequently, when changes are pushed to the server, there will be no conflicts and the server syncs up with the client-side.
Local Branch is the Winner
  1. Client pulls the revision changes from the Sync Gateway
  2. Client detects that the incoming revision is in conflict with the current saved revision (i.e. the incoming revision and saved revision share a common parent)
  3. Client calls the conflict resolver function which determines the winner between the current saved revision and the server revision.
    1. Since the local revision (Rev2-B) is the more recent than the incoming server revision (Rev2-A), the local revision is selected as the winner. Note that this would also be the case if the revision on the local branch was deleted.
  4. The server branch is grafted to the local revision tree and a new revision Rev3 is added which corresponds to the contents of the winning revision, Rev2-B
  5. Subsequently, when changes are pushed to the server, there will be no conflicts and the server syncs up with the client-side.

NOTE: As you have observed, conflicts are resolved on Couchbase Lite during a pull replication. The implication of this is that if the replicator is configured to only be a push replicator, then Couchbase Lite’s view of the data will diverge from Sync Gateway’s (since some of CBL’s attempted writes would fail w/ 409). Hence, if you foresee conflicts to occur, then it is recommended that the Couchbase Lite replicator be configured in push-pull mode when used in Conflict Free mode

 

Configuring Conflict-Free Mode on Sync Gateway

“Conflict Free” mode on Sync Gateway is configurable through allow_conflicts property in the Sync Gateway configuration file. It must be set to “false” to enable conflict-free mode. It should also be noted that regardless of this property configuration, there will be no conflicting revisions added to the Sync Gateway when syncing with Couchbase Lite 2.0 clients. We discussed that earlier in the section on Replication. The “allow_conflicts” configuration only has implications for non 2.0 clients and REST API clients. The table below summarizes the implication

allow_conflicts_configuration

Impact of Conflict-Free mode on Database sizes

The impact of conflict resolution on database size was discussed in detail in our earlier post on Managing database sizes. In the system that allowed for conflicts, as conflicting revisions grow, the size of the revision tree can grow to impact the size of the database. So it was important to resolve conflicts in a timely fashion. Sync Gateway has a revs_limit property that determines the size of the revision tree. The revs_limit property defaults to 1000, which means that the metadata corresponding to the last 1000 revisions are stored in Sync Gateway before getting pruned away. While setting the revs_limit to a large value would negatively impact the database size, it was important not to set it to a very low value. It was important to maintain the metadata corresponding to the older revisions as they were needed to handle conflict resolution else you run the risk of having a forest of disconnected revision trees for a document. So the minimum permissible value of revs_limit is 20.

However, with conflict-free mode, there is no real need to save the metadata corresponding to older revisions. This implies that the revs_limit could be just 1. This implies that only the latest/active revision is stored. This brings big savings in database size.

Acknowledgments

Special thanks to Pasin Suriyentrakorn from the Mobile Engineering team for his feedback on the Couchbase Lite sections, to Adam Fraser, also from the Mobile Engineering team, for his feedback on the Sync Gateway sections and to Daniel Petersen from the Mobile Engineering team for his feedback.

What Next

This blog post discussed how conflicts are automatically handled in Couchbase Mobile 2.0. You can download Couchbase Mobile 2.0 from our downloads page.

If you have questions or feedback, please leave a comment below or feel free to reach out to me at Twitter at @rajagp or email me at priya.rajagopal@couchbase.com.  The Couchbase Forums are another great place to reach out with questions.

 

Share this article

Author

Priya Rajagopal is a VP of Product Management at Couchbase responsible for developer platforms for the cloud and the edge. She has been professionally developing software for over 20 years in several technical and product leadership positions, with 10+ years focused on mobile technologies. As a TISPAN IPTV standards delegate, she was a key contributor to the IPTV standards specifications. She has 22 patents in the areas of networking and platform security.

Leave database sprawl in the past

Every stack addition means another bill, and more to manage. Estimate how you could save in 2 minutes.