Skip to content
tezvyn:
Menu

Graph Databases: When Relationships Are the Data

Source: Wikipedia: Graph databaseMediumHow cards are made

A graph database treats connections between data as first-class citizens. It's ideal for social networks or fraud detection where you query relationships by traversing links. The footgun is using it for simple tabular data where a relational DB is faster.

Why it exists

Relational databases struggle with deep, complex relationships. Queries like "find all friends of friends of my friends" require multiple expensive JOIN operations that slow down as the dataset grows. Graph databases were created to solve this by making relationships a core, directly queryable part of the data model.

The mental model

Think of a social network map. Each person is a node, and the line connecting two friends is an edge. A graph database stores this map directly. Instead of calculating relationships at query time by joining tables, you just walk along the pre-drawn lines from one node to another. The relationship is stored, not computed.

How it works

Data is stored as a collection of nodes (entities like 'User' or 'Product'), edges (relationships like 'KNOWS' or 'BOUGHT'), and properties (attributes on nodes or edges, like a user's name or the date of a purchase). Queries traverse this graph, moving from node to node via their edges. Because the relationships are physically stored pointers, this traversal is extremely fast, regardless of how many connections you cross or the total size of the database.

When to use it

Use a graph database when your queries are about the relationships between data points. Three common scenarios: first, social networks for finding connections; second, fraud detection for identifying linked rings of suspicious accounts; third, recommendation engines for suggesting items based on what similar users have liked.

When not to use it

Avoid graph databases for data that isn't heavily interconnected. If your primary queries are aggregating large amounts of data (e.g., "what is the total sales for Q3?") or fetching simple records by ID, a relational or analytical database is a better tool. They are not a one-size-fits-all replacement for other database types.

One canonical example

To find all of a user's "friends of friends" in a relational database, you might need multiple self-joins on a users table and a friendships table, which gets very slow. In a graph database, you start at the user's node and simply traverse the "FRIEND" edge twice. The query is intuitive and performance remains high, even as the total number of users grows into the millions.

Interview question

Which application best leverages the core strength of a graph database?

  • a.Analyzing financial transactions to detect patterns of fraudulent activity across multiple accounts.Correct
  • b.Storing a large catalog of products with their prices and descriptions for e-commerce.
  • c.Aggregating website traffic logs to generate daily unique visitor counts.
  • d.Maintaining employee records, including salary and department, for HR reporting.
Why?

Graph databases are designed to make relationships first-class citizens, excelling in scenarios like fraud detection where identifying complex, linked patterns between entities is critical. The other options describe use cases better suited for relational or analytical databases, focusing on simple record storage, tabular data, or aggregate queries rather than intricate relationships.

Just read this? Test yourself on what you have been reading.

Read the original → en.wikipedia.org

Put your scrolling time to good use

Learn one idea, try a quiz and save useful cards for revision. Tezvyn makes it easy to learn and stay current in your tech field, a few minutes at a time.

The iPhone app is on the way

We are building it. Until it lands, nothing here is held back from you: every interview card, your saved cards, streaks and the job board all work in Safari, plus hundreds of free practice quizzes of thirty questions each. Sign in and it all carries over to the app the day it arrives.

Want it as an icon? Tap Share at the bottom of Safari, then Add to Home Screen. It opens full screen and the cards you have read stay available offline.

Get it on Google PlayiPhone app coming soon

We are hiring for this. Open roles that interview on databases — each one lists the topics its interview covers.

See open roles