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 caseExample traversal
Social NetworkUser β†’ Friends β†’ Friends
RecommendationUser β†’ Product β†’ Category β†’ Product
Fraud DetectionAccount β†’ Transaction β†’ Account
Knowledge GraphPerson β†’ Company β†’ Product
Dependency GraphService β†’ Dependency β†’ Service
Network/TopologyRouter β†’ Link β†’ Router
Access ControlUser β†’ 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 Friends

requires 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 β†’ Charlie

The 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--> Bob

So traversal is approximately:

Node β†’ Edge β†’ Node β†’ Edge β†’ Node

rather than:

Node β†’ Index lookup β†’ Relationship table β†’ Index lookup β†’ Node

Key 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 β†’ friends

may 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/Documents

The graph is essentially an abstraction over another database.

Native

The storage engine itself is designed around graph structures:

Graph Query β†’ Graph Processing β†’ Native Graph Storage

Example: 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

SQLNoSQLGraph DB
Primary strengthStructured data + transactionsScale/flexible data modelsRelationships
Data modelTablesDocuments/KV/ColumnsNodes + Edges
RelationshipsForeign keys + JOINsUsually application-managedFirst-class
Deep traversal❌ Expensive❌ Usually awkwardβœ… Excellent
SchemaUsually structuredFlexibleFlexible
Transactionsβœ… StrongDependsDepends
Best forOrders, payments, usersLarge-scale KV/doc workloadsSocial/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, name
  • PURCHASED: timestamp, quantity
  • Product: 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}──> Bob

9. Direction matters

Relationships can be directed:

Alice ──FOLLOWS──> Bob

This does not necessarily mean:

Bob ──FOLLOWS──> Alice

For social networks, this distinction is important:

Alice ──FOLLOWS──> Bob
Charlie ──FOLLOWS──> Bob

Now 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──> User

Later:

User ──FOLLOWS──> User
User ──WORKS_AT──> Company
User ──OWNS──> Car
User ──PURCHASED──> Product

You 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 β†’ Device

Graph 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 ── C

If 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 β†’ Node

Network 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 ──> C

and 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 friend

Other 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.