Courses › System Design › Key Technologies

Neo4j & Graph Databases

Nodes and relationships, Cypher traversals, and when an edges table is enough.

Neo4j is the best-known graph database: it stores data as nodes and relationships and is optimized for traversing those relationships many hops deep. In interviews, graph databases come up for social networks, recommendations, fraud rings and access control — and the strongest answers know both when a graph database helps and when a relational table of edges is enough.

Data model

  • Nodes — entities, with labels and properties: (:User {id: 42, name: "Ada"}).
  • Relationships — directed, typed edges that can also carry properties: (:User)-[:FOLLOWS {since: 2024}]->(:User).
  • Index-free adjacency — each node stores direct pointers to its relationships, so following an edge is a pointer hop, not an index lookup. Traversal cost depends on how much of the graph you touch, not on the total graph size.

Querying with Cypher

MATCH (me:User {id: 42})-[:FRIEND]->(:User)-[:FRIEND]->(fof:User)
WHERE NOT (me)-[:FRIEND]->(fof) AND fof <> me
  AND (fof)-[:LIKES]->(:Genre {name: "Jazz"})
RETURN fof.id, count(*) AS mutual
ORDER BY mutual DESC LIMIT 10

The same query in SQL is a chain of self-joins on an edges table, which gets slower and harder to write with each extra hop.

Graph DB vs relational edges table

[!WARNING] Most "social graph" questions don't need a graph database Twitter-style follow graphs at scale are usually stored as sharded adjacency lists (follower_id → followee_ids and the reverse) in a key-value or wide-column store. A news feed only needs one hop. Reach for a graph database when the queries genuinely traverse many hops.

Scaling limits

  • Graphs are hard to partition: any shard boundary cuts edges, and cross-shard traversals are slow. Neo4j typically scales reads with replicas, and sharding (Fabric/composite databases) needs careful design.
  • Super-nodes — a celebrity with 50 million followers — make traversals through them expensive. Filter on relationship types and properties, or treat them specially.
  • Write throughput is modest compared to a key-value store.

When to use it

  • Fraud detection: rings of accounts sharing devices, cards or addresses
  • Recommendations based on paths ("people who bought X also bought Y," friends-of-friends)
  • Permission and org hierarchies with inheritance (e.g. Google Drive-style sharing)
  • Knowledge graphs and dependency graphs

When not to

  • Simple one-hop relationships — an edges table is enough
  • Massive write-heavy graphs that must shard horizontally
  • Aggregations over most of the data — use an analytics engine

[!INTERVIEW] Fraud ring detection "Accounts, devices, cards and addresses are nodes; 'used' and 'registered with' are relationships. When a new account signs up, a Cypher query looks for any existing flagged account within three hops through shared devices or cards. That's a few milliseconds in Neo4j, but a fan-out of self-joins in SQL."