Graph Data Models 101
Graphs provide an alternative to tabular data structures, allowing for a more natural way to store and retrieve data
We’re overhauling Dgraph’s docs to make them clearer and more approachable. If you notice any issues during this transition or have suggestions, please let us know.
When building an app, you might wonder which database is the best choice. A traditional relational database that you can query using SQL is a familiar choice, but does a relational database really provide a natural fit to your data model, and the performance that you need if your app goes viral and needs to scale up rapidly?
This tutorial takes a deeper look at data modeling using relational databases compared to graph databases like Dgraph, to give you a better understanding of the advantages of using a graph database to power your app. If you aren’t familiar with graph data models or graph databases, this tutorial was written for you.
Learning goals
In this tutorial, you’ll learn about graphs, and how a graph database is different from a database built on a relational data model. This tutorial doesn’t include any code or syntax, but rather a comparison of graphs and relational data models. By the end of this tutorial, you should be able to answer the following questions:
- What’s a graph?
- How are graphs different from relational models?
- How’s data modeled in a graph?
- How’s data queried from a graph?
Along the way, you might find that a graph is the right fit for the data model used by your app. Any data model that tracks lots of different relationships (or edges) between various data types is a good candidate for a graph model.
Whether this is the first time you are learning about graphs or looking to deepen your understanding of graphs with some concrete examples, this tutorial should help you along your journey.
If you are already familiar with graphs, you can jump right into our coding example for React.
Graphs and natural data modeling
Graphs provide an alternative to tabular data structures, allowing for a more natural way to store and retrieve data.
For example, you could imagine that we’re modeling a conversation within a family:
- A
father
, who starts a conversation about going to get ice cream. - A
mother
, who comments that she would also like ice cream. - A
child
, who likes the idea of the family going to get ice cream.
This conversation could easily occur in the context of a modern social media or messaging app, so you can imagine the data model for such an app as follows:
For the remainder of this guide, we use this as our example app: a basic social
media or messaging app, with a data model that includes people
, posts
,
comments
, and reactions
.
A graph data model is different from a relational model. A graph focuses on the relationships between information, whereas a relational model focuses on storing similar information in a list. The graph model received its name because it resembles a graph when illustrated.
- Data objects are called nodes and are illustrated with a circle.
- Properties of nodes are called predicates and are illustrated as a panel on the node.
- Relationships between nodes are called edges and are illustrated as
connecting lines. Edges are named to describe the relationship between two
nodes. A
reaction
is an example of an edge, in which a person reacts to a post.
Some illustrations omit the predicates panel and show only the nodes and edges.
Referring back to the example app, the father
, mother
, child
, post
, and
comment
are nodes. The name of the people, the post’s title, and text of the
comment are the predicates. The natural relationships between the authors of the
posts, authors of the comments, and the comments’ topics are edges.
As you can see, a graph models data in a natural way that shows the relationships (edges) between the entities (nodes) that contain predicates.
Was this page helpful?