Topic 47

Graph Databases (and the Family Photo)

NoSQL

The last family in the tour is for data where the connections are the point — who follows whom, what leads to what, which regular tends to come with which other regular. A graph database stores those connections as first-class things and answers questions like "friends of friends of friends" in a way that relational JOINs can technically manage but practically groan under. Neo4j is the name. And once its portrait is drawn, the chapter closes with a family photo of everything you've met.

Nodes and Edges

A graph is made of two things. Nodes are the things — a customer, a film. Edges are the connections between them — "Anna FOLLOWS Marta", "Marta ATTENDED Night Bus". Both can carry a little information of their own (when the follow happened, which seat the attendance was). Draw it and you don't get a table; you get a web — dots joined by labeled lines. In a graph database the schema is that web, and connections aren't derived by matching ids later — they're stored directly, as themselves.

The Depth Problem, Honestly

Relational databases handle relationships perfectly well — pointing was the whole of Chapter 2. Where they groan is depth. Ask "who does Anna follow?" and it's a clean one-step lookup. Ask "friends of friends of friends" — three steps out — and in SQL you must join the follows table to itself, once per level:

The relational baseline — a self-join per level, readable but not something you'd want to own
SELECT f3.followed_id
  FROM follows f1
  JOIN follows f2 ON f2.follower_id = f1.followed_id
  JOIN follows f3 ON f3.follower_id = f2.followed_id
 WHERE f1.follower_id = 214;

Three levels, three joins. Go to six levels and the query is a monster; go to "as deep as it takes" and you can't write it at all, because you don't know the depth in advance. A graph database walks the edges directly, and depth is just a number you write in the query:

Cypher-style — one dialect; the depth 1..3 is just a number, not more joins
MATCH (a)-[:FOLLOWS*1..3]->(b) WHERE a.id = 214 RETURN b

The *1..3 says "follow the FOLLOWS edges, one to three hops out". Change it to *1..6 and you've asked for six levels without rewriting anything. Depth that turns SQL into a monster is, for a graph database, a parameter.

Think of a detective's corkboard: photos pinned up, red string running between them. The insight is never in a single photo — it's in the string, and the detective's native move is "what connects to this, three strings out?" A graph database is that corkboard made queryable. The analogy has done its work, so back to the real terms.

When Graphs Earn It

Graphs earn their place wherever the question is about connections traced through many hops: recommendations ("regulars who attend what you attend"), fraud rings (accounts linked through shared details), org charts, delivery routes, permission chains. Anywhere you find yourself asking "what's connected to this, and to that, and onward" — that transitive reaching is the graph's home.

And, one last time, the refrain: everything else the Marquee does is not this. Booking a seat, listing tonight's screenings, counting a film's sales — none of them trace connections through many hops, so none of them want a graph. The graph is a specialist you add when a genuinely connection-shaped question arrives, not a replacement for the core.

The Family Photo

Here is the whole chapter in one image. The Marquee's core — films, screenings, customers, bookings — lives in the relational database, the source of truth. In front of the homepage sits the key-value cache, serving "tonight's screenings" from memory. Beside the films sits the search engine, holding its copy of the text so the search box can forgive typos. And a future "regulars who attend together" feature would sit on a graph. One business, four families, each doing the one job it's best at, all arranged around a relational heart. The professional name for running several database families together like this is polyglot persistence — right tool, each job — and it is the mature shape of a real data layer, not a sign of indecision.

The family photo — one Marquee, four database families, each on its job
Polyglot persistence — right tool for each job, around a relational heart
The truthrelational core
Speedcache → homepage
Findabilitysearch → film box
Connectionsgraph → recommendations
Common Confusions
  • "Relational databases can't do relationships." Relationships are their middle name — pointing was Chapter 2. Graphs win at deep, variable-length traversals specifically; the distinction is depth, not whether relationships exist at all.
  • "I should pick one family and commit to it." The family photo is the answer to that instinct: real systems compose several families around one source of truth, each on its job. Loyalty is for sports teams, not databases.
  • "Graph databases are only for social networks." Fraud rings, logistics routes, permission chains, knowledge maps — anywhere the question is "what's connected to what, traced onward" — are all graph territory. Follows are just the friendliest example.
  • "Polyglot persistence means the design is a mess." It's the opposite — a deliberate small team of specialists around a relational heart. The mess would be forcing every job onto one family that fits only some of them.
Why It Matters
  • "How deep do my questions go?" is the last tool-choosing instinct this chapter installs — the one that tells a graph job from a relational one.
  • The family photo is the take-home of the whole chapter: a professional's data layer is a small, deliberate team of specialists arranged around a relational core.

Knowledge Check

In a graph database, what is an edge?

  • A stored connection between two things, like 'Anna FOLLOWS Marta'
  • One of the individual things in the graph, like a customer or a film
  • A column of data attached to a table row
  • The query you write to search the graph

Where do relational JOINs "groan" compared to a graph database?

  • On a single one-step relationship, like 'who exactly does Anna follow'
  • On deep, many-hop traversals, where each level adds another self-join
  • On counting and grouping rows, like sales per film
  • On fetching a single row by its primary key

In the Marquee's family photo, which job sits on the search engine?

  • Storing the bookings and screenings together as the source of truth
  • Serving 'tonight's screenings' instantly from memory
  • Powering the film search box that ranks results and forgives typos
  • Tracing which regulars tend to attend screenings together

What does "polyglot persistence" describe?

  • A single database that can speak several different query languages at once
  • Running several database families together, each on the job it fits best
  • A team that couldn't decide, so it uses every database at once
  • Replacing the relational database with NoSQL wherever possible

You got correct