Spring boot ID generation in XDCR


I am new to Couchbase. I want to generate unique Id (preferable number format) to save the document. I am using couchbaseTemplate to generate it with below code

couchbaseTemplate.getCouchbaseClientFactory().getDefaultCollection().binary().increment(“my-id”, IncrementOptions.incrementOptions().initial(1001l).delta(1)).content();

Question: In case of multiple Data Centers there is a chances that both Data centers generate same id. How can I prevent this situation using Spring Boot Data library?

Hello @ramitgulati welcome to the forums, what I am understanding here is that you have an Active-Active setup where applications are writing to 2 different clusters which you are trying to sync up using XDCR ? Is this the usecase ?

Thanks for the reply @AV25242 !
Yes in active active set up with multiple Data Centers if I am generating the unique Id using below code, is it possible DC-1 generates say ID 1002 and with the next hit DC-2 also generates ID 1002 which will defeat the purpose of uniqueness?

couchbaseTemplate.getCouchbaseClientFactory().getDefaultCollection().binary().increment(“my-id”, IncrementOptions.incrementOptions().initial(1001l).delta(1)).content();

The problem statement you have will probably be handled with conflict resolution but the impact is what I am not certain.

So in Active-Active with XDCR enabled (hoping its bi-directional) I would expect ID 1002 when its created be also available in DC2 , it would be a problem if 1002 is created in DC2 by the application while the replication is not done yet - (we are talking about few milliseconds latency may be ? ) . Basically that would be a race condition where application writes to DC2 ID 1002 before Replication happens.

Question will be is this an edge case ?
What is the latency for replication

@hyunjuV any inputs ?

My concern is if during the high traffic two concurrent users hitting our application say User-1 and User2.
User-1 request goes to DC-1 generates ID - 1002 and at the same time there is a possibility that User-2 request goes to DC-2 and genrates the same ID -1002.
So my understanding this can occur even with very low latency for replication.
You said this can be handled with conflict resolution? Can you please elaborate more or sample piece of code would be much appreciated?

The xdcr conflict resolution will not resolve a situation like the one you are describing – where there is a document key collision. The available conflict resolution methods will treat them as same documents, and one will be the winner. Your application will need to make sure that it is generating a globally unique id (i.e. an id that is unique among all clusters) – so, it may need to add the cluster name/id or something to the key to make it unique.

1 Like

Thanks @hyunjuV ! Got it . Any idea how i would get the cluster name using spring data couchbaseTemplate. I tried couchbaseTemplate.getCouchbaseClientFactory().getCluster() but not seeing any getter with name may be i am missing something?

@ramitgulati your application should be aware of your cluster isnt it ? what I meant is your application should be connected to a particular cluster via a configuration, can you not add a config setting that you can refer to to get the cluster name ? Or is your application context switches dynamically ?

Got it what you are saying!
I will check with my app ops team whether there is any possibility to automatically switch the traffic dynamically. I am good for now thanks for all the responses and let you know if I will have more questions

thanks again !