Database Systems · Guide

Graph Databases

When graphs win, model patterns, use cases.

— min read Database Systems

Theory

Graph Databases: When graphs win, model patterns, use cases.

A graph database stores data in a property graph model: nodes represent entities, edges (relationships) represent connections between them, and both can carry properties. The fundamental difference from relational databases: in SQL, relationships are discovered at query time via JOIN (O(n) table scans). In a graph database, relationships are stored as pointers — traversal is O(1) per hop because no join computation is needed.

When graph databases win: multi-hop traversals (friends-of-friends, 6 degrees of separation), pattern matching (detecting fraud rings — accounts that share device IDs, emails, or phone numbers), hierarchical queries (organizational charts, category trees, bill of materials), and knowledge graphs (entity relationships at Wikipedia/Google scale). When SQL wins: simple CRUD, aggregations on flat data, existing tooling investment.

Query languages: Cypher (Neo4j, Memgraph, Kuzu) uses ASCII art patterns — MATCH (a:Person)-[:KNOWS]->(b:Person). Gremlin (Apache TinkerPop — JanusGraph, Amazon Neptune, CosmosDB) is a traversal language: g.V().has('name','Alice').out('knows').values('name'). SPARQL queries RDF triple stores (semantic web data). GQL is the ISO standard being standardized across vendors.

Graph database options: Neo4j (mature, native graph, Cypher, ACID, enterprise clustering), Amazon Neptune (managed on AWS, supports Gremlin and SPARQL — no Cypher), JanusGraph (open-source, distributed, uses Cassandra/HBase as backend storage), ArangoDB (multi-model: document + graph + key-value in one engine), RedisGraph (graph module for Redis — deprecated 2023, replaced by FalkorDB).

Modeling: nodes get labels (`:Person`, `:Product`). Relationships are typed and directed (`:PURCHASED`, `:FOLLOWS`) but can be traversed in both directions in queries. Properties on both nodes and relationships. A common mistake: creating generic relationships (:RELATED_TO) instead of specific ones — specific relationship types enable selective traversal and make query intent clear.

Scaling: Neo4j Enterprise scales reads via read replicas; writes go through a single leader. JanusGraph scales by distributing the graph across Cassandra partitions — but cross-partition traversals are expensive. Graph partitioning (assigning nodes to shards) is an NP-hard problem; in practice, keep highly connected subgraphs on the same shard. Most social-scale graphs (Twitter follows, Facebook friends) use custom distributed graph engines (Giraph, GraphX, FlyG) rather than commercial graph databases.

Graph algorithms at scale: Apache Giraph (Pregel model — batch graph processing), GraphX (Spark), and Neo4j GDS run algorithms like PageRank, shortest path, betweenness centrality, and community detection. These operate on the full graph in memory across distributed workers. Results from batch algorithms are typically written back to the graph or exported to a feature store for ML.

Architecture Diagram

Users / clients
         |
  Graph Databases
         |
  Core services
         |
  Data + observability

Examples

bash
# Graph Databases
# When graphs win, model patterns, use cases.
# Validate in staging before production rollout.

Key Concepts

Data modelHow Graph Databases structures and queries data
ConsistencyGuarantees under failure and replication
Scaling axisVertical vs horizontal growth patterns
OpsBackup, monitoring, and upgrade path

Interview Questions

What problem does Graph Databases solve?

It addresses the core use case described in production architecture — map features to reliability, scale, or velocity outcomes.

Key components of Graph Databases?

Identify inputs, outputs, control plane, data plane, and failure domains — interviewers want structured decomposition.

Common production pitfalls?

Misconfiguration, missing observability, no rollback path, and scaling bottlenecks under peak load.

How do you test changes safely?

Staging parity, canary/gradual rollout, automated health checks, and documented rollback.

Metrics to prove success?

Error rate, latency percentiles, throughput, cost, and toil reduction — pick one primary SLO.

Beginner vs advanced concern?

Beginners focus on setup; advanced teams focus on blast radius, security boundaries, and operability at 10× scale.

Best Practices

  • Treat Graph Databases config as code with review and CI validation.
  • Define SLOs and dashboards before production cutover.
  • Document rollback and ownership for on-call.
  • Use least privilege for credentials.

Common Mistakes

  • Adopting Graph Databases without measurable success criteria.
  • No staging environment mirroring production constraints.
  • Missing rollback path during incidents.
  • Undocumented on-call expectations.

Cheat Sheet

GraphWhen graphs win, model patterns, use cases.
SLOService level objective
RollbackRevert to last known good
CanaryLimited blast-radius rollout
RunbookIncident steps

Practical Exercises

Graph Databases sandbox

Stand up Graph Databases locally or in free tier; document commands and failure recovery.

Failure drill

Introduce misconfiguration; practice detection and rollback under time limit.