1. What is a Graph Database
A database optimized for storing and querying relationships between entities.
Node ββRelationshipββ> Node
Core building blocks
Node
βββ ID
βββ Labels / Type
βββ Properties
Relationship
βββ Type
βββ Direction
βββ Properties
Example:
(Alice) ββFOLLOWSββ> (Bob)
β β
β βββWORKS_ATββ> (Google)
β
βββBOUGHTββ> (iPhone)
Key idea: Relationships are first-class data, not just foreign keys.
2. When should I use a Graph DB?
Use it when relationships are central to the queries.
Common System Design use cases
| Use case | Example traversal |
|---|---|
| Social Network | User β Friends β Friends |
| Recommendation | User β Product β Category β Product |
| Fraud Detection | Account β Transaction β Account |
| Knowledge Graph | Person β Company β Product |
| Dependency Graph | Service β Dependency β Service |
| Network/Topology | Router β Link β Router |
| Access Control | User β Group β Role β Permission |
If the problem is primarily about βhow are things connected?β, consider a Graph DB.
3. Why not SQL?
SQL can represent a graph via Users and Relationships tables, e.g. Users(id, name) and Friendships(user_id, friend_id).
A simple query like βWho are Aliceβs friends?β is fine. But:
Alice β Friends β Friends of Friends β Friends of Friends of Friendsrequires repeated JOINs / lookups. As traversal gets deeper:
1-hop β 2-hop β 3-hop β 4-hop β ...the query becomes increasingly expensive and complex.
Graph DB
Instead:
Alice β Edge β Bob β Edge β CharlieThe database is designed to traverse relationships directly.
4. Why is Graph DB fast?
Index-Free Adjacency β
This is the most important concept.
In a native graph database, nodes maintain references to their relationships, and relationships reference connected nodes. Conceptually:
Alice --pointer--> FOLLOWS --pointer--> BobSo traversal is approximately:
Node β Edge β Node β Edge β Noderather than:
Node β Index lookup β Relationship table β Index lookup β NodeKey statement
Index-free adjacency means traversing from one node to a connected node doesnβt require an index lookup; the relationship itself provides the path to the next node.
This is why native graph databases are particularly good at deep relationship traversal.
5. Complexity intuition
Donβt say βGraph traversal is O(1).β Thatβs misleading.
Instead:
- Following one relationship:
β O(1) - Traversing K relationships:
β O(K)
The important property is:
Traversal cost depends primarily on the portion of the graph being traversed, rather than the total number of nodes in the database.
Example: with 1 billion nodes, a query like:
Alice β friends β friendsmay only need to visit a relatively small neighborhood around Alice. This is called localized traversal.
6. Native vs Non-Native Graph DB
Non-native
Graph capabilities are built on top of another storage system:
Graph API/Query β Underlying DB β Tables/DocumentsThe graph is essentially an abstraction over another database.
Native
The storage engine itself is designed around graph structures:
Graph Query β Graph Processing β Native Graph StorageExample: Neo4j is a well-known native graph database.
Interview takeaway
Native graph databases can optimize both storage and traversal for graph workloads.
7. Graph DB vs SQL vs NoSQL
| SQL | NoSQL | Graph DB | |
|---|---|---|---|
| Primary strength | Structured data + transactions | Scale/flexible data models | Relationships |
| Data model | Tables | Documents/KV/Columns | Nodes + Edges |
| Relationships | Foreign keys + JOINs | Usually application-managed | First-class |
| Deep traversal | β Expensive | β Usually awkward | β Excellent |
| Schema | Usually structured | Flexible | Flexible |
| Transactions | β Strong | Depends | Depends |
| Best for | Orders, payments, users | Large-scale KV/doc workloads | Social/fraud/recommendation |
Important: This doesnβt mean Graph DB replaces SQL/NoSQL. Choose based on the access pattern.
8. Graph Data Modeling
Think in terms of: Entities β Nodes, Relationships β Edges, Attributes β Properties.
Example:
(User) ββPURCHASEDββ> (Product) ββBELONGS_TOββ> (Category)Properties:
User: id, namePURCHASED: timestamp, quantityProduct: id, price
Relationship properties are important
Unlike a simple foreign key Alice β Bob, the edge itself can contain properties, e.g. FOLLOWED { since, source }.
So:
Alice ββFOLLOWS {since: 2024}ββ> Bob9. Direction matters
Relationships can be directed:
Alice ββFOLLOWSββ> BobThis does not necessarily mean:
Bob ββFOLLOWSββ> AliceFor social networks, this distinction is important:
Alice ββFOLLOWSββ> Bob
Charlie ββFOLLOWSββ> BobNow we can ask βWho follows Bob?β β thatβs a reverse traversal. Native graph systems are designed to support these relationship traversals efficiently.
10. Flexible Schema
Graph databases generally allow the graph model to evolve easily.
Initially:
User ββFOLLOWSββ> UserLater:
User ββFOLLOWSββ> User
User ββWORKS_ATββ> Company
User ββOWNSββ> Car
User ββPURCHASEDββ> ProductYou donβt necessarily need to redesign a large collection of relational tables whenever a new relationship type appears. This flexibility is particularly useful for evolving domains and knowledge graphs.
11. Graph DB is NOT always better
Donβt use Graph DB just because:
βWe have relationships.β Almost every application has relationships.
Use it when:
Relationship traversal is a dominant access pattern.
For example:
Banking transaction
βGet account balanceβ / βGet transaction by IDβ β SQL is usually a better fit.
Fraud detection
Account β Device β Account β Transaction β Account β DeviceGraph DB becomes much more attractive.
12. Main Trade-offs
β Advantages
- Excellent for relationship-heavy workloads
- Fast multi-hop traversal
- Index-free adjacency in native implementations
- Relationships are first-class
- Flexible/evolving data model
- Natural representation of connected data
β Disadvantages
- Not ideal for every workload
- Distributed graph partitioning can be difficult
- Cross-partition traversals are expensive
- Distributed transactions can require coordination
- Operational complexity can be higher
- Ecosystem/query patterns may be less familiar than SQL
13. Distributed Graph DB β
This is a good deep-dive interview topic.
Imagine two partitions:
Partition 1: A ββ B
Partition 2: C ββ D
Cross-link: B ββ CIf traversal crosses partitions:
A β B β C (B β C hop is a network call)So traversal isnβt just Node β Edge β Node, it can become:
Node β Edge β NETWORK β Another partition β NodeNetwork latency becomes important.
Key principle
Graph partitioning is difficult because highly connected nodes can create cross-partition traversals.
14. ACID / Transactions
Graph databases can support transactions.
Suppose:
A ββ> B ββ> Cand one transaction must update A + B + C. We want ALL SUCCESS or ALL ROLLBACK.
If A, B and C are on different machines, this becomes a distributed transaction and may require coordination such as Two-Phase Commit (2PC). This introduces latency and complexity.
15. Common Graph DB Technologies
Neo4j
Popular native graph database. Query language: Cypher.
Example:
MATCH (u:User)-[:FOLLOWS]->(friend)
WHERE u.name = "Alice"
RETURN friendOther options
- Amazon Neptune
- JanusGraph
- TigerGraph
- ArangoDB
16. How to answer in an interview
Interviewer:
βWhy would you choose a graph database?β
Answer:
βIβd choose a graph database when relationships and multi-hop traversal are core to the applicationβs access patterns. for example social graphs, recommendation systems, fraud detection, or knowledge graphs. Native graph databases provide index-free adjacency, allowing the database to traverse from node to node through stored relationships without repeatedly performing expensive joins or index lookups.β
17. 30-second mental model
Graph Database = Nodes (Entities) + Edges (Relationships)
β Native Graph Storage
β Index-Free Adjacency
β Fast Multi-Hop Queries
β (Social Network, Fraud Detection, Recommendation Engine)β The 5 things Iβd memorize
1. What?
Nodes + relationships + properties.
2. When?
Relationship-heavy, multi-hop traversal.
3. Why fast?
Index-free adjacency / direct relationship traversal.
4. Why not SQL?
Deep traversals can require expensive joins and lookups.
5. Biggest distributed challenge?
Partitioning the graph and minimizing cross-partition traversals.
That is enough to handle most Graph DB questions in a System Design interview without getting lost in database-internals trivia.