0

What is best practice for data synchronization operation between client and server?

We have 2 (or more) resources:

  1. cars -> year, model, engine
  2. toys -> color, brand, weight

And we need to get updated resources from server in case of any updates on them. For example: someone made changes from another client on the same data and we need to transfer those updates to our client application.

Request:

http://api.example.com/sync?data=cars,toys (verb?)

http://api.example.com/synchronizations?data=cars,toys (virtual resource "synchronizations")

Response with mixed data:

status code: 200

{ message: "ok", data: { cars: [ { year: 2015, model: "Fiat 500" engine: 0.9 }, { year: 2004, model: "Nissan Sunny" engine: 1.3 } ], toys: [ { color: "yellow", brand: "Bruder" weight: 2 } ], } } 

or response with status code 204 if no updates available. In my opinion making separated http calls in not a good solution. What if we have 100 resources (=100 http calls)?

1 Answer 1

1

I am not an expert, but one method I have used in the past is to ask for a "signature" of the data, as opposed to always going and getting the data. The signature can be a hash of the data you are looking for. So, flow would be something like:

  1. Get signature hash of the data
http://api.example.com/sync/signature/cars 

Which returns the signature hash

  1. Check if the signature is different from the last time you retrieved the data

  2. If the signature is different, go and get the data

http://api.example.com/sync/cars 
  1. Have the REST also add the new signature to the data
{ message: "ok", data: { cars: [ { year: 2015, model: "Fiat 500" engine: 0.9 }, { year: 2004, model: "Nissan Sunny" engine: 1.3 }, ], signature: "570a90bfbf8c7eab5dc5d4e26832d5b1" } } 
Sign up to request clarification or add additional context in comments.

1 Comment

Hi Tim. The problem isn't in catching updates. In your sample It depends on the difference between client's hash and server's hash. But in my case I'm trying to make ping calls to the server (each 1-3 seconds) and fetch different updated resources in a single http call.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.