Neo4j
Neo4j is a graph database where relationships are first-class citizens. Instead of joining tables to follow connections, you store nodes and the typed relationships between them and traverse them directly — which makes deeply connected queries dramatically faster and simpler to express.
It shines when the relationships are the value: social graphs, recommendations, permission hierarchies, fraud rings, and knowledge graphs. For tabular data with few relationships, a relational database is still the better tool.
TL;DR
- Relationships are first-class — traverse them instead of joining.
- Query with Cypher, a pattern-matching language for nodes and edges.
- Best for relationship-heavy domains (recommendations, fraud, permissions, knowledge graphs).
- Choose it for the model, not the novelty.
Quick Example
Cypher reads like ASCII-art of the graph — here, "products bought by people who bought what this user bought":
Core Concepts
- Node — an entity (a User, a Product).
- Relationship — a typed, directed edge between nodes (
(:User)-[:PURCHASED]->(:Product)). - Properties — key/value attributes on nodes and relationships.
- Labels — classify nodes (
:User,:Product). - Cypher — the declarative query language for matching graph patterns.
Common Uses
- Recommendations — "users who bought X also bought Y."
- Fraud detection & link analysis — find rings and suspicious paths.
- Permissions / access graphs — who can reach what, transitively.
- Knowledge graphs — richly interconnected entities.
Best Practices
- Model the traversals you run frequently, with relationship direction and types that match them.
- Index the properties you look nodes up by (entry points into the graph).
- Bound traversals — guard against queries that fan out across the whole graph.
- Plan imports: define indexes before bulk-loading large datasets.
Comparison: Graph vs Relational
Common Mistakes
Forcing a relational schema into a graph
Unbounded traversals
FAQ
When should I use a graph database?
When relationships and their traversal are central — recommendations, fraud/link analysis, permission hierarchies, and knowledge graphs. Queries that would need many self-joins or recursive CTEs in SQL often become short, fast Cypher patterns. For mostly tabular data, stick with relational.
What is Cypher?
Neo4j's declarative query language. It uses ASCII-art patterns — (node)-[:REL]->(node) — to match and return subgraphs, making relationship queries far more readable than the equivalent SQL joins.
How is a graph database different from relational joins?
Relational databases compute joins at query time, which gets expensive as you chain many or variable-depth relationships. Graph databases store relationships as direct pointers, so traversals are local and stay fast regardless of total dataset size.
When should I not use Neo4j?
When your data is mostly tabular with few relationships, or your workload is large-scale aggregations and reporting — a relational database or analytics store will outperform it. Use a graph database for the connections, not as a general-purpose store.
Related Topics
- Databases — Choosing a data store
- System Design — Modeling connected data
- PostgreSQL — Recursive CTEs for lighter graph needs
- SQL Fundamentals — The relational contrast