System Design Cases
Neo4j / Graph DBs
Neo4j / Graph DBs concept page. Native graph storage (nodes/edges as fixed-size records, doubly-linked rel lists, index-free adjacency). Cypher pattern matching. Replication via Raft (Causal Cluster). Compared with Postgres recursive CTE and vector embeddings. Five scenarios: friends-of-friends 3-hop traversal vs SQL self-joins, weighted shortest path (bidirectional BFS / Dijkstra), fraud ring detection (cycles of length 3-5), sharding pain (cross-shard traversal latency variance), operational gotchas (unbounded variable-length paths). Two ADRs: Neo4j vs Postgres CTE vs vector embeddings, replicate-the-full-graph vs vertex partitioning.
Neo4j graph database: query plans, shortest paths, clustering, and sharding boundaries
Neo4j stores a property graph and executes Cypher plans over nodes, relationships, properties, and indexes. Graph modeling can make a traversal natural, but it does not turn every graph query into constant time or make equivalent relational modeling impossible. Endpoint cardinality, branching, predicates, path semantics, indexes, and memory determine work.
Current Neo4j has several scale surfaces with different contracts: clustered primaries/secondaries replicate one database for availability and read scaling; composite databases federate or shard separate graphs with limited cross-constituent transaction scope; newer Infinigraph property sharding is a separate licensed feature. They are not one generic automatic-sharding guarantee.
Модель и предпосылки
- The driver routes a transaction to the database writer or an eligible reader; bookmarks can enforce causal read-your-writes.
- Cypher planner chooses indexes and traversal/shortest-path operators from cardinality and predicates; inspect the actual plan.
- Relationships do not span composite constituent graphs; cross-graph modeling and single-constituent write scope are explicit.
Проверяемые утверждения
- C1. For a single estimated source-target pair, Cypher can use bidirectional BFS shortest-path operators; other cardinalities/predicates can select stateful, unidirectional, or exhaustive work.
- C2. A clustered database has one elected writer among primaries; writes require enough primary acknowledgements, while secondaries serve read scaling.
- C3. Bookmarks can request causal consistency/read-your-writes, but the application must carry them.
- C4. Composite databases query multiple constituent graphs but writes in one transaction are limited to a single graph; relationships cannot span graphs.
- C5. Indexes help find starting nodes and predicates, but traversal cost still depends on expanded relationships and query plan.
Исполняемые сценарии
Planned indexed traversal. An index narrows starting nodes, then relationship expansion and predicates determine remaining work.
Shortest-path operator choice. Bidirectional BFS is available for eligible single-pair plans; complex/all-target cases can use different or exhaustive operators.
Clustered database write. The driver routes a write to the elected writer, which commits with the primary replication protocol.
Bookmark causal read. A reader waits or routes until it can serve at least the bookmarked transaction.
Composite shard boundary. Reads can span constituents, while one transaction writes at most one constituent and relationships do not cross graphs.
Ошибки проектирования
- Не утверждайте, что
shortestPathвсегда Dijkstra или A*: unweighted Cypher operators и GDS algorithms имеют разные contracts. - Не начинайте traversal с неиндексированного широкого match и не скрывайте exhaustive fallback.
- Не путайте cluster replication, composite sharding и Infinigraph property sharding.
- Supernode с огромной degree остаётся hotspot; relationship type/direction, partitioning и query bound важны.
Границы гарантии
- Neo4j edition и availability surfaces различаются; текущая документация и лицензия проверяются перед выбором.
- Causal consistency не означает одну глобальную serializable transaction через composite constituents.
- Graph database не отменяет denormalization, cache, authorization или backup/restore design.