esc
  • WorkflowsPages
  • Case studiesPages
  • Machine Learning FoundationsLearn
  • AI EngineeringLearn
  • AI Guardrails & Red TeamingLearn
  • UI/UXLearn
  • Design SystemsLearn
  • Production System DesignLearn
  • Book a callContact
  • Download ResumeContact
Let’s talk

I’ll build the agent for it.

Graph Databases: Storing Connections as Edges You Can Follow

A graph database stores things and the links between them, so questions about connections are fast to ask. Learn the model, the good fits, and the poor fits.

In short

A graph database stores things as nodes and the links between them as edges, so questions about connections, like friends of friends, are fast to ask.

9 min readFree

What you will learn

Most systems begin by storing records in tables. Some data, though, is mostly about how things connect. Who follows whom, which shop a buyer visited, and which account sent money to which other account are all links. A graph database is a database built around those links. It is a type of NoSQL database. By the end of this lesson you will know the graph model, why walking links can be quick, a worked example, and when a graph is the wrong choice.

Key words

Graph database
A database that stores things and the links between them as its main shape.
Node
One thing you store, such as a person, a product, or an account.
Edge
A link between two nodes, such as 'follows' or 'bought'.
Property
A small named fact attached to a node or an edge, stored as a key and a value.
Label
A tag that groups nodes of one kind, such as Person.
Hop
One step along an edge, from one node to the next node.
Traversal
Walking through a graph by following edges from node to node.
Join
A step in SQL that matches rows from two tables using a shared value.
Relational database
A database that keeps data in tables made of rows and columns.
NoSQL
A family of database types that do not all store data in fixed-column tables.

What is the graph data model?

A graph database keeps data in three parts. Nodes are the things. Edges are the relationships between them. Properties are the details that describe a node or an edge. SAP calls properties key-value pairs, which means each detail has a name and a value.

  • Nodes

    Each node stands for one thing, such as a person, a movie, a city, or a bank account. Think of a node as a noun.

  • Edges

    Each edge joins two nodes and names the relationship, such as FOLLOWS or PURCHASED. An edge can point one way, like a follow, or work both ways, like a friendship.

  • Properties

    Both nodes and edges can carry properties. A person node might hold a name and a city. A FOLLOWS edge might hold the date the follow began.

Labels help too. A label groups nodes of one kind, so a query can ask only for People and skip Products. Labels do not change the shape of the graph. They only sort the nodes.

How does a graph compare with tables and documents?

The easiest way to see the graph idea is to compare it with the models you may already know. Each model keeps the same facts in a different shape, and the shape decides which questions are easy.

  • Tables and rows

    A relational database stores each kind of item in its own table. A link between two tables is just a shared key, such as a customer ID in an orders table. To find the link, the database must join the tables when you ask.

  • Documents

    A document store keeps one whole record in one flexible file. This works well when one record holds almost everything about one item, such as a product with all its details.

  • Key-value pairs

    A key-value store maps one key to one value. It is fast when you know the key, but it does not describe links between items.

Tip

The main difference is where the connection lives. In a table design, the link is worked out at question time. In a graph, the link is stored as an edge and is ready to follow.

Why are traversals fast?

Take a common question: who are the friends of my friends? In a table design, you start with your ID, find your friend rows, then join the friends table again to find their friends. Each extra hop is another join, and each join builds a larger temporary result. A graph database avoids this by storing links directly.

The second part of the answer is the traversal. Two ideas make it quick. First, the database starts at one node and only looks at the edges attached to it. Second, it does not scan the whole data set at each step. The steps below show the work.

  1. 1

    Start at one node

    Find your own node. This is a direct lookup of one item.

  2. 2

    Follow the first hop

    Move along every edge from your node to your direct connections.

  3. 3

    Follow the second hop

    From each connection, move along their edges. Skip yourself and anyone you already know.

  4. 4

    Count what you found

    Each person reached is a candidate. Count how many paths led to each one.

Treat that as a claim about the design, not a promise for every product.

Tip

Graphs help most when a query walks several hops. A one-hop lookup, such as one user's profile by ID, can be just as fast in a table, so do not expect a graph to win every query.

Worked example: People You May Know

Social networks were early users of graph databases. A feature called 'People you may know' is a clear example. The model below is simplified. It is not how LinkedIn builds its feature. It shows the idea: start from you, walk two hops along connection edges, and rank people by how many paths lead to them.

The sample network

Five people are stored as nodes: Ana, Ben, Cara, Dev, and Eli. Ana is you. Ana is connected to Ben and Cara. Ben is connected to Dev and Eli. Cara is connected to Dev. Connections go both ways, so each edge is stored in both directions. Ana already knows Ben and Cara, so she should not be suggested to herself or to them.

Steps the database takes

  1. 1

    Start at Ana

    Ana's node is the starting point. Her direct connections are Ben and Cara.

  2. 2

    Hop one

    Move along each edge from Ana. You reach Ben and Cara.

  3. 3

    Hop two

    From Ben, you reach Dev and Eli. From Cara, you reach Dev again. Ana also appears, but she is you, so skip her.

  4. 4

    Count the paths

    Dev is reached by two paths, one through Ben and one through Cara. Eli is reached by one path, through Ben.

  5. 5

    Suggest the best matches

    Suggest Dev first, because two of your connections also know Dev.

Python
knows = {
    'Ana': ['Ben', 'Cara'],
    'Ben': ['Ana', 'Dev', 'Eli'],
    'Cara': ['Ana', 'Dev'],
    'Dev': ['Ben', 'Cara'],
    'Eli': ['Ben'],
}

def suggestions(person):
    direct = set(knows[person])
    counts = {}
    for friend in knows[person]:
        for friend_of_friend in knows[friend]:
            if friend_of_friend != person and friend_of_friend not in direct:
                counts[friend_of_friend] = counts.get(friend_of_friend, 0) + 1
    return counts

for name, paths in sorted(suggestions('Ana').items()):
    print(name, paths)

Output

Dev 2
Eli 1

The sample above uses a plain Python dictionary as a tiny stand-in for a graph. It is not a graph database. Still, it shows the walk: each hop follows the lists attached to a node, with no table join.

In SQL, the same answer needs a self-join on a large connections table. A self-join matches a table against itself, once for each hop. Each hop needs its own join against the connections table. A graph database avoids this because each node already points to its neighbors. Real products add more signals, but the core pattern stays the same: start at a node, walk edges, and count what you reach.

When should you choose a graph database?

Choose a graph database when the connections are the point of the question, not a small detail. A good test is to ask whether the answer depends on links that run several steps deep.

  • Social networks

    Friends, followers, and group members form a web of links. Users often ask about their wider circle, which is a multi-hop question.

  • Recommendations

    Shows, products, or songs can be suggested by following what viewers or buyers with similar tastes chose. Cognee notes that streaming and shopping services use this pattern.

  • Fraud detection

    Accounts, cards, devices, and transactions can be linked as one network. Suspicious rings often show up as unusual paths between them.

  • Routing

    Places can be nodes and roads can be edges. Google Cloud notes that analysing locations and routes can help cut travel times and costs.

When should you avoid one?

A graph is not a faster version of every database. Some jobs are a poor match, and picking a graph for them adds cost without benefit.

  • Simple lookups

    If you only need one record by its ID, a table or a key-value store is simpler. A graph adds nothing when there are no links to walk.

  • Heavy totals

    Adding up millions of records, or counting across the whole data set, is a job that tables handle well.

  • Very large data that is hard to split

    Sharding methods get their own lesson.

Tip

Use a quick test. If the same question would need several joins in SQL, a graph may fit. If the question is one total over everything, a table will probably fit better.

Check yourself

Try these two short scenarios. Each one needs the idea from this lesson.

Quick check

A streaming app wants to recommend shows. It looks for what viewers who watched the same shows as you also watched, three steps away in the chain of viewers and shows. Which store fits, and why?

Quick check

A bank needs a monthly report that adds up the amounts of 20 million card payments and groups the totals by shop type. Which choice fits best?

Key ideas to remember

A graph database stores nodes, edges, and properties. Its main strength is following links, such as friends of friends, without building large joins. Choose it for social networks, recommendations, fraud detection, and routing, where connections drive the question. Avoid it for simple lookups, heavy totals, and very large data that is hard to split. Keep the test in mind: if the question is about how things connect several steps deep, a graph is worth a look.

Sources

How is this lesson?

Last updated on 17 November 2026

Learn with me

Every lesson is free. I add new ones as I learn. Have a question, or want help with your own work? Book a short call.