AWS Builder Center

What is a good DynamoDB data model?

What makes a good database application? It depends on who you ask. Technical and business requirements are obvious, but there are political and human factors to consider as well. Three common criteria for evaluating a proposed solution are cost, performance, and simplicity.

Specialist Solution Architect - DynamoDB
Philosopher

It's said that if you follow the first link in any Wikipedia article, and repeat the process, you will eventually end up at the  Philosophy  page. You can try this yourself at  pathtophilosophy.com 
A PhD, or Doctor of Philosophy degree is the highest academic degree, signifying expertise through original research and a dissertation, applicable across nearly all fields, not just philosophy, with the name stemming from "love of wisdom".
But what does this have to do with Amazon DynamoDB? Well, we will not be pondering questions like: what does it mean to live a good life? Instead, we will consider a relevant question:
What makes a good DynamoDB data model?
Or more generally, what makes a good database application? It depends on who you ask. Technical and business requirements are obvious, but there are political and human factors to consider as well. We will get to those soon.
Three common criteria for evaluating a proposed solution are cost, performance, and simplicity. These goals are somewhat in competition with each other. While a business may want to get all three, there will need to be tradeoffs. For example, a solution that is fast and simple may come with a high cost. A solution that has a low monthly cost and fast performance may require a complicated, custom solution that is hard to build and maintain. A common mistake is to focus on optimizing just one of these dimensions.
Rationales

These are but a simplification of many types of tradeoffs a software architect will need to make all throughout the design process. Let's dive into an example.
Whether to denormalize
We can examine whether it's best to normalize or denormalize data in a single table, especially when migrating multiple relational database tables into DynamoDB.
We could start simple and migrate tables as-is, 1-for-1 from our source SQL database. There are many advantages to doing this. The migration can be highly automated, and can use ETL tools like Amazon DMS to achieve a zero-downtime live migration.
However, the target tables in DynamoDB can no longer be queried all together with a SQL JOIN. Because of this, data retrieval becomes more complicated.
As we review NoSQL best practices, we discover that many solutions use the denormalization pattern to arrange data together in a single table.
But, if we dismiss the 1-for-1 table migration, we have crossed a significant line. Doing so is to claim there is a superior migration solution. We will be asked "why"? Our new solution may seem better, and may feel better, but we will need to explain and prove why it is better. We have responsibility for evaluating how good our solution is, and whether it is fully optimized. This is no small undertaking.
Let's continue and see if we can quantify a straightforward denormalization migration.
Starting with our source database, we might create a SQL VIEW from an INNER JOIN query on customers, order-lines, and products, as shown here.
Denormalized data

If we load this dataset into a single DynamoDB table, with our Partition Key of customer and Sort Key of order-line, then we have a table optimized for fast lookup of a single order line.
Fetch a single order line
For an application needing to fetch the item for cust-2, ord-2-line-3, here is the result:
Denormalized Order Line

In DynamoDB, a get-item call will perform well, returning data in as little as 5 milliseconds. We can consider the goal of optimizing single-order retrieval to be fully optimized. So, are we done?
Not so fast. We neglected to consider a few more things.
The first consideration is the impact of write operations on the customer and product entities. Because these entities are denormalized, duplicate values exist across many items in our table. If we need to update product or customer information, it will require a more complex operation to identify all affected items and loop to update each one separately.
With the information shown so far, we don't know the average number of items involved in such an update, or how often or likely this is to occur. Because writes are an order of magnitude more expensive than reads in DynamoDB, this could lead to higher costs and slow performance if an update needs to visit thousands or millions of items. Furthermore, if the update takes several minutes to complete, then some orders will have stale data right after the update job begins.
We now need to consider how tolerant is the business to this type of risk. If we are asked to dive deep and quantify this risk, we would need to conduct a survey to sample the dataset for some statistics. We would need to gauge the amount of duplication of customer and order information across all orders. Further, we would need to build a prototype and benchmark test in DynamoDB to measure how fast the application could identify and update these items, in order to tell stakeholders how long stale data would exist in the table. Without this, we now have a new, inconvenient dimension of uncertainty on top of our original three of cost, performance, and simplicity. We cannot say in advance how good our solution is since we can't easily estimate the cost or performance.
For example, the company may have a million order records, but only ten products. And some products may be more common than others. So, Product 1 could appear on half the order records while product 2 may appear on 1% of records. The cost and time to update the product on a denormalized dataset will vary greatly according to the statistics of each product. Looking at the schema alone will not help us understand the cost and time for a bulk update.
Another consideration is the added storage cost required to hold all the duplicated customer and product information. Without the dataset sampling done, including modeling of the size of the customer and product attributes, we would not know how much additional storage would be required and what that would cost.
Let's now try a different approach to the migration, by keeping our three entities normalized, or separated from each other.
These entities may exist in three separate tables, or all together in the same table.
Now, to fetch all the information for cust-2, ord-2-line-3, we would need to perform three steps.
1. Fetch the cust-2, ord-2-line-3 item from the order-line table, and notice the product ID.
2. Fetch the customer metadata
3. Fetch the product metadata using the product ID.
Single Table Normalized

We can compare the two approaches so far against our two access patterns.
Tradeoffs

We still don't have enough information to make a good design choice. We need to interview our stakeholders to find out what their priorities are.
Is Performance the top priority?
If so, is it more important to have:
· Fast single order lookups
· Good performance in updating customer and product information
Is Total Cost the top priority?
The business is likely to want both performance and cost optimized together. When presented with the options and tradeoffs above, the business will gain an understanding of the tradeoffs involved with each decision. The architect should require a prioritization, or ranking, of the priorities. Even better would be a rating of the relative importances.
For example, in one scenario the business requires low latency item lookups as a priority. Here we can see how the stakeholders rated the relative priorities of each dimension.
· Performance for get-item: 60% important
· Performance to update customers and products: 10%
· Consistency to update customers and products: 10%
· Total cost: 20%
A different business may prefer we optimize the total cost and not worry about performance:
· Performance for get-item: 10% important
· Performance to update customers and products: 20%
· Consistency to update customers and products: 20%
· Total cost: 50%
The more we dive into the details, the closer we get to being able to give a particular design recommendation to the stakeholder. That cost and performance estimates, and their priorities, are the inputs for the recommendation decision.
Did we go too far?
It's a fair question to ask if this level of analysis and modeling is required to generate a design recommendation. After all, if we are building a solution for a vegetable stand, either approach will work fine, as the volumes and costs involved will be so low compared to the cost of gathering requirements and modeling the options available. But, for a large bank with millions of customers and products, the cost of getting the design wrong is much higher.
We also have the cost of research to consider. A business may want to spend time prototyping, cost modeling, and performance benchmarking a proposed solution. The manager may need to decide whether it's worth it to spend a day, a week, or a month on this phase of the project. The cost of research is real, while the benefits of a thorough research effort are harder to estimate. However, it may be wise to spend more than a day on the research phase, especially if there are problems down the road, and design decisions are called into question.
Can't we just go with the standard approach?
In this case, industry conventions need to be considered. Relational database developers have been taught to normalize (to the third normal form) in order to save disk space and simplify updates to entities.
NoSQL developers, on the other hand, have been taught that denormalization is the way to go. When building an application that requires the absolute lowest latency for single-item queries, this is true. But that's only looking at a part of the total requirements.
This example considers only one design decision. A full solution could involve equally complex analysis, modeling, and tradeoffs for topics such as:
· Whether it's best to have a GSI to handle searches, or rely on Scan and Query with filter expressions
· How many GSIs are optimal, given their tradeoffs
· Whether to keep separate entities in the same table or not
· Whether large documents should be loaded as-is or deconstructed into item collections
· How best to isolate attributes to reduce the total write cost, while adding some complexity
What makes a good DynamoDB data model?
Can we now answer this question? In the world of consulting, this may be an easier question to answer. If we were a consultant hired to propose a good data model, a successful engagement would deliver a final report with:
· A list of requirements as understood, summarized and prioritized
· At least two proposed approaches, with estimates of cost, performance and simplicity
· A list of tradeoffs involved with each approach, side by side
· An offer to generate a prototype along with benchmark code, so the client can try it out and see the performance and cost
We should not attempt to deliver a single recommended approach, unless we can show our work and demonstrate why it is optimal.
If we deliver a good data model, we will:
· Make the client informed, enabled, and satisfied
· Be able to build a solution that performs well, is cost efficient, and is easy to maintain
Please join the #nosql topic to add your feedback below!
Any opinions in this article are those of the individual author and may not reflect the opinions of AWS.
Enjoyed reading this content? Let the author know!

Your likes, comments, shares, and saves help creators reach more builders.

Loading recommendations

Loading article