I have a set of objects, let's say these are “messages”, and these objects can be changed. I would like to display a list on the client side that is being updated dynamically. Thus, on the client side, if you do this through a poll, the client will call the API as:
getPostsChangedSince(serial)
where serial can be a monotonously increasing number, probably a timestamp. The client returns a list of messages that have changed since that time, stores the new last serial number, and the next time the client polls, it requests changes from the moment of the last serial number.
I think the main idea in this question is the same (we are talking about ASP.NET): How to implement "get lattests with changed elements" with ADO.NET data services?
I am trying to find a better way to implement this in MongoDB.
I like the idea of using time for the series, because it automatically works, at least mostly correctly, even if there are several application servers. The serial number will be stored in each post-object and updated with every change in the object.
Timestamp based serial number can be implemented as:
Some useful features to solve will include:
- make sure that creating and immediately updating an object in OS timer resolution will continue to increase the number of serial numbers, even though it is equal to one time
- it would be even better to guarantee a monotonic increase on a global scale for all objects, and not only guarantee that changing this object will lead to a failure on this object (in the absence of calls to getPostsChangedSince (), it will probably take a while to avoid omitting the change - by the price of receiving some changes twice)
- Mongodb timestamps can be enjoyable because getting time in the application creates a gap between when you get time and when a new object is saved and available in requests
- using findAndModify () with the query, including the old serial one, so "conflicts" (two changes at once) will cause an error that allows the application to retry
I understand that some of the corner cases here are a bit “academic” and probably can be tricked into real life.
So far my approach is:
- use date type for sequential
- when changing the object, get the current time, and if it matches the object of the old serial number, add 1 millisecond (yes, it breaks if you make two modifications quickly without reinstalling from mongodb, but it seems OK)
- use findAndModify (), but based on https://jira.mongodb.org/browse/JAVA-276 there can be no way to determine if it will end with something, change (i.e. the second change is ignored, in case of conflict )
Questions:
- I feel that I should use Timestamp instead; true? Any flaws?
- If you have a mongo cluster, can the time in milliseconds be more unique and correct than the Timestamp time in seconds plus a number, and with one mongod Timestamp is more unique?
- Is there a way to find that findAndModify () has updated something?
- Any general tips / experience with this problem? how would you do that?
source share