In this article we'll cover:
- 3️⃣ 3 steps to grasp DynamoDB working as a Graph database
- 📺 Work with examples and illustrative data
- 💪 Increasing visibility of the serverless stack
DynamoDB is the NoSQL, managed database by AWS. It is designed more as a key-value store. But it can be hacked to work as a graph database.
Which is pretty cool 😉
There is a data access pattern called Adjacency List. It's a way to describe nodes and edges. We can use it in DynamoDB to leverage graph relationships.
Each entry in DynamoDB has two basic keys:
The first (
primary-key) allows efficient access to the entries in the DB. Think of it as the entries'
The second (
sort-key) allows... well, for sorting results, obviously.
As an example, we could model e-commerce purchases as:
|order-1||1570147200||['Tesla Roadster', 'Sunglasses']|
|. . .||. . .||. . .|
This allows us to query by Order ID, as well as sort orders by their timestamp.
One could say a graph (node-edge model) is a form of a many-to-many relationship. As such, it can be represented by an adjacency list. The
primary-key represents the top-level item, while the
sort-key is used for associations. Consider:
- A blog has two posts: "Post-1" and "Post-2"
- There are three possible tags: "cool", "awesome", "neat"
|Primary Key||Sort Key||Title||Text|
|Post-1||Post-1||Hello World!||I'm a Post inside Dynamo!|
|Post-2||Post-2||Foo Bar||Can't wait for the graph|
primary-key == 'Post-1' && sort-key == 'Post-1'
primary-key == 'Tag-2' && sort-key == 'Tag-2'
Now, say Post-1 has one tag: cool.
|Primary Key||Sort Key||Primary Title||Sort Title|
The connection is represented twice, which is important from an access pattern standpoint, as you can see below:
Get tags assigned to Post-1:
primary-key == 'Post-1' & sort-key BEGINS_WITH 'Tag'
Get posts tagged with Tag-1:
primary-key == 'Tag-1' & sort-key BEGINS_WITH 'Post'
BEGINS_WITH operator allows matching multiple Tags associated with a Post, or vice versa.
By having two entries in the DB for each connection, we can reach the other node regardless of our starting point. If we have a
tag, we can find all
posts associated. If we have a
post, we can find all
We'll take the tripestore model for this example. It consists of subject-predicate-object, such as:
Now, how do we model this data structure in DynamoDB? We leverage the adjacency pattern!
Say we have a Friendly social media site. Here's how we could model it in DynamoDB.
We start by describing the
nodes. In our example, we only have
users. These will be the
objects of the triplestore model. A
name property is also added to each user:
|Primary Key||Sort Key||Name|
|Primary Key||Sort Key|
In the case of predicates, there are no properties associated.
Finally, we model the relationships between
predicates to connect them. We also add a property indicating when the relationship started:
|Primary Key||Sort Key||Relationship Start|
Again, we have two entries for each connection, also for access patterns reasons, as explained above in the
post <> tag example.
Here's how we read the
- Ross and Monica are siblings
- Monica and Rachel are friends
- Rachel and Ross are married
Access patterns to retrieve our data:
Who is married to Ross?
primary-key == 'User-1' & sort-key BEGINS_WITH 'Pred-Married-User'
Who are Monica's siblings?
primary-key == 'User-3' & sort-key BEGINS_WITH 'Pred-Sibling-User'
Who are Rachels's friends?
primary-key == 'User-2' & sort-key BEGINS_WITH 'Pred-Friend-User'
The example above is very simplified, but it illustrates the idea.
It's possible to add other types of nodes to the graph. For instance:
|Primary Key||Sort Key||Title|
One advantage is that links can have their own properties as well. For instance: in the
Ross-Married-Rachel link, we could have properties such as where the couple met:
|Primary Key||Sort Key||Meeting Place|
Or we could model the "Meeting Place" as a node in the graph:
|Primary Key||Sort Key|
Get everyone Ross met at the Cafeteria:
primary-key == 'User-1' & sort-key BEGINS_WITH 'Pred-MeetingPlace-Location-1'
Where did Ross meet Rachel?
primary-key == 'User-1' & sort-key BEGINS_WITH 'Pred-MeetingPlace-User-2'
This is actually kind of a hack of the triplestore model. The predicate
Pred-MeetingPlace-Location-1 is actually a combination of a real predicate
Pred-MeetingPlace and a node
Location-1. This allows for flexible queries. Think of it like the node attached modifying, or characterizing the predicate.
The full table combined is available down at the end of the article.
It is important to think about your access patterns before modeling your data. It may be difficult to add support for different access patterns down the road.
Check the DynamoDB documentation to learn more.
DynamoDB is easy to get started with and keeps infrastructure hurdles to a minimum. Especially for startups and small teams that can't afford a DevOps team to maintain healthy DB servers/instances, it can be a great ally.
In summary, Dynamo will give us:
- Fully managed & serverless: minimal infrastructure overhead
- Hyper scalability: in both IO and storage, Dynamo can scale to tens of thousands of concurrent requests and exabytes of data
- Integration with Lambda
- Microsecond latency
- Built-in global replication
That's a common concern about serverless.
We really need to ask ourselves: what and why do I want to control?
Usually, it boils down to monitoring, making sure everything is running smoothly. Serverless indeed requires a different approach to observability.
There are professional services, that can take care of that. Dashbird, especially, was created from the ground up with the goal to provide full visibility over entire serverless stacks, so you might want to check them out.
|Primary Key||Sort Key||Primary Title||Sort Title||Text|
||I'm a Post in Dynamo!|
||Graph is cool!|
|Primary Key||Sort Key||Title||Rel. Start|
I work as a Developer Advocate at Dashbird.
- There is a great talk from re:Invent 2018 covering design patterns in DynamoDB, I definitely recommend watching.
- AWS has documentation with info on the adjacency list implementation.