How to deal with id sequences in couchbase lite 2.0


How to deal with the Id sequence when you have multiple devices running couchbase lite, all sync with sync gateway, and in a online/offline system ?

Do I have to it myself, or couchbase include something helping ?



Can you be a bit more specific on your question -

How to deal with the Id sequence

The Couchbase Replication protocol will handle sequence Ids and such for you . You shouldn’t have to worry about it. Of course, you could provide custom conflict resolvers if you want to otherwise the default conflict resolver will apply.


When you look at the travel-sample database, you can see that each document has their ID, as a long value. This value is part of the documentID too. for exemple you have the document airline_4, with some keys, one of them is id = 4.

When you add a now airline from a mobile app, how do you choose your id ? If you use your Iphone and your android phone to add a new airline document, how can you obtain the next id, (id=5 in my exemple). And how do you do to know that this ID was reserved on your Iphone app first (for exemple).

Adding the fact that each apps can be used offline, so with sync gateway off, when you start it back after inserting a local document, you will deal with an id collision.

I can invent a solution for exemple in reserving ranges for each mobile apps, so my question is : Is there something provide by couchbase lite 2.0 to do it ?



The travel sample database comes from Couchbase Server. On the server it’s of course easier to assign unique IDs, for example by using the Increment operator.

In a distributed system there are several ways to assign document IDs —

  • Use a UUID. CBL will do this if you request a new document without giving an ID. Or you could generate your own UUID and then add some other identifier to it, like a document type or something (this can make it easier to find docs by type without having to create a view/index.)
  • Use a unique ID already associated with the object the document represents. For example, if the document is a book you could use the ISBN. This is good if there needs to be one and only one document representing this object, because it obviously prevents duplicates.
  • Use the user ID as part of the docID, like “jens-TaskList”. This gives every user their own namespace, so collisions can only happen between devices logged into the same account, which is more tractable.