Counter of Neo4j and Php operations in a transaction

Well the script looks like this:

I am creating one node called counter node

. Its initial value is 0 and it increases when the user creates their account on my website.

So there are three operations:

Read value counter node


Do some logic in php. Here as +1 to previous node counter value
Write new node counter value

Now the problem is that two or more users arrive at exactly the same time and create a condition such that

Before the first user writes a new value for the node counter, it is read by the second user. So this will leave my "node count" value unstable.

I hope you understand what I mean.

Any solution?

I am using neo4j 1.9.5 and php

Php Jadell:

https://github.com/jadell/Neo4jPHP

I've heard about batch processing but not sure if this will work. If any solution please give me a short example.

Thanks Amit Aggarwal

+1


source to share


2 answers


You cannot do this with a pure REST API. I would try it with Cypher, maybe something like:

START n=node(123)
SET n.noOfUsers = n.noOfUsers + 1
RETURN n.noOfUsers

      



This should work in the latest Cypher version http://console.neo4j.org/?id=tnkldf

+1


source


Neo4j 2.0 has mandatory transactions. If you incremented your counter noOfUsers

in a transaction, I think this will help you with your concurrency problem.

Just a thought, but first a question: what is the purpose of the counter? Is it for assigning user IDs, or is it strictly informational? If the latter, do you have an accurate score? For example, if you want the total number of Twitter or Facebook users, would it matter if the account was disabled by multiple? If the counter does not need to be accurate (or accurate at a specific instance over time), you can run a batch process to return the number of custom nodes, for example:



MATCH n:User
return count(*)

      

It will also help you deal with remote nodes.

0


source







All Articles