preventing race conditions with nodejs
<p>I'm writing an application using nodeJS 6.3.0 and aws DynamoDB.</p>

<p>the dynamodb holds statistics information that are added to dynamodb that are being called from 10 different function (10 different statistic measures). the interval is set to 10 seconds, which means that every 10 seconds, 10 calls to my function are being made to add all the relevant information.</p>

<p>the putItem function:</p>

<pre><code>function putItem(tableName,itemData,callback) {
var params = {
    TableName: tableName,
    Item: itemData
};
docClient.put(params, function(err, data) {
    if (err) {
        logger.error(params,"putItem failed in dynamodb");
        callback(err,null);
    } else {
        callback(null,data);
    }
});
</code></pre>

<p>now... I created a queue. </p>

<pre><code>var queue = require('./dynamoDbQueue').queue;
</code></pre>

<p>that implements a simple queue with fixed size that I took from <a href="http://www.bennadel.com/blog/2308-creating-a-fixed-length-queue-in-javascript-using-arrays.htm" rel="nofollow">http://www.bennadel.com/blog/2308-creating-a-fixed-length-queue-in-javascript-using-arrays.htm</a>.</p>

<p>the idea is that if there is a network problem.. lets say for a minute. i want all the events to be pushed to the queue and when the problem is resolved to send queue information to dynamodb and to free the queue.</p>

<p>so I modified my original function to the following code:</p>

<pre><code>function putItem(tableName,itemData,callback) {
var params = {
    TableName: tableName,
    Item: itemData
};
if (queue.length&gt;0) {
    queue.push(params);
    callback(null,null);
} else {
    docClient.put(params, function (err, data) {
        if (err) {
            queue.push(params);
            logger.error(params, "putItem failed in dynamodb");
            handleErroredQueue(); // imaginary function that i need to implement
            callback(err, null);
        } else {
            callback(null, data);
        }
    });
}
}
</code></pre>

<p>but since I have 10 insert functions that runs at the same second, there is a chance of race conditions. which means that ...</p>

<p>execute1 - one function validated that the queue is empty... and is about to execute <code>docClient.put()</code> function.</p>

<p>execute2 - and at the same time another function returned from <code>docClient.put()</code> with an error and as a result it adds to the queue it's first row.</p>

<p>execute1 - by the time that the first function calling <code>docClient.put()</code>, the problem has been resolved and it successfully inserted data to dynamodb, which leaves the queue with previous data that will be released in the next iteration.</p>

<p>so for example if i inserted 4 rows with ids <code>1,2,3,4</code>, the order of rows that will be inserted to dynamodb is <code>1,2,4,3</code>. </p>

<p>is there a way to resolve that ?</p>

<p>thanks!</p>