When graph databases win
Graph databases excel when the most important queries traverse relationships of variable depth. In a relational database, a 5-level deep friend-of-a-friend query requires 5 JOIN operations. In Neo4j, it is a single variable-length path query.
Core concepts
- Node — an entity (Person, Product, Account).
- Edge (Relationship) — a directed, typed connection between nodes (PURCHASED, KNOWS, TRANSFERRED_TO).
- Property — key-value attribute on a node or edge.
Cypher query example
-- Find all accounts within 3 hops of a flagged account
MATCH (flagged:Account {status: "flagged"})-[:TRANSFERRED_TO*1..3]->(suspicious:Account)
RETURN suspicious.id, suspicious.balance
ORDER BY suspicious.balance DESC;
Use cases
- Fraud detection — transaction networks and ring detection.
- Recommendation engines — collaborative filtering via relationship traversal.
- Knowledge graphs — entity relationships for RAG systems.
- Access control — complex permission hierarchies.
Limitations
Poor at bulk aggregations across all nodes (use a warehouse for that). Not a replacement for relational databases — use graph for the relationship-heavy queries and keep transactional data in Postgres.