How to deal with "create_time" in couchbase-lite while devices is offline and the device's time is not correct?


I have a web based app and a mobile app that synced document with sync gateway.and I can create/modify documents from mobile and webpage(the documents will be push and pull synced).
I need to use view to query and order document by the document’s create_time by view in couchbase-lite and sync gateway.
one problem is that when I create a document in couchbase-lite while devices is offline and the device’s time is not correct, the doc’s create_time is not correct, so when my device is online,the documents are synced, the results of query and order documents by document’s create_time by view is not correct.
How should I fix this problem?

Chat messages ordered by date, how?

You can’t, really. This is one of the basic limitations of a distributed system: there isn’t a consistent view of time, so you can’t reliably use wall-clock timestamps to order events. (This has been an issue in computer science since the 1970s.)

In your case, I don’t think there’s a way of finding out the true time the doc was created, if you can’t trust the system clock. You can’t use the time on the server because that would only tell when the doc was uploaded, which could be hours or days later than when it was created, depending on how long the device was offline.

You may just need to recommend to your users that they turn on automatic clock sync on their devices (which should be on by default on anything with a cellular connection, anyway.)


Thank you for reply. If I can not use timestamps to order events, Is there any way to order document by the create_time that document stored in Couchbase Server in couchbase-lite?


No, that’s just internal metadata that isn’t part of the document. (If it were, adding the create_time would change the document, creating another revision, which would then have to be synced again to all clients…)

In any case, the server’s create_time is not accurate if the document was created while the device was offline, since it only records the time the document was synced to the server, not the time it was actually created.


OK,I just think that I can maintain a order document with array of event ids, it looks like revision tree. but if there are so many events,there might be some performance issue.
Thanks again.


There are other ways to represent ordering, if you need to enforce a “happened-before” relationship between documents… I suggest reading about Lamport Clocks, as a starting point. Or you can have each document contain a set of zero or more docIDs that it’s directly dependent on (i.e. happens after), and perform a topological sort.


Thank you very much. I will choose the simplest way – make the device turn on automatic clock sync.