Java – Synchronisation with offline system

concurrencyjavaspringsynchronization

I'm designing a system from which I will synchronise business data from the mobile device (that have an embedded application) that generates data and sends it back to the server. Each line synchronised generates a specific business log in the database.

If what I synchronise generates data with a date (within the sync data) inferior to the last modification date of my business data, I must ignore it and just add the log in database. Once the uploaded data is processed, data is fetched from the database and downloaded to the device.

Because of this download right after writing, the synchronisation must be synchronous. It's possible still to have a reader/writer pattern if something like this is worth enough to replace my existing solution. The more important thing is to be able to download up-to-date data. That data is fetched as a whole, there is no diff implemented at the moment (it may come later but that won't be a problem).

I may have multiple synchronisations on the same business object running, it's unlikely but can happen and I prefer to be able to handle it. The synchronisation is expected to last for some seconds but not some minutes, unless using the embedded mobile application without resync for some days.

The volume of data synchronised is not expected to be big, neither the synchronisation process.

So I end up using a mutual exclusion on my method of synchronisation, more precisely, I'm using Java and I put a synchronized on the writing method not the whole synchronisation process to not block read-only synchronisation.

I would like to know :

  1. If this way makes sense? As long the volume and time of synchronization process are still acceptable.
  2. In a general way, what concepts should I look at. Bonus: if there is any implementation of these concepts in a Spring module.

Best Answer

One approach that I've been investigating for awhile now (with some success) to have client data sync up with server data, without relying on dates (which might be unreliable) or synchronous requests, is a combination of JSON Patches (perhaps POJOs in your case) and event sourcing.

The basic idea is that instead of storing the current state on the client and the server, the client and server store a list of changes, and message each other through either events or patch requests.

So instead of having the client send all the data plus a date to the server, the client sends an event, along with a revision number that corresponds to the last time the client thinks the data has updated. Something like this:

Server.send("MODIFY FOO", 3);

Once the server gets this event (asynchronously), it reconciles it with other events it may have already received. For example, it's possible that another client working with the same data might have already modified some things, and now the revision number on the server is at 5. Thus, this revision will need to be applied before the last 2 were applied, and all clients will need to be notified of this change.

Once the server finishes, it notifies all the interested clients of the changes have been made, and the new current revision number. The client then applies these changes and updates its internal revision number.

Your mileage may vary, but I hope that helps.

Edit: Another name for this approach, or a variation of it, is called message queuing, as mentioned in this related question.

Related Topic