Graph Data Modeling Best Practices
Module Duration: 5-6 hours
Learning Style: Pattern-Based + Refactoring Examples + Real-World Schemas
Outcome: Design graph models that match query patterns and perform at scale
The Golden Rule
In relational databases: Normalize first, query later In graph databases: Design for your queries Graph modeling is query-driven. Start with:- What questions do I need to answer?
- Design graph structure to answer them efficiently
Part 1: From Relational to Graph
Example: Social Network
Relational Schema:Part 2: Modeling Patterns
Pattern 1: Entities as Nodes
Rule: Domain entities become nodes Example: E-commercePattern 2: Relationships Capture Connections
Rule: Relationships represent actions, associations, or hierarchies Examples:Pattern 3: Relationship Properties
Use Case: Metadata about connectionsPattern 4: Intermediate Nodes
Problem: Relationships can’t have relationships! Example: User enrolls in course on a specific date, gets a gradePattern 5: Multiple Labels
Use Case: Entity belongs to multiple categories- Fine-grained querying
- Index optimization (indexes per label)
Part 3: Time-Series and Versioning
Pattern 6: Time-Series Events
Example: User actions timelinePattern 7: Versioning (Bi-Temporal Model)
Scenario: Track changes over time (audit trail)Part 4: Handling Hierarchies
Pattern 8: Tree Structures
Example: File systemPattern 9: Hierarchies with Shortcuts
Problem: Deep hierarchies slow down queries Solution: Add shortcut relationshipsPart 5: Many-to-Many Relationships
Pattern 10: Tags and Categories
Example: Blog posts with tagsPattern 11: User Roles and Permissions
Part 6: Modeling Anti-Patterns
Anti-Pattern 1: Dense Nodes
Problem: Node with millions of relationships (celebrity with 10M followers) Issue: Traversing all relationships is slow Solution:- Fan-out to intermediate nodes:
- Use properties for aggregates:
Anti-Pattern 2: Redundant Relationships
Problem: Same information as properties Example:Anti-Pattern 3: Property Explosion
Problem: Too many properties on a nodePart 7: Real-World Examples
Example 1: Social Media Platform
Requirements:- Users post content
- Users follow each other
- Posts have likes and comments
- Posts are tagged
- Newsfeed: Posts from people I follow
- Popular posts: Most liked
- Trending tags: Most used in last 24 hours
Example 2: Recommendation Engine
Model:Example 3: Knowledge Graph
Model:Part 8: Refactoring Strategies
Refactoring 1: From Properties to Relationships
Before:Refactoring 2: Adding Intermediate Nodes
Before:Refactoring 3: Denormalization for Performance
Before (normalized):Summary
Design Principles:- Query-driven: Start with questions, design graph to answer them
- Entities → Nodes: Domain objects become nodes
- Connections → Relationships: Actions, associations, hierarchies
- Intermediate nodes: When relationships need relationships
- Denormalize: Duplicate data for read performance
- Dense nodes (millions of relationships)
- Redundant relationships
- Property explosion
What’s Next?
Module 6: Graph Algorithms
Implement PageRank, community detection, shortest paths, and centrality algorithms at scale