Follow Graph Storage
You'll learn how to store and query the social graph—who follows whom—efficiently at massive scale.
The Core Challenge
A social media feed depends on knowing "Which users does Alice follow?" and "Who follows Bob?" These relationships form a graph where users are nodes and follows are directed edges. At scale (hundreds of millions of users, billions of relationships), choosing the right storage model directly impacts feed generation speed and system cost.
Storage Approaches
Adjacency Lists in Relational Databases
Store follows as simple rows:
followers(follower_id, followee_id, created_at)
- Query followers:
SELECT follower_id WHERE followee_id = ? - Query following:
SELECT followee_id WHERE follower_id = ?
Indexes on both columns enable fast lookups. Works well for moderate scale, but range queries and multi-hop traversals become expensive.
Graph Databases
Specialized stores like Neo4j treat relationships as first-class citizens. You model users as nodes with labeled "FOLLOWS" edges. Graph databases excel at:
- Multi-hop queries (friends-of-friends)
- Bidirectional traversal
- Complex relationship queries
Trade-off: higher operational complexity and cost. Best when you need advanced graph analytics beyond simple feed generation.
Choosing the Right Model
For feed generation specifically, adjacency lists in sharded relational stores often suffice. You primarily need fast single-hop lookups ("who does Alice follow?"), not deep graph traversals. Shard by follower_id for pull models, by followee_id for push models (covered in lesson 1637-1639).
Graph databases shine when your product needs recommendations, social circles, or influence analysis—features beyond basic feed display.
Key Takeaway:
Store follower relationships as adjacency lists in indexed relational tables for simple feed queries; upgrade to graph databases only when you need multi-hop traversals or complex graph analytics at scale.