Skip to content
Tutorials Center
My learning

Topics

Lesson 3 of 30 ยท The Two Architectures

Neo4j and the property graph model

Learn the property graph model used by Neo4j: nodes, relationships, labels and properties, plus why index-free adjacency makes traversals cheap.

Cassandra is built around partitions spread over a ring. Neo4j is built around something much more familiar to a human: dots and the lines between them. A graph database stores your data as things (nodes) and the connections between those things (relationships), and it is designed so that walking from one thing to its neighbours is the cheapest operation it has.

This lesson explains the property graph model that Neo4j uses and how Neo4j stores a graph on disk so that following a connection does not need a search. Neo4j is a server product, so its queries and diagrams here are read-only. The JavaScript stand-ins build a small property graph from scratch so you can see nodes, labels, properties and relationships working together, and then compare a walk through direct references with a scan through a list of relationships. Those stand-ins are teaching models, not the real storage engine.

The four ingredients

A property graph has exactly four kinds of building block.

Nodes represent things: a person, a product, a city, an order. Relationships connect exactly two nodes, always with a direction (from a start node to an end node) and always with a type, written in capitals by convention, such as FOLLOWS or BOUGHT. Labels are tags on nodes that say what kind of thing they are; a node can have zero, one or several labels, for instance :Person and :Admin together. Properties are key-value pairs that can sit on both nodes and relationships, like name: "Ada" on a node or since: 2021 on a FOLLOWS relationship.

flowchart LR
    A(("Person<br/>name: Ada")) -- "FOLLOWS<br/>since: 2021" --> B(("Person<br/>name: Ben"))
    B -- "FOLLOWS<br/>since: 2022" --> C(("Person, Admin<br/>name: Cy"))
    A -- "LIKES" --> J(("Genre<br/>name: Jazz"))
    C -- "LIKES" --> J

A tiny property graph: circles are nodes with labels and properties, arrows are typed, directed relationships that can carry properties too.

Three rules are worth remembering because they shape how you model data later:

  1. A relationship always connects two nodes; it cannot dangle. You cannot delete a node that still has relationships without also removing them.
  2. A relationship has one direction when stored, but you can traverse it either way in a query.
  3. Relationships are not rows in a join table. They are real stored objects that can have their own properties.

In Cypher, Neo4j's query language, the same picture is written as ASCII art. This is read-only here; you will learn to write it in Cypher: MATCH, CREATE and patterns.

CYPHER
CREATE (ada:Person {name: 'Ada', born: 1990})
CREATE (ben:Person {name: 'Ben', born: 1988})
CREATE (ada)-[:FOLLOWS {since: 2021}]->(ben)

Parentheses are nodes, square brackets are relationships, and the arrow shows the direction.

Compare it with tables

In a relational database, a person is a row and "follows" is usually a separate table with two foreign keys. To find the people Ada follows you look up her row, then use a join to match foreign keys in the other table. If you have not seen how joins work, see SQL joins. The database finds matching rows using indexes, which costs an index lookup for every hop.

In a property graph, the "follows" connection is a stored relationship attached to Ada. Asking who Ada follows means looking at the relationships attached to Ada and reading their other ends. The work depends on how many neighbours Ada has, and not on how many people exist in the whole database.

Index-free adjacency

The technical name for this is index-free adjacency: each node holds direct references to its relationships, and each relationship holds direct references to the nodes at both ends. To go from a node to its neighbour you follow a pointer instead of consulting a global index.

In Neo4j's native graph storage, nodes and relationships are kept in fixed-size records. A node record points to its first relationship, and the relationships of one node are chained together, so reading all neighbours means walking that chain. Because the records have a fixed size, the position of a record on disk can be computed from its id. This is a simplified description, but it is the essential idea: traversal follows references and its cost depends on the local neighbourhood.

flowchart LR
    N["Node record<br/>id 7"] --> R1["Relationship record<br/>type FOLLOWS"]
    R1 --> R2["Relationship record<br/>type LIKES"]
    R1 --> M["Node record<br/>id 12"]
    R2 --> K["Node record<br/>id 31"]

A node points to its first relationship; relationships point onward to the next one in the chain and to the node at the other end.

"Native graph storage" means the database stores the graph this way from the bottom up, in contrast to a graph layer built on top of a relational or key-value store, where each hop would still be a lookup in a general-purpose table.

Index-free adjacency is not magic. Finding the starting node still needs a way to locate it, usually an index on a property such as name. Once you have a starting point, traversals fan out cheaply. You will look at indexes in Query performance: indexes, EXPLAIN and PROFILE.

Stand-in 1: a tiny property graph

The code below defines a Graph class with createNode and createRel. Each node keeps its outgoing and incoming relationship lists, just like the picture above. Then it prints every person with their outgoing relationships and answers "who likes Jazz?" in two ways: by checking every person, and by walking the incoming relationships of the Jazz node.

JS Build a property graph

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

No output.

Notice that Cy has two labels, that relationships have their own properties (since) and that the relationship LIKES has none. The last two lines show a key property of graphs: you can enter from either end of a relationship, and walking from Jazz backwards is just as direct as walking forwards from a person.

Stand-in 2: hops versus scanning

Now a bigger graph: 20,000 people, each following three others, so 60,000 relationships. The program finds the friends of friends of person 5 in two ways. The first uses a flat list of relationships and scans it (like a join without an index). The second uses direct references. It counts how many items each approach touched. This counts operations in this toy program; it does not measure any real database, and real relational databases use indexes that make the first approach far better than a raw scan. The point is the shape: one cost depends on the whole data set, the other only on the neighbourhood.

JS Scan a list or follow the references

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

No output.

Both find the same 5 friends of friends. The scan touched 240,000 items, the hop version 12. Double the number of people and the scan doubles; the hop version stays at 12 because person 5 still has the same three friends, each with three friends.

Where the model fits, and where it does not

Graphs are a strong fit when relationships are central and questions go several steps deep: social networks, recommendations, fraud rings, network and IT topology, knowledge graphs, access-rights hierarchies. They are a weaker fit for bulk number crunching over the whole data set (sums, averages and reports over millions of rows), where a column store or relational database is usually better.

Check your understanding

Quick check ยท 4 questions

of 4 answered You scored / 4 ยท perfect!

  1. 1. Which statement about relationships in a property graph is correct?

    A relationship always joins one start node and one end node, has a type such as FOLLOWS, and can carry properties like since.
  2. 2. What is a label?

    Labels such as Person or Admin group nodes, and one node may carry more than one.
  3. 3. What does index-free adjacency mean?

    Each node points to its relationships and each relationship to its end nodes, so traversal follows references. You may still need an index to find the starting node.
  4. 4. Why does a traversal's cost depend on the neighbourhood and not the whole database?

    A hop reads the relationships of the node you are standing on, so the amount of work grows with the neighbourhood you explore, not with the total data size.

Summary

  • A property graph has nodes, typed and directed relationships, labels on nodes, and properties on both nodes and relationships.
  • Cypher draws patterns with parentheses for nodes and square brackets for relationships.
  • Index-free adjacency stores direct references from nodes to relationships and from relationships to nodes, so a hop is a pointer follow.
  • You still need an index to find a starting node; traversals fan out from there.
  • The stand-ins model the structure and the cost shape only; the real Neo4j storage engine is far more sophisticated.

Next: CQL: keyspaces, tables and basic queries.