Guide / AI engineering Fundamentals
18. Graph databases and GraphRAG
18.1 Graph databases#
- Model: nodes (entities: Person, Product, Order) and edges (relationships: BOUGHT, FOLLOWS, SUPPLIES), both with properties. Labeled property graph (Neo4j, Neptune, Memgraph) vs RDF triples (subject–predicate–object; SPARQL; used in knowledge graphs, ontologies).
- Query languages: Cypher / openCypher / ISO GQL (
MATCH (u:User)-[:FRIEND]->(f)-[:BOUGHT]->(p) RETURN p), Gremlin (traversal API), SPARQL. - Why fast for relationships: index-free adjacency — each node points directly to its neighbours, so a hop costs the same regardless of total graph size. In SQL, each hop is a join whose cost grows with table size; 4–5 hops become very slow.
- Good for: fraud rings (shared devices/cards/addresses), recommendations (users who bought X also bought Y), social graphs, network/IT dependency maps, supply chains, access control graphs (Google Zanzibar-style), knowledge graphs.
- Weak for: bulk aggregations over all data (use OLAP), simple CRUD, very high write throughput, horizontal sharding (cutting a graph across machines makes traversals cross the network; supernodes with millions of edges are hot spots).
- Alternatives: Postgres recursive CTEs or Apache AGE handle shallow graphs; a separate graph DB pays off when multi-hop queries are central.
18.2 What GraphRAG is#
Plain vector RAG retrieves isolated chunks by similarity. It struggles with:
- Multi-hop questions: “Which suppliers of products recalled in 2026 also supply our top-selling brand?” needs connecting facts spread over many documents.
- Global / summary questions: “What are the main themes across 10,000 support tickets?” — no single chunk contains the answer.
GraphRAG builds a knowledge graph from the corpus and uses it in retrieval.
Indexing:
- Chunk the documents.
- Use an LLM (or NER/relation extraction models) to extract entities and relationships (+ claims) from each chunk.
- Entity resolution: merge duplicates (“Nykaa”, “Nykaa E-Retail”, “FSN E-Commerce”).
- Store in a graph DB, linking each entity/edge back to its source chunks.
- (Microsoft GraphRAG approach) Run community detection (Leiden algorithm) to cluster related entities, and have the LLM write community summaries at several levels.
Querying:
- Local search: find entities in the question (via vector search over entity descriptions), expand their neighbourhood k hops, collect related chunks and relationships as context. Good for specific multi-hop questions.
- Global search: map-reduce over community summaries to answer corpus-wide questions.
- Hybrid: vector retrieval for chunks + graph expansion for connected facts; or text-to-Cypher where the LLM writes a graph query.
18.3 GraphRAG trade-offs#
| Pros | Cons |
|---|---|
| Answers multi-hop and global questions plain RAG can’t | Indexing is expensive: LLM calls over every chunk (can be 10–100× vector indexing cost) |
| Explainable: you can show the path of facts | Extraction errors and missed entities propagate |
| Structured facts reduce hallucination for relationship questions | Entity resolution is hard; graph quality needs maintenance |
| Combines well with existing enterprise knowledge graphs | Incremental updates are harder (community summaries go stale) |
| Slower queries; more infrastructure (graph DB + vector DB) |
When to use it: questions are about relationships across many documents, a domain with clear entity types (legal, medical, supply chain, finance, compliance), or a need for corpus-level summaries. When not: FAQ-style lookups, small corpora, fast-changing data, or tight budgets — hybrid vector RAG with reranking is cheaper and usually good enough. Light variants (LightRAG, entity-linked chunks, graph built only for key entity types) cut the cost.
18.4 Structured data: text-to-SQL and tools#
Many “RAG” questions are actually database questions (counts, sums, filters). Route them to text-to-SQL (LLM writes SQL against a described schema, executed read-only with row limits, on a replica, with an allowlist of tables and permission checks) or to typed API tools. Validate generated SQL, never let it write, and show the query for transparency.