Skip to content
Tutorials Center
My learning

Topics

Lesson 20 of 30 ยท Cypher and Graph Modeling

Graph data modeling

Learn how to model data as a graph: when to use nodes, properties or relationships, how to name types and pick directions, and how to avoid supernodes.

Cypher is easy to learn compared with the harder skill behind it: deciding what in your domain becomes a node, what becomes a relationship and what stays a property. A good graph model makes the questions you care about short, readable patterns. A poor one gives you slow queries and awkward workarounds, even though the database is the same.

This lesson gives you a practical method: start from the questions, turn nouns into nodes and verbs into relationships, and then check the result against a few well-known traps, especially the supernode. Neo4j itself cannot run on this site, so the Cypher blocks are read-only. The JavaScript stand-ins are small counting programs: they compare a join-style lookup with a traversal and show why a node with a million relationships needs care. The counts come from the toy programs; they are not benchmarks of any real database.

Start from the questions

In the relational world you usually start from the data and normalize it (see normalization). In a graph model, start from the questions your application must answer. Write them down in plain language:

  1. Who does Ada follow, and who follows her?
  2. Which people have worked at the same company as Ben?
  3. Which courses did Cy complete in 2024?
  4. What are the shortest connections between two people?

Underline the nouns (person, company, course) and the verbs (follow, work at, complete). Nouns are candidates for nodes, verbs for relationships, and the words that describe a noun (name, year, title) for properties. Then draw the model on a whiteboard. If a question cannot be answered by walking a short pattern through your drawing, change the drawing before you create any data.

Node, property or relationship?

Use these rules of thumb.

Make it a node when the thing has an identity of its own, when other things connect to it, when you want to start a query from it, or when many entities share it. A company, a course, a city and a tag are all nodes.

Keep it a property when it only describes one node or relationship and you will mostly read it, filter on it a bit and never walk through it. A person's name, a post's text and a follow's since are properties.

Make it a relationship when it connects two things and the connection itself is the fact: (:Person)-[:FOLLOWS]->(:Person). A relationship can carry its own properties, such as since or weight.

The gray area is a property that you start to query a lot. "City" as a text property is fine until you want to find everyone in the same city, list cities, or connect cities to countries. At that point promote it to a node: (:Person)-[:LIVES_IN]->(:City).

Facts with more than two parts

A relationship connects exactly two nodes. What if a fact involves three things, or has a lot of detail of its own? Ada worked at Acme as an engineer from 2019 to 2022. Putting role, from and to on a WORKED_AT relationship works while that is all you need. As soon as the fact must connect to more things (a manager, a project, a location), turn it into an intermediate node.

flowchart LR
    P["Person<br/>Ada"] -- "HAS_JOB" --> E["Employment<br/>role: Engineer<br/>from: 2019, to: 2022"]
    E -- "AT" --> C["Company<br/>Acme"]
    E -- "IN" --> L["City<br/>Berlin"]
    E -- "REPORTED_TO" --> M["Person<br/>Ben"]

An intermediate node turns a many-part fact into something every participant can connect to.

An order with line items follows the same pattern: (:Customer)-[:PLACED]->(:Order)-[:CONTAINS {quantity: 2}]->(:Product). The quantity belongs to the relationship between this order and this product, not to either node alone.

Relationship types and direction

Name relationship types with specific verbs in capitals: FOLLOWS, WORKED_AT, PURCHASED. Avoid a vague catch-all such as RELATED_TO, because queries then have to filter on a property to learn what the connection means, and the query planner cannot use the type to narrow its search.

The opposite mistake is encoding data in the type name: VISITED_2023, VISITED_2024. Values that change belong in a property ({year: 2024}), and the type should stay a small, stable vocabulary.

Every relationship is stored with a direction, so pick the one that reads as a natural sentence: (ada)-[:FOLLOWS]->(ben) means "Ada follows Ben". You do not need to create a second relationship in the other direction to query backwards. These two patterns are both cheap, because traversal works from either end:

CYPHER
MATCH (:Person {name: 'Ben'})<-[:FOLLOWS]-(fan) RETURN fan.name   // who follows Ben
MATCH (:Person {name: 'Ben'})-[:FOLLOWS]->(x)   RETURN x.name     // whom Ben follows

For truly symmetric facts such as friendship, store one relationship in either direction and query without an arrow: (a)-[:FRIEND_OF]-(b).

Modeling is not a join table with arrows

Moving from SQL joins to a graph, it is tempting to copy the tables one to one. A foreign key becomes a relationship, a join table becomes a node. Sometimes that is right, but often the join table disappears entirely, because a many-to-many link is simply a relationship. What changes the most is the cost of following links. A join must find matching rows for each hop, usually through an index; a traversal follows the relationships already attached to the node you are standing on.

The first stand-in builds a follow graph of 5,000 people in two forms: a relational-style table of (follower, followee) pairs and a graph of direct references. It finds all people exactly three hops from person 0 with three strategies: a table scan for each hop, an indexed lookup for each hop (a sorted table searched with binary search, counted per comparison), and a traversal. It prints how many units of work each used. These are counts of operations in this toy program, which is the point: how the cost grows, not real timings.

JS Join cost versus traversal

Press Run (or Ctrl + Enter) to see the console output here.

No output.

All three find the same 10 people. The scan needed 45,000 units of work (15,000 rows, three times), the indexed version 170 and the traversal 30. The scan's work is the table size multiplied by the number of hops, so it grows with the whole data set. The indexed join pays a logarithmic search per node it visits, plus the rows it reads. The traversal only reads the relationships of the nodes it reaches. Real relational databases are far better than a raw scan and are perfectly adequate for shallow joins; the gap matters when queries go deep, or when a model forces many hops.

Beware the supernode

A supernode is a node with a huge number of relationships: a Country node with 50 million residents, a Male node, a celebrity with 80 million followers. Walking through such a node means reading every relationship attached to it, and most of those relationships are irrelevant to your question. It also becomes a hot spot when many transactions update the same node at once.

Common fixes:

  • Use a property instead of a node when you never traverse through the shared value. A country property filtered with an index is cheaper than a hub.
  • Add intermediate levels: Country -> Region -> City -> Person, so each node has a manageable number of neighbours.
  • Split by time or category: instead of one Day hub, use Year -> Month -> Day nodes, or bucket nodes such as FollowerBucket with a date range.
  • Filter early with specific relationship types and properties so the planner can avoid unrelated edges.

The second stand-in compares the work needed to list one city's residents who also follow a given person, with the residents attached to a small City node versus a giant Country node.

JS The cost of a supernode

Press Run (or Ctrl + Enter) to see the console output here.

No output.

The same 29 people turn up either way, but the answer needs 60,000 relationship reads through the hub and 200 through the city. In a real database the planner may do better with a good index, but the principle holds: model the structure so that the common traversals start at small nodes.

A modeling checklist

  • Have you written down the real questions and checked that each one is a short pattern?
  • Are nodes things with identity, and properties just descriptions?
  • Are relationship types specific verbs, with changing values stored as properties?
  • Do any nodes risk millions of relationships? Can you add a level or bucket?
  • Did you avoid storing the same relationship in both directions?

Once the model is right, queries get fast with the right indexes, which is the topic of Query performance: indexes, EXPLAIN and PROFILE.

Check your understanding

Quick check ยท 4 questions

of 4 answered You scored / 4 ยท perfect!

  1. 1. When should a property be promoted to a node?

    A value that has its own identity or that you traverse through, such as a city, is better as a node.
  2. 2. What is the best way to model Ada working at Acme with a role, dates, a location and a manager?

    A relationship joins only two nodes. A fact with several participants becomes a node so that each participant can connect to it.
  3. 3. Which is a problem with a RELATED_TO relationship used for everything?

    Specific types such as FOLLOWS let queries and the planner narrow the search without inspecting properties.
  4. 4. What is a supernode and why is it a problem?

    Hubs with millions of relationships make traversals slow and become write hot spots; add levels, buckets or properties instead.

Summary

  • Model from the questions: nouns become nodes, verbs become relationships, descriptions become properties.
  • Promote a property to a node when it has identity or when you traverse through it.
  • Use an intermediate node for facts with more than two participants or with rich detail.
  • Use specific relationship types, store changing values as properties and do not duplicate relationships in both directions.
  • Traversal cost depends on the neighbourhood, so keep nodes from becoming supernodes by adding levels, buckets or properties.
  • The stand-ins count operations in toy programs; they show growth patterns, not real database timings.

Next: Query performance: indexes, EXPLAIN and PROFILE.