DynamoDB is a reliable, managed NoSQL database that is not only easy to use but also supports consistent reads and transactions. But did you know that it can also be even easier to use by using a single-table design for storing all entities? In this article we will dive into single-table design and why it would be beneficial to use as well as why you would want to avoid it.
So, what is single-table design? Single-table design is where all your different entities (document types) go into the same table. This makes management of the table easy because you are only managing one table. If you have a simple site or are building a basic web application, single-table design may be the solution for you.
In a single-table design, the index should be generic enough to not be coupled to any single entity. This means that you should name the attribute as such. You should also make the partition-key a composite index in which you have a sort-key as well. The sort-key will typically be the classifier of the entity type. For sake of simplicity, you can use the composite index below:
When dealing with a single-table design, you will want to at-minimum be able to retrieve results based on the classifier of the entity type. With a secondary index, the hash-key does not need to be unique, so we can make a composite secondary index based on the hash of the entity type classifier and the range of the sort-key. We will want this key to be a global secondary index. You can use the composite secondary index below:
Typically, with the above SORT key it would be a date or date-time.
The indexes above will give you the minimum functionality to implement and use a single-table design. As you figure out your access patterns in your site or web application, you can add more secondary indexes. With the above indexes, you can access the data by listing it by type sorted by the sort-key, and then getting an individual entity by the primary-key and type. This access pattern should suffice for most sites and web applications that need basic lookup functionality.