Question

If I store an array-like structure in couchbase like this one:

mykey = 3
key_1 = 47
key_2 = 11
key_3 = 17

and my update procedure is something like this:

a = increment(mykey)
set key_a = 42

will this work on a bucket replicated across multiple datacenters? Is there a better way of doing this?

I'm thinking that two clients on different data centers might call increment at the same time, getting the same value, and then setting the same key to different values.

Was it helpful?

Solution

If you're using XDCR, situations with duplicate keys are possible. And even if you check value before set (using couchbase.add operation) it can also produce two identical keys.

Within a cluster, Couchbase Server provides strong consistency at the document level. On the other hand, XDCR also provides eventual consistency across clusters. Built-in conflict resolution will pick the same “winner” on both the clusters if the same document was mutated on both the clusters. If a conflict occurs, the document with the most updates will be considered the “winner.” If the same document is updated the same number of times on the source and destination, additional metadata such as numerical sequence, CAS value, document flags and expiration TTL value are used to pick the “winner.” XDCR applies the same rule across clusters to make sure document consistency is maintained.

To avoid this couchbase recommends to store some info about datacenter/cluster or use unique keys like GUIDs. I think that the second way is not preferred, so you can implement the first one by adding datacenter location as key prefix and handle them on application side:

US-east.mykey_1
US-west.mykey_1
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top