0

I am implementing a basic sync strategy for a multi-client application that needs to support offline data access. I am using @Chris' suggestion in his answer to this question (not required reading).

One detail I would like to add is the ability to resolve conflicts based on the last change saved, not the last change synced. In other words, if two clients update the same item, the client that saved the change last should win, even if the other client syncs later.

Clearly I need some way to timestamp each change on the client, so I can compare the stamps on the server at the time of sync. However, I can't guarantee much about each client's internal clock.

I would like to know if there is an established way to solve this? The simpler the better!

Community
  • 1
  • 1
Ben Packard
  • 26,102
  • 25
  • 102
  • 183

2 Answers2

2

If you're asking about client clock hijacking: a client should maintain it's own internal clock based on timestamp it gets from server and time span got from local clock.

So you just update 'client timestamps' relatively to server:

  • Client record has CT1 update time;
  • After connecting to server at the moment of CT2, you find out that server time is ST2;
  • So record update time is changed to ST1 = ST2 - CT2 + CT1.

The other way is maintaining the same transformation at server side. (Which is probably more correct and secure).

And sorry - just a note - odd part is that you call it 'conflict resolution', when it's more 'last update wins' and no actual resolution is performed.

mikalai
  • 1,746
  • 13
  • 23
  • 1
    Thanks for note on terminology. – Ben Packard Jun 18 '13 at 00:12
  • This is problematic, what if the time is wrong on the local machine? Also, what if the wrong time is changed somewhere in between? 5% of my users have the wrong time on their machine, and that may seem like a small number but with a large base it can be thousands of users. – daniel metlitski Jul 07 '16 at 22:24
2

I'd not be happy trying to get away with doing it based on just timestamps, I think you need to be looking at a proper versioning solution. I don't know what language you are using etc, but I have a complete library for doing this. Even if the library is not interesting you might find the documentation for it is useful in constructing your own solution... it's pretty fully explained...

The project is on GitHub.

Forbesmyester
  • 936
  • 6
  • 15
  • What happens if the time on the local file and the server are wrong – daniel metlitski Jul 08 '16 at 00:42
  • 1
    Good question... The server is under your control so it should be correct or at least incrementing consistently... In reality it is controlled by unique ID generators which are orderable... In effect it is: 1) I've wrote this changes in this order. 2) And I have these changes from the server in this order. 3) I can merge these changes using this algorithm (which is overridable by the developer of the application). – Forbesmyester Jul 14 '16 at 09:30