Course contentsShow
← System Design
Lesson 454 of 1,91913. NoSQL: Graph DatabasesFree lesson

When Relationships Are First-Class Citizens

How graph databases make relationships first-class, enabling efficient traversals without expensive joins.

When Relationships Are First-Class Citizens

You'll learn: How graph databases treat relationships as equally important as data entities, making them fundamentally different from other database types.

The Traditional Problem

In relational databases, relationships between entities are implicit—expressed through foreign keys and requiring JOINs to traverse. Each JOIN operation scans indexes, matches rows, and builds temporary result sets. When you need to traverse multiple relationships (like "friends of friends of friends"), the query becomes exponentially more expensive:

-- Finding friends of friends requires multiple joins
SELECT DISTINCT u3.*
FROM users u1
JOIN friendships f1 ON u1.id = f1.user_id
JOIN users u2 ON f1.friend_id = u2.id
JOIN friendships f2 ON u2.id = f2.user_id
JOIN users u3 ON f2.friend_id = u3.id
WHERE u1.id = 123;

First-Class Relationships

Graph databases flip this model. Relationships (edges) are first-class citizens—stored explicitly as data structures with their own identity, properties, and direct pointers between nodes. Each edge physically points to its start and end nodes, making traversals a simple pointer-following operation.

Analogy: Imagine a party where everyone wears name tags (nodes) and colored ribbons connecting them to their friends (edges). To find your friend's friends, you just follow the ribbons—no searching through a guest list required.

Why This Matters

When relationships are first-class:

  • Traversal is O(1) per hop instead of requiring index lookups and joins
  • Multi-hop queries ("find friends within 3 degrees") remain fast
  • You can query relationship properties directly (when friendships started, relationship strength)
  • Adding new relationships doesn't require expensive schema migrations

This makes graph databases ideal for social networks, recommendation engines, fraud detection, and knowledge graphs where the connections are as important as the data itself.

Key Takeaway: Graph databases make relationships first-class citizens by storing them as explicit, navigable structures rather than implicit foreign keys, enabling constant-time traversals instead of expensive joins.