I
I
imater2012-12-04 06:43:28
JavaScript
imater, 2012-12-04 06:43:28

Tell me the algorithm for synchronizing an array of strings in javascript with strings stored on the server in mySQL. How to sync multiple computers with a server

At me on the server the data of users is stored in one table. I need to set up synchronization with the client's browser so that the data is stored for offline access. The array of objects will be stored in the browser's localstorage as an array of objects matching mySQL strings on the server.

A user can have several computers with browsers and can change data anywhere. All changes must be synchronized with the server, eliminating collisions at a later change time. The strings between the server and the client can be sent as a whole, there is no need to track the change of each field separately.

What modern replication technologies should be used?

So far, the algorithm looks like this:
1. We keep a log of changes, where we write down the time and id of the line that has changed. (this raises the question of synchronizing the clocks on all the user's computers, and this is unbearable)
2. When synchronizing, we send a unique device code to the server, and the last time of successful synchronization.
3. All records that have changed in the log since the time of the last successful synchronization are sent to the client for updating.
4. The client checks if he himself changed the data from these servers that changed later, he keeps his fresh data and sends a command to the server to change the server data to his fresh ones.
5. The client extracts all the latest data from its own changelog and sends it to the server.
6. Periodically poll the server: Have there been any changes since the successful synchronization of my device code, if so, then repeat the steps.
7. If there are too many changes, or the time is very out of sync, or the client is new, then the server sends all its data to the client, and the client sends its data to the server. Both record in their changelogs the latest date of successful synchronization.

The answer can be written in the form of an approximate algorithm in Russian.

Answer the question

In order to leave comments, you need to log in

4 answer(s)
P
Pavel Tyslyatsky, 2012-12-04
@tbicr

Data:
1. Take synchronization time from the server.
2. On the client, store the actual data, the last synchronization time (referenced from the server) and the id of the data changed since the last synchronization. If there is no data yet, then the last synchronization time is taken as 0.
3. On the server, store data with an additional last_update field or a change log (separate table) with the change time and record id (In any case, such a log can be compressed to the size of the data itself by deleting old duplicate id's).
Description:
1. The client knocks on the server and sends the latest synchronization time and the data changed since the last synchronization.
2. The server saves the data if there were no conflicts, and returns the updated data after the last user update (it is possible without user updates), the new update date. And the client updates the data and deletes the sent id records after synchronization.
3. If there were conflicts (the date of the last change of the user is less than the last change on the server and the data hashes are different), then you can postpone the update and transfer data to the user for merge, while updating the last_update of such records to the synchronization date returned to the user. After that, the user is synchronized again. If a conflict arises again, then repeat until it is resolved.
Request:
{time: , data: []}
Response:
{time: , data: [], mergeData: []
}
Those localStorage stores up to 5 meters according to the specification, then perhaps you should not bother with a large amount of data. Although you can add a method to the server that will return the number of updates in bytes according to the last change time and tie additional logic. If the data is more than 5 mb, then you should use indexed db or web sql (hello ie 8 and 9), but browsers have an asynchronous API.

M
mark_ablov, 2012-12-04
@mark_ablov

I would not mess around with time, but would sculpt each version of the data in the form of a hash from the data.

1
1nd1go, 2012-12-04
@1nd1go

There is couchdb, which was specially sharpened for data synchronization (if the mobile phone, for example, is out of network coverage for a long time). There, each object is assigned an id. Further, the insertion goes according to optimistic locking - they suggested that if the id is the same, we write it down, if different we fall and invite the user to decide for himself.

O
OCTAGRAM, 2012-12-04
@OCTAGRAM

To avoid verbose logging, you can add TIMESTAMP columns to MySQL, they are automatically updated on INSERT and UPDATE.
DELETE, however, still have to be logged

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question