Graph Databases Basics (Neo4j & Cypher)
Model highly connected data with nodes/relationships and query paths using Cypher.
In plain words
Stable node labels and relationships. Next, indexed starting property rather than full graph scan. Finally, aggregate evidence and avoid oversized path JSON.
Think of it like…
A graph is a road map stored with the roads attached to intersections, making 'find three turns from this station' cheaper than assembling every road junction on demand.
Where you’ve seen it
MATCH (a:Account {id:$id})-[:SENT_TO*1..3]->(b:Account) RETURN DISTINCT b.id; an index on :Account(id) anchors the search and *1..3 caps path expansion.
Words you’ll hear
- Neo4j
- A property graph database optimized for labeled nodes, typed relationships, indexes, and transactional online graph workloads.
- Cypher
- Neo4j's declarative pattern-matching language, often read as visual node and relationship shapes.
- Graph
- A sequence of nodes connected by relationships whose variable length must be bounded for predictable work.
- Operational boundary
- Labeling a graph as magical avoids index and cardinality work; an unconstrained variable-length path can explore a combinatorial network and return huge duplicate result sets. Model only high-connectivity domains, index anchor properties, bound traversal depth and relationship types, profile expanded rows, and return projection-specific paths rather than whole subgraphs.
Visual diagram
Theory & deep dive
Neo4j: the working mechanism
A graph database stores entities as nodes and facts as relationship records, so path and neighborhood traversal follows stored edges instead of repeatedly joining relational association tables.
Stable node labels and relationships. Indexed starting property rather than full graph scan, then constrained types, direction, depth, and uniqueness; this exposes the state governed by Graph.
MATCH (a:Account {id:$id})-[:SENT_TO*1..3]->(b:Account) RETURN DISTINCT b.id; an index on :Account(id) anchors the search and *1..3 caps path expansion.
Graph Databases Basics: failure mode and production response
Labeling a graph as magical avoids index and cardinality work; an unconstrained variable-length path can explore a combinatorial network and return huge duplicate result sets.
Model only high-connectivity domains, index anchor properties, bound traversal depth and relationship types, profile expanded rows, and return projection-specific paths rather than whole subgraphs. Track Anchor cardinality as "a selective start node before traversal", and make Hop bound visible to the dataset owner.
How it works, step by step
Step 1 of 41. Model entities
Stable node labels and relationships. A graph database stores entities as nodes and facts as relationship records, so path and neighborhood traversal follows stored edges instead of repeatedly joining relational association tables.
- Anchor cardinality
- a selective start node before traversal
- Cypher
- Neo4j's declarative pattern-matching language, often read as visual node and relationship shapes.
When to use it & trade-offs
What it is
A graph database stores entities as nodes and facts as relationship records, so path and neighborhood traversal follows stored edges instead of repeatedly joining relational association tables. MATCH (a:Account {id:$id})-[:SENT_TO*1..3]->(b:Account) RETURN DISTINCT b.id; an index on :Account(id) anchors the search and *1..3 caps path expansion.
Why it matters: Labeling a graph as magical avoids index and cardinality work; an unconstrained variable-length path can explore a combinatorial network and return huge duplicate result sets. Model only high-connectivity domains, index anchor properties, bound traversal depth and relationship types, profile expanded rows, and return projection-specific paths rather than whole subgraphs.
When to reach for it
- Model highly connected data with nodes/relationships and query paths using Cypher.
- Choose Neo4j because it provides this capability: A property graph database optimized for labeled nodes, typed relationships, indexes, and transactional online graph workloads.
- Keep evidence for Anchor cardinality at "a selective start node before traversal" so the last production stage-Project - aggregate evidence and avoid oversized path JSON-remains testable.
When to avoid or be careful
- Labeling a graph as magical avoids index and cardinality work; an unconstrained variable-length path can explore a combinatorial network and return huge duplicate result sets.
- Do not conflate Cypher with Neo4j. The second concept contributes different behavior: Neo4j's declarative pattern-matching language, often read as visual node and relationship shapes.
- Release only after this guardrail is in place: Model only high-connectivity domains, index anchor properties, bound traversal depth and relationship types, profile expanded rows, and return projection-specific paths rather than whole subgraphs.
Neo4j / Cypher / Graph - roles, limits, and operating cost
Strengths: A property graph database optimized for labeled nodes, typed relationships, indexes, and transactional online graph workloads. A graph database stores entities as nodes and facts as relationship records, so path and neighborhood traversal follows stored edges instead of repeatedly joining relational association tables.
Watch out: Labeling a graph as magical avoids index and cardinality work; an unconstrained variable-length path can explore a combinatorial network and return huge duplicate result sets.
Strengths: Neo4j's declarative pattern-matching language, often read as visual node and relationship shapes. In this workflow it supports: Traverse - constrained types, direction, depth, and uniqueness.
Watch out: The operating cost is real: explicit maximum for variable-length paths.
Strengths: A sequence of nodes connected by relationships whose variable length must be bounded for predictable work. Its output can be checked with Expanded rows.
Watch out: Operating guardrail: Model only high-connectivity domains, index anchor properties, bound traversal depth and relationship types, profile expanded rows, and return projection-specific paths rather than whole subgraphs.
Interview signals & numbers to know
When this topic comes up
- “Where is the failure boundary for Neo4j, and how would Anchor cardinality reveal it?”
- “Why is Cypher distinct from this behavior: a sequence of nodes connected by relationships whose variable length must be bounded for predictable work?”
- Production clue: Labeling a graph as magical avoids index and cardinality work; an unconstrained variable-length path can explore a combinatorial network and return huge duplicate result sets.
- Design probe: Model only high-connectivity domains, index anchor properties, bound traversal depth and relationship types, profile expanded rows, and return projection-specific paths rather than whole subgraphs.
Benchmarks & rules of thumb
- Anchor cardinality
- a selective start node before traversal
- Hop bound
- explicit maximum for variable-length paths
- Expanded rows
- track query plan operator growth, not only final results
Common mistakes & follow-ups
Common interview mistakes
- Labeling a graph as magical avoids index and cardinality work; an unconstrained variable-length path can explore a combinatorial network and return huge duplicate result sets.
- Reporting success from a job exit code without checking Anchor cardinality and Hop bound.
- Treating this example as complete without replay, rollback, or access boundaries:
MATCH (a:Account {id:$id})-[:SENT_TO*1..3]->(b:Account) RETURN DISTINCT b.id; an index on:Account(id)anchors the search and*1..3caps path expansion.
Likely interviewer follow-ups
- How would you prove this contract instead of only checking that the command ran: A property graph database optimized for labeled nodes, typed relationships, indexes, and transactional online graph workloads.
- What does the recovery plan do when this occurs? Labeling a graph as magical avoids index and cardinality work; an unconstrained variable-length path can explore a combinatorial network and return huge duplicate result sets.
- When is Neo4j insufficient for Hop bound, and what does adding Cypher cost in Anchor cardinality?
Knowledge check
Put it into practice
Practice related interview Q&A
Apply Graph Databases Basics (Neo4j & Cypher) with data engineer interview Q&A and hands-on practice.