conflicts can occur in replication environments where a document can be updated concurrently by one or more writers. this is especially common in mobile environments wherein unreliable network connections may result in concurrent changes from multiple devices not being synchronized in a timely fashion , thereby resulting in conflicts.
in this post, we discuss the fundamentals of how conflicts are handled in couchbase mobile and outline the role of applications in resolving conflicts. in a following post, we will discuss a related topic of managing revision trees and database sizes in a revision based document management system.
the illustration below describes a scenario where conflicts can occur in mobile environments
stack includes the
embedded database running locally on devices and
in the cloud which is typically backed by
persisting the data in the cloud. the sync gateway handles the replication of documents across the devices. it is conceivable that a document can be updated by multiple devices at the same time.
multi version concurrency control
couchbase mobile uses the multi version concurrency control (mvcc) technique to handle conflicts. in this method, every document is assigned a unique system generated revision id. this id is in addition to the document id which remains the same across document revisions. every change to a document, be it is a modification or a delete is treated as a new revision to the document and is hence, assigned a new revision id.
every time a change 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.
every document has an associated revision tree that grows over the lifetime of the document. we will discuss techniques to manage the size of the tree in a future post – stay tuned!
at a very high level, every document in couchbase mobile v1.4 is comprised of a document id, current revision id, json body and metadata. the metadata, among other things, holds the revision history for the document. the metadata is a “behind the scenes” concept, and user applications should never really worry about it. in fact, in
of couchbase, the metadata will be moved out of the document completely and into an xattr.
in addition, every document revision has an associated ttl value (that defaults to 5 minutes).
in a mvcc based system, every update including a delete operation creates a document revision. deleted revisions are called “tombstone” revisions. a deleted revision is essentially a special revision that has the “_deleted” property to true. deletion revisions are replicated. these revisions are special in that if you do a query on couchbase lite, they won’t be returned.
the revision id structure
a revision id is of the format
<generation id> “-“ <content hash id>
generation id (new revision) = generation id (parent of the revision) + 1
the very first revision that is created when a document is created has a generation id of 1
content hash id =
hash computed from the contents of the revision
this implies that two revisions of a document with identical content will have the same content hash id.
: as an optimization, if two writers make identical changes to a document concurrently, resulting in two incoming revisions with the same revision id, couchbase mobile will only store a single revision.
in a mvcc based system, a conflict occurs if the system encounters a branch in the tree. from the discussion on revision trees, you can infer that this would be the case where there are two or more leaf nodes in the tree.
when a conflict occurs, couchbase mobile still needs to select a “winner” or “current revision” among the conflicting leaf nodes. picking a winner does not imply that the conflict is resolved.
couchbase mobile picks a winner deterministically. due to the deterministic nature of the process, there is no need for couchbase mobile nodes to communicate with each other about picking winners – they all pick the same one using the following criteria
case 1 : all leaf revisions are undeleted
the winner is the undeleted leaf revision on the longest revision branch
case 2: all leaf revisions are deleted
the winner is the deleted leaf revision on the longest revision branch
case 3: some leaf revisions are deleted and some are undeleted
the winner is the undeleted leaf revision on the longest revision branch
case 4: there is a tie
the winner is the one whose revision id sorts higher in a simple ascii comparison
who must handle conflicts ?
although couchbase mobile picks a current revision among conflicting revisions, it is the ultimately the responsibility of the application to resolve conflicts for the following reasons –
the choice of the winner among conflicting revisions may be based on criteria other than the ones that the system uses to deterministically pick a winner. the current revision selected by couchbase may not be the right choice for the application.
there isn’t a clear winner among the conflicting resolutions. so, in this case, there may be a need to merge changes from conflicting revisions. the specifics of a merge depends on application semantics.
even if the application decides to go with the revision that couchbase picked as the winner, remember, that the conflicting revisions still remain in the database. so, the application must still delete the non-winning revisions from the revision tree in order to prevent the revision tree from growing very large with unused conflicting revisions. this will be discussed in depth in an upcoming post.
options for handling conflicts
there are two options for handling conflicts
option 1: pick a revision among conflicting revisions
in this option, one of the conflicting revisions as the winner and “tombstone” the rest.
the application can retain the “current revision” picked by couchbase as the winner or it can pick a different revision among the conflicting revisions. regardless, it is important to delete the non-winners so they can be purged during database compaction as discussed in the next section
option 2: n-way-merge
in this option, the changes from conflicting revisions are merged according to application semantics. the merges go into a new revision which becomes the current/winning revision. the non-winning branch is tombstoned.
what if conflicts are not resolved ?
if an application decides to go with the winner picked by couchbase mobile and does not explicitly resolve conflicts, you can end up with a revision tree with a large number of branches and leaf nodes. this will have undesirable consequences on the size of the document and consequently, on the size of the database. couchbase mobile has automatic processes in place to alleviate some of this but the application is ultimately responsible for ensuring that unwanted leaf revisions are tombstoned.
we will discuss the details of database and revision tree management in a following post , so stay tuned.
this post was an introduction to the concurrency control system in couchbase mobile. one important consideration in a mvcc based system is managing the size of the revision trees and to preventing it from bloating. this aspect will be discussed in an incoming post.