Graph Databases
A graph database stores data as nodes (things) and relationships (connections between things), and is built to answer questions by walking those connections: who are my friends' friends, which accounts share a device with this fraudster, what's the shortest chain of introductions to this person. Neo4j is the best-known example, alongside Amazon Neptune, TigerGraph, JanusGraph, Memgraph and others.
A few properties are worth separating:
- Relationships are first-class data: In a relational database, a relationship is a foreign key you join on at query time. In a graph database, a relationship is a stored record with a type, a direction and its own properties, linked directly to the nodes at either end.
- Traversal-oriented storage: Native graph databases store each node with direct pointers to its relationships (index-free adjacency), so moving from a node to its neighbors costs about the same no matter how large the whole graph is.
- Pattern-matching query languages: You query by drawing the shape you're looking for, as in Cypher's
(a)-[:FRIEND]->(b), and the database finds every place that shape occurs. Variable-length paths ("one to four hops") are a single clause instead of a recursive query. - Usually ACID on a single machine: Neo4j offers full transactions. Distributed graph databases vary, and scaling a graph across machines is the hardest part of the whole category.
Graph databases come up in interviews in a specific way. Questions like "design LinkedIn connections," "design a friend recommendation feature," or "design fraud detection" have an obvious graph shape, and candidates often reach for Neo4j. Interviewers want to see that you know what a graph database actually does better, and also that you know why most large social networks don't run their core graph on one. This post covers both.
The Property Graph Model
Most graph databases use the property graph model:
- Nodes represent entities. Each has one or more labels (
Person,Company,Account) and properties (key-value pairs:name: "Ana",joined: 2019). - Relationships connect two nodes. Each has exactly one type (
FRIEND,WORKS_AT,USED_DEVICE), a direction, and its own properties (since: 2021,weight: 0.8). - Both can be indexed by property, which is how a query finds its starting nodes (look up the person with
email = ...), before traversing from there.
The appeal is that the model looks like the whiteboard sketch. In a relational schema, "Ana works at Acme since 2021" becomes a row in a employment join table with two foreign keys and a date. In a graph, it's an edge labelled WORKS_AT {since: 2021}, and many-to-many relationships need no join tables at all.
There's a second model you may hear about: RDF triple stores, where everything is a (subject, predicate, object) triple like (Ana, worksAt, Acme), queried with SPARQL. RDF is common in knowledge graphs and the semantic web, where data from many sources is merged and shared vocabularies matter. For application design interviews, the property graph is the one to know.
Modeling Choices
Graph modeling has its own judgment calls, and interviewers sometimes ask about them:
- Node or property? Make something a node when you'll traverse through it or when several entities share it. A device ID stored as a property on each account can't link accounts together; a
Devicenode that many accounts point to turns "which accounts share a device?" into a one-hop query. A user's birthday, on the other hand, is a property: nobody traverses through it. - Specific relationship types.
WORKS_AT,MANAGESandFOLLOWSare better than a genericRELATED_TO {kind: 'works_at'}. The database can skip whole relationship types during a traversal, but it has to read every generic relationship to check its property. - Direction is cheap to ignore. Every relationship is stored with a direction, but you can traverse it either way at no extra cost, so model a mutual friendship as one relationship and query it undirected, as the queries below do. Don't create two relationships for one symmetric fact.
- Intermediate nodes for rich events. A purchase that involves a user, a product, a store and a payment card is clearer as a
Purchasenode connected to all four than as a relationship with a pile of properties. It also lets fraud queries hop through purchases.