Brett
Brett

Reputation: 1781

Neo4j modelling advice

I am developing a realtime chat for people selling/buying items I would like to know what is the most performant way to implement in Neo4j the storing of the messages in a room. (I can see 2 options)

1) add a messages array property to the Room node. 2) make the messages as nodes and have a "NEXT" relation between them.

What option would be the most performant for Neo4j ? Would just adding a value to the messages array would be easier to deal with for Neo4j ?

Upvotes: 1

Views: 76

Answers (1)

saljuama
saljuama

Reputation: 2956

From a performance point of view, the costs of the operations with Neo4j are as follows:

  • Find a node: O(1)
  • Transverse a relationship: O(1)

If you store every message in a single node, you only have to find one node, so the total cost of the operation is O(1) (constant)

But if you store every message in its own node with a relationship of NEXT between each message, to extract N messages, you need to find N nodes, so the cost becomes N * 2 * O(1) = O(N) (linear, and 2 because, 1 for finding, and 1 for transversing)

So with this in mind, seems that the having all the messages in a single node is better, but of course, the base cost of getting a node with a lot of information in it, might take a bit longer than getting a node that is smaller, so in order to make sure, I'd suggest to measure the time it takes to load a node with all of the messages in it, with different sizes to see how it scales, and then you can decide:

  • If it scales in a linear way => both will have similar performance
  • If doesnt scale linearly:
    • less than linear => one node with all the messages will be better
    • more than linear => a node per message will be better.

I suspect it will be less than linear, but assumptions aren't a good guide, so better check it.

If you are using Java 8 in your app, one way you can measure the operation time is using:

Instant start = Instant.now();
// operation
Instant end = Instant.now();
Duration.between(start,end);

Upvotes: 1

Related Questions