2

Im using a node.JS server for a test project im working on that involves a user connecting to the site via an app on their phone, and based on the URL used, will add that data to a doccument for access at a later time (not particualrly important as the accessing data half is done routinely by a single process). but the writing of data from multiple sources worries me in that, if two users were to connect at the same time, (for the sake of discussion, we'll say the exact same tick). what would happen to the file? would there be data corruption/loss? and if so, what would be the best way of preventing it/circumventing the situation (i.e. set one process to wait for the other). since the data will be added at specific locations in the document based on information in the data, im worried that there could be some overwriting/loss of data.

ultimately the question im asking is, if two users try and edit the same document at the same time, what happens and how can i prevent it?

P.S. i can't test this myself as i've not got the best setup for this but it can most assuredly happen regardless of me not being able to reproduce it.

EDIT: on top of all this, how could i prevent the automatic process from blocking the user input should they try and add data while data is being read?

EDIT 2: the file is stored as a single text document in the ./routes directory using the default Express setup.

PREEMPTIVE EDIT: (lol). everything is running on windows 8 64bit, if that makes a difference, which i imagine it could.

user2620255
  • 137
  • 1
  • 6
  • 2
    How is the data stored? A file in the filesystem? A variable in js? An entry in a database? Each of those have different mechanisms to prevent write collisions. – Bergi Apr 13 '15 at 02:53
  • Thanks! ill edit the post, i didn't even think of that >_ – user2620255 Apr 13 '15 at 03:35

1 Answers1

1

There are a number of different strategies for dealing with conflicting edits. Which to use depends upon a lot of things including the infrastructure you're using (e.g. type of data store), the frequency of conflict, the need or lack of need to save both edits, whether you can use a locking system, etc... In any case, here are some possible strategies:

  1. Lock Before Change. Before editing, require a user to "lock" a resource and when the resource is locked, do not allow any other user to try to edit it. You will have to invent some method of expiring a lock if the client never releases the lock (e.g. timeout).

  2. Lock During Update. Version check when retrieve/save. Reject edits that occurred during a conflict. When a client gets a version that they wish to edit, it also gets a version number. When submitting an edit, it also submits the version number that it thinks that it edited. If the version in the repository has since changed, then a conflicting edit may have occurred and the edit can be rejected.

  3. Lock During Update. Version check when retrieve/save. Merge edits that occurred during a conflict. When a client gets a version that they wish to edit, it also gets a version number. When submitting an edit, it also submits the version number that it thinks that it edited. If the version in the repository has since changed, then a conflicting edit may have occurred and the server can attempt to merge the edits or if a merge fails, then the edit can be rejected.

  4. Lock During Update. Version check when retrieve/save. Save both versions during a conflict. When a client gets a version that they wish to edit, it also gets a version number. When submitting an edit, it also submits the version number that it thinks that it edited. If the version in the repository has since changed, then a conflicting edit may have occurred and both edited versions can be saved. (This is what EverNote does if two clients try to save conflicting edits to the same note).


Reads and Writes to your data store have to made so they cannot be interrupted by other clients that wish to change the data (simultaneous reads are OK, but any write in the middle of any other read or write is not OK). There are also many possible strategies for this depending upon what type of data store you are using.

For file system reads/writes, you can use file open permissions to give you exclusive access to a file and then you can implement appropriate error handling so that requests for a resource that is currently not available can be retried when the resource becomes available.

This post might be useful: node.js readfile woes

There are also a number of file locking modules available for node.js. Here's one and another.

Community
  • 1
  • 1
jfriend00
  • 683,504
  • 96
  • 985
  • 979