title: Are neo4j's cypher transactions broken?
body: <p><em>TL;DR: I am either losing my mind, or neo4j's transactions are slightly broken. It looks like uncommitted nodes are available outside of committed transactions, with missing properties - or something equally weird.</em></p>
  
  <p>Our node.js app uses neo4j. A part of it has to generate unique IDs. We have the following cypher query that is meant to locate the last <code>:Id</code>-type node and attempt to commit a new <code>:Id</code> node with <code>last_uuid+1</code>. </p>
  
  <pre><code>MATCH (i:Id) WITH i ORDER BY i.uuid DESC LIMIT 1  #with it like a sub-return, will "run" the rest with the last i at read-time
  CREATE (n:Id {label:"Test"}) 
  SET n.uuid = i.uuid + 1
  RETURN n
  </code></pre>
  
  <p>There is also a constraint:</p>
  
  <pre><code>neo4j-sh (?)$ schema
  Indexes
    ON :Id(uuid) ONLINE (for uniqueness constraint) 
  
  Constraints
    ON (id:Id) ASSERT id.uuid IS UNIQUE
  </code></pre>
  
  <p>And the DB is initialized with an <code>(:Id{uuid:1})</code> to kick-start this joy.</p>
  
  <p>The app code basically retries the above query until it is successful. If two or more Id creation requests hit at the same time, only one of them will go through, the rest of them would fail and be retried by the app code.</p>
  
  <p>This worked, until we tried it in parallel.</p>
  
  <p>The code started returning data without a uuid. After a lot of investigation, it turns out that the write part of the query (CREATE...) is somehow receiving an :Id from MATCH which has no .uuid (or other) properties. This shouldn't be possible. This is the only code that operates on those nodes.</p>
  
  <p>The strangest (perhaps) thing, is that if I save <code>i</code>'s <code>nodeid</code> in order to locate that node in the DB, it actually exists <em>and</em> has a .uuid property.</p>
  
  <p>In order to isolate this behaviour I wrote a PoC: <a href="https://github.com/tasinet/neo4j-transaction-test">neo4j-transaction-test</a> It should be really simple to run with nodejs.</p>
  
  <p>It is basically a tad bit more than the above code - tries to create the Id, setting <code>prev_label</code>, <code>prev_nodeid</code>, and <code>prev_uuid</code> to the previous Node's (i) values. It runs the query for every GET request it receives on localhost:9339 and outputs:</p>
  
  <pre><code> &gt; node server.js 
   * 1412125626667 Listening on 9339
   Req Id | Datetime | -&gt; $uuid $nodeid
   1 1412125631677 'GET'       # When it first receives the GET request
   1 1412125631710 '-&gt;' 9 60   # When neo4j returns; numbers are $uuid $node_id)
  </code></pre>
  
  <p>when things start getting concurrent, queries may fail:</p>
  
  <pre><code>3 1412125777096 '(retry) (0)' 'Node 64 already exists with label Id and property "uuid"=[13]'
  4 1412125777098 '(retry) (0)' 'Node 64 already exists with label Id and property "uuid"=[13]'
  de[]
  </code></pre>
  
  <p>which is to be expected, and they are retried. If we "slam" the server with a couple of reqs per second (<code>ab -n 1000 -c 10 http://localhost:9339/</code>), we will eventually see:</p>
  
  <pre><code>...
  59 1412127103011 'GET'
  23 1412127103024 'ERROR - EMPTY UUID' '{"this_nodeid":22,"prev_nodeid":20,"label":"Test"}'
  
  Error: Empty UUID received
  </code></pre>
  
  <p>(and by eventually, I mean almost instantly) A node comes back, without uuid, prev_uuid or prev_label. this_nodeid and prev_nodeid refer to neo4j's internal id. If we look these up, starting with the previous (<code>i</code>) Id node (by nodeid - 20):</p>
  
  <pre><code>neo4j-sh (?)$ match (i) where id(i)=20 return i;
  +--------------------------------------------------------------------------------------------+
  | i                                                                                          |
  +--------------------------------------------------------------------------------------------+
  | Node[20]{uuid:10,label:"Test",prev_label:"Test",prev_uuid:9,prev_nodeid:17,this_nodeid:20} |
  +--------------------------------------------------------------------------------------------+
  1 row
  19 ms
  </code></pre>
  
  <p>It is exactly as it should be. <code>.uuid</code> and all. The new one is indeed created just as it is returned above:</p>
  
  <pre><code>neo4j-sh (?)$ match (i) where id(i)=22 return i;
  +------------------------------------------------------+
  | i                                                    |
  +------------------------------------------------------+
  | Node[22]{label:"Test",prev_nodeid:20,this_nodeid:22} |
  +------------------------------------------------------+
  1 row
  17 ms
  </code></pre>
  
  <p>No prev_label or prev_uuid. How is this possible? What am I missing? Is an incomplete :Id node leaking into my query?</p>
  
  <p>I've tried restarting, wiping the data dir, restarting after wiping the data dir, scavenged the logs (nothing interesting, or even boring but at the right time - when the above is happening). I am now at the point where I am questioning my understanding of how this is supposed to work.</p>
  
  <p>This is on 12.04 with neo4j 2.1.1. <a href="https://gist.github.com/tasinet/5733fea44281cdc42b72">More Version Info</a> and <a href="https://gist.github.com/tasinet/e02829a0f228603942c3">Neo4j startup/shutdown logs</a>.</p>
  
  <p><strong>I am aware that this is not an optimal way to create UUIDs. This question is about understanding how these results are possible if neo4j's transactions work as expected.</strong></p>