Emre Koç
Emre Koç

Reputation: 1381

Android Single Server Multiple Client Data Synchronization

These days i am working on an Android Application and i have problem with data synchronization.

I am using JSON for transferring data.

Now i'll explain my problem.

Assume that you have one server and multiple Android devices which is sending data between each other. We have same database tables not only on android devices but also server.

Operation of system is like this :

At the beginning of day, Android devices must receive data from server.

During day, Android devices can change data in own database but it won't change data on server database.

At the end of day, Android devices will send

My problem is starting here..

My code is logging every changes in Android device for sending at the and of day to server.

User A is added this data during day (id=1024 name=testA value=testAvalue)

User B is added this data during day (id=1024 name=testB value=testBvalue)

And then user B wanted to change data name during day (id=1024 name=testC value=testBvalue)

At the and of day, Firstly user A sent data to server. Now server has this data

(id=1024 name=testA value=testAvalue)

Secondly, user B sent data to server. Create(id=1024 name=testB value=testBvalue)

Here this data id will not be 1024 because server database has data which id is 1024. New data id will be 1025.

Now server has 2 data (id=1024 name=testA value=testAvalue) *(id=1025 name=testB value=testBvalue)*

And then, server will receive edit command like this

Edit(id=1024 name=testC value=testCvalue)

It will edit 1024. data and it will be wrong

(id=1024 name=testC value=testCvalue)

(id=1025 name=testB value=testBvalue)

Upvotes: 2

Views: 1309

Answers (2)

Havrl
Havrl

Reputation: 774

One of your problem is in ID collision. You need to select the appropriate column types for your primary keys based on your specific requirements.

Let me suggest few options (they all have some advantages and disadvantages).

  • Auto-Increment (Identity) columns: the clients only create temporary ids and replace with permanent ones after the server creates new records and send the newly generated ids to the clients.
  • Or the server will designate the range of ids for each client. E.g. Client A will insert records from 1-100000, Client B: 100001-200000, etc. Once new records riches the range limit the server should issue the new range.
  • GUIDs: each client will generate unique id on insert using new UUID() command and then your server will be able to insert or update the client changes without any problems.

Upvotes: 1

Dave Swersky
Dave Swersky

Reputation: 34810

This is a typical "he who saves last wins" problem. You have copies of the data out in the field, and you need to aggregate and synchronize updates at the end of the day. The problem here is not so much technical as it is a problem of design:

"How can I accept batched data updates from multiple sources when the updates conflict?"

So, this is really a conflict resolution problem. The "right" solution will depend on your application requirements.

One solution is to assign "ownership" of records to a device, so that only that device can make an update. An example might be a sales force in which representatives are assigned clients. Only the rep assigned to a client can make changes to records associated with that client.

Another solution is to write specific rules for resolving conflicts into the system. Your specific business case will determine exactly how to resolve each conflict. As long as your application cannot make changes to the server in realtime, this is probably your best bet to accept updates to the same record from multiple sources.

Upvotes: 3

Related Questions