Thursday, May 2, 2024

Mastering DynamoDB: Essential Techniques for Modern Data Modeling by Tudor Ioan Marin Apr, 2024

dynamodb single table design

In the realm of NoSQL databases, DynamoDB stands out for its structured approach to managing data. First of all, everything related to clusters or servers is managed by AWS, so you only care about what happens from the table layer down. Dynamo doesn’t enforce a rigid schema on the items, they can have any attributes you see fit.

The DynamoDB Single-Table Design Process

Before even thinking of writing code or creating a database table, ensure you have determined all your application data entities and their relationships. You should document this as an Entity Relationship Diagram (ERD). The first key difference is that OneTable uses a schema to define the application entities, keys, attributes and table indexes. Having your application indexes, entities and keys defined in one place is much better than scattering these definitions throughout your application.

Second access pattern

DynamoDB also has fewer features than some other NoSQL databases. It provides only features that are scalable, in contrast to other databases like MongoDB, which is feature richer. This completes the journey to design your single-table DynamoDB database.

Use Case: Modeling Entities in DynamoDB's Single Table Design

Dynamo single table design refers to the practice of harnessing the full power of a single DynamoDB table to store multiple types of data. This approach sets it apart from traditional SQL databases, where each unique type of data typically resides in its own table. The patterns provided by modules such as Spring Data, which is used by Spring Boot based application for data access, still heavily depend on these patterns from the RDMS world. However, normalizing your data in this way doesn’t yield optimal results when you’re using a nonrelational database. Relational databases use joins to combine records from two or more tables, but those joins are expensive.

Principals of Data Modeling

We could get most of the same benefits by structuring our tables to fetch both sets of data in parallel, rather than sequentially. While a DynamoDB table has some things in common with a table in a relational database, there are a number of differences, too. Most importantly, each DynamoDB table is a separate piece of infrastructure. That infrastructure requires configuration, monitoring, alarms, and backups. If you have 15 different entities in your application and thus 15 different DynamoDB tables, it can become a burden. However, your table design is narrowly tailored for the exact purpose for which it has been designed.

dynamodb single table design

The DynamoDB table

With relational databases, you generally normalize your data by creating a table for each type of entity in your application. For example, if you're making an e-commerce application, you'll have one table for Customers and one table for Orders. The preceding table does not represent the races table in the normalized view, so you cannot query by class ID to retrieve a list of races. Depending on your design, you can solve this by adding a second index to the table to enable querying by class ID and returning a list of partition keys (race IDs). However, you can also overload GSIs to contain multiple types of value. While it’s possible to implement the design with multiple DynamoDB tables, it’s unnecessary and inefficient.

We need again to create a GSI that will give us the result we need. Now that we know how our data is structured, let’s see how can we meet all access requirements. Check out Part 2 where I walk through the process and example Python scripts for migrating to the new data model. In our example, we seamlessly integrated DynamoDB Toolbox into a Fastify application, showcasing the synergy between a robust web framework and the simplicity of Single Table Design. This integration empowers developers to focus on building features rather than wrestling with database complexities.

To do this, we'll take a quick journey through the history of databases. We'll look at some basic modeling in relational databases, then see why you need to model differently in DynamoDB. With this, we'll see the key reason for using single-table design. Colocating multiple entities in a single-table, on the other hand, allows you to fetch multiple entities in a single query because they are stored in the same partition. Chances are that not all entities in the table have the same throughput and being part of the same partition will help to alleviate overprovisioning of the table. The point is to have values in this index just to separate/filter them from the rest of the data.

Split into Multiple Documents with the Same Partition Key

In the request mapping template for allClasses, we define a query operation on the bySemester index. Then, we utilize the semester argument as the primary key, and scope down the query to all items with a typeWithID (the sorting key) starting with class#. This ensures that the query only returns classes in the response. OneTable centralizes key management for your queries and database operations. The entity partition and sort keys in OneTable can be ordinary attributes. However, it better to uncouple your keys by defining them as template strings that are calculated at run-time based on the values of other attributes.

When modeling a single-table design in DynamoDB, you start with your access patterns first. Think hard (and write down!) how you will access your data, then carefully model your table to satisfy those access patterns. When doing this, you will organize your items into collections such that each access pattern can be handled with as few requests as possible -- ideally a single request.

Applying best practices for securing sensitive data in Amazon DynamoDB Amazon Web Services - AWS Blog

Applying best practices for securing sensitive data in Amazon DynamoDB Amazon Web Services.

Posted: Mon, 11 Nov 2019 08:00:00 GMT [source]

To follow these pointers, the SQL language for querying relational databases has a concept of joins. Joins allow you to combine records from two or more tables at read-time. Before creating the DynamoDB table, test the proposed schema against the list of data access patterns. In this section, I review Alleycat’s list of queries to ensure that each is supported by the table schema. I use the Item explorer feature to run queries against a test table, after running the Alleycat simulator for multiple races. With this table design, you can query by racer ID or by race ID.

But if the data can change, you have to take care of consistency by updating each record when data changes. But in this case, this data is related to other items, so it needs to be duplicated. The most simple denormalization is to contain all the data in one item. This will create the test data according to the defined schema. Migrations are Javascript files that contains up and down methods that are invoked to upgrade or downgrade the database.

DynamoDB’s billing is based on write capacity units (WCUs) and read capacity units (RCUs). At a quick glance, one WCU allows you to write 1 KB of data, while one RCU allows you to read 4 KB of data. You can provision read and write capacity units in advance, or you can use on-demand billing to pay for each read or write request as it comes in.

The other, “byStudentID”, allows the querying of information for a specific student ID. We put values of the primary key and sort key only for shipped orders. We put customer ID in PK and ship date in SK, which enables us additional uses of this index. Although transactions are quite limiting, you need it just to form some patterns. They are not commonly used like in SQL databases because you do not need them that much.

No comments:

Post a Comment

Kitchen Cabinet Ideas

Table Of Content Modern Scandinavian Style Cabinets with Interior Details Traditional Elegance No Upper Cabinets Island Storage Space Unusua...