Skip to main content

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:
  1. What questions do I need to answer?
  2. Design graph structure to answer them efficiently

Part 1: From Relational to Graph

Example: Social Network

Relational Schema:
Problem: Finding friends-of-friends requires self-join:
Graph Model:
Query:
Benefit: No JOINs, natural traversal, 100x faster!

Part 2: Modeling Patterns

Pattern 1: Entities as Nodes

Rule: Domain entities become nodes Example: E-commerce
Anti-Pattern: Storing lists as properties

Pattern 2: Relationships Capture Connections

Rule: Relationships represent actions, associations, or hierarchies Examples:
When to use relationships vs properties:

Pattern 3: Relationship Properties

Use Case: Metadata about connections

Pattern 4: Intermediate Nodes

Problem: Relationships can’t have relationships! Example: User enrolls in course on a specific date, gets a grade
Real-World Example: Order line items

Pattern 5: Multiple Labels

Use Case: Entity belongs to multiple categories
Query by specific role:
Benefits:
  • Fine-grained querying
  • Index optimization (indexes per label)

Part 3: Time-Series and Versioning

Pattern 6: Time-Series Events

Example: User actions timeline
Query: Recent events

Pattern 7: Versioning (Bi-Temporal Model)

Scenario: Track changes over time (audit trail)
Query: Price at specific date

Part 4: Handling Hierarchies

Pattern 8: Tree Structures

Example: File system
Query: All files under /home

Pattern 9: Hierarchies with Shortcuts

Problem: Deep hierarchies slow down queries Solution: Add shortcut relationships
Query:

Part 5: Many-to-Many Relationships

Pattern 10: Tags and Categories

Example: Blog posts with tags
Query: Posts with specific tag
Query: Posts with multiple tags (AND)

Pattern 11: User Roles and Permissions

Query: Check if user has permission

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:
  1. Fan-out to intermediate nodes:
  1. 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 node

Part 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
Model:
Queries:
  1. Newsfeed: Posts from people I follow
  1. Popular posts: Most liked
  1. Trending tags: Most used in last 24 hours

Example 2: Recommendation Engine

Model:
Query: Recommend products

Example 3: Knowledge Graph

Model:
Example Data:
Query: What did Steve Jobs create?

Part 8: Refactoring Strategies

Refactoring 1: From Properties to Relationships

Before:
After (if cities have more data):
When to refactor: When you need to query by location, aggregate by city, or add city-specific properties.

Refactoring 2: Adding Intermediate Nodes

Before:
After:
Benefits: Can add relationships to enrollment (teacher, classroom, etc.)

Refactoring 3: Denormalization for Performance

Before (normalized):
After (denormalized):
Trade-off: Faster reads, slower writes (acceptable for read-heavy workloads)

Summary

Design Principles:
  1. Query-driven: Start with questions, design graph to answer them
  2. Entities → Nodes: Domain objects become nodes
  3. Connections → Relationships: Actions, associations, hierarchies
  4. Intermediate nodes: When relationships need relationships
  5. Denormalize: Duplicate data for read performance
Anti-Patterns to Avoid:
  • Dense nodes (millions of relationships)
  • Redundant relationships
  • Property explosion
Next: Apply these patterns to graph algorithms!

What’s Next?

Module 6: Graph Algorithms

Implement PageRank, community detection, shortest paths, and centrality algorithms at scale