Neo4j (Graph)
Nodes, relationships, Cypher, graph traversals.
Theory
Neo4j is a native graph database where data is stored as a property graph: nodes (entities), relationships (directed, typed connections between nodes), and properties (key-value pairs on both nodes and relationships). Unlike SQL where relationships are foreign keys computed via JOIN at query time, Neo4j stores relationship pointers directly on each node — traversal is O(1) per hop regardless of graph size.
Cypher is Neo4j's query language. MATCH (u:User)-[:FOLLOWS]->(f:User) WHERE u.name = 'Alice' RETURN f finds all users Alice follows. Patterns use ASCII art arrows: (a)-[:REL]->(b). CREATE adds nodes and relationships; MERGE finds or creates (upsert). WITH pipelines query stages; UNWIND expands lists into rows. Cypher reads naturally — the pattern in the query looks like the shape of data you want.
Use cases where graph databases excel: social networks (friends-of-friends queries that require 3+ JOIN levels in SQL are 1 MATCH in Cypher), fraud detection (detecting ring patterns — multiple accounts sharing the same phone/address/device), recommendation engines (collaborative filtering via shared connections), knowledge graphs, and network topology (IT infrastructure dependencies).
ACID transactions: Neo4j is fully ACID — writes are atomic, consistent, isolated, and durable. Multi-statement Cypher runs in an implicit transaction. Explicit transactions via the driver allow multi-request transactions with BEGIN/COMMIT/ROLLBACK. Neo4j uses a write-ahead log (WAL) for durability and MVCC for concurrent reads without blocking writers.
Indexes and constraints: CREATE INDEX ON :User(email) creates a B-tree index on the email property of User nodes. CREATE CONSTRAINT ON (u:User) ASSERT u.email IS UNIQUE creates a uniqueness constraint (implicitly creates an index). Full-text indexes (CREATE FULLTEXT INDEX) enable keyword search across node properties using Lucene under the hood.
Neo4j clustering: Neo4j Enterprise supports Causal Clustering — a Raft-based consensus cluster with designated read replicas. Reads scale horizontally across replicas; writes go to the leader and replicate to followers. Bookmarks ensure causal consistency — a client waits for a replica to catch up to a specific transaction before reading from it.
Graph algorithms: Neo4j Graph Data Science (GDS) library runs algorithms directly on the graph: PageRank (node importance), Louvain (community detection), shortest path (Dijkstra, A*), node similarity, link prediction. GDS projects a subgraph into memory for fast iterative algorithm execution. Results can be written back as properties or relationships, then queried with Cypher.
Architecture Diagram
Users / clients
|
Neo4j (Graph)
|
Core services
|
Data + observabilityExamples
# Neo4j (Graph)
# Nodes, relationships, Cypher, graph traversals.
# Validate in staging before production rollout.
Key Concepts
Interview Questions
What problem does Neo4j (Graph) solve?
It addresses the core use case described in production architecture — map features to reliability, scale, or velocity outcomes.
Key components of Neo4j (Graph)?
Identify inputs, outputs, control plane, data plane, and failure domains — interviewers want structured decomposition.
Common production pitfalls?
Misconfiguration, missing observability, no rollback path, and scaling bottlenecks under peak load.
How do you test changes safely?
Staging parity, canary/gradual rollout, automated health checks, and documented rollback.
Metrics to prove success?
Error rate, latency percentiles, throughput, cost, and toil reduction — pick one primary SLO.
Beginner vs advanced concern?
Beginners focus on setup; advanced teams focus on blast radius, security boundaries, and operability at 10× scale.
Best Practices
- Treat Neo4j (Graph) config as code with review and CI validation.
- Define SLOs and dashboards before production cutover.
- Document rollback and ownership for on-call.
- Use least privilege for credentials.
Common Mistakes
- Adopting Neo4j (Graph) without measurable success criteria.
- No staging environment mirroring production constraints.
- Missing rollback path during incidents.
- Undocumented on-call expectations.
Cheat Sheet
Practical Exercises
Stand up Neo4j (Graph) locally or in free tier; document commands and failure recovery.
Introduce misconfiguration; practice detection and rollback under time limit.