![[aws-dynamodb-icon-454x512-71fcy541.png|150]]
DynamoDB is a "No SQL" database service offered by AWS. It can be highly scalable and performant—if you know what you're doing. Many of the traditional data modeling strategies you might use to build a relational database simply don't apply.
# Modeling
Modeling data in Dynamo is very different from relational database design. Tables in Dynamo are distributed across various partitions using a (you guessed it!) Partition Key.
Dynamo uses the Partition Key to generate a hash of the value. That hash is used to place the item within the network of nodes used to store the table's data.
![[Drawing 2023-03-21 20.43.26.excalidraw.svg]]
When querying data, you will **always** provide the Partition Key value. Dynamo applies the same hashing algorithm to the value you provided and quickly finds the node in the cluster that contains the item you're looking for. Since the algorithm used to generate the hash performs at constant time, there is no impact on performance as the number of items in your table grows to millions and millions of items.
# Single Table Design
# Access Patterns
When querying
Approaches:
* **Partition Key** = The thing you know
* **Sort Key** = The thing you are asking about
# Large Data
> [!info]
> [[Items]] in Dynamo can only be 400K.
For large items, you can:
1. Compress the data and store as a Binary attribute
2. Split data into multiple items using the sort key
3. Save the data into S3 and just save a link to the data
Tables in DynamoDB are comprised of [[Items]]. Items are similar to rows in a relational database. Each Items is defined by a collection of Attributes. Attributes are essentially name/value pairs where the value can be one of a number of different types.
## Attribute name-value pairs per item
The cumulative size of attributes per item must fit within the maximum DynamoDB [[Items|items]].
## Number of values in list, map, or set
There is no limit on the number of values in a List, a Map, or a Set, as long as the item containing the values fits within the 400 KB item size limit.
## Attribute values
Empty String and Binary attribute values are allowed, if the attribute is not used as a key attribute for a table or index. Empty String and Binary values are allowed inside Set, List, and Map types. An attribute value cannot be an an empty Set (String Set, Number Set, or Binary Set). However, empty Lists and Maps are allowed.
## Nested attribute depth
DynamoDB supports nested attributes up to 32 levels deep.
# Attribute Types
DynamoDB supports several different attribute types:
| Type | Abbr. |
|------|------------------------|
| String | `S` |
| Number | `N` |
| Binary | `B` |
| Boolean | `BOOL` |
| Null | `NULL` |
| List | `L` |
| Map[^2] | `M` |
| Set (of Strings) | `SS` |
| Set (of Numbers) | `NS` |
| Set (of Binaries) | `BS` |
## Examples
### String
```
"Hello world!"
```
---
> [!note]
> Strings are the best format to store **date** and **time** values.
```
"2023-05-03 13:47:21"
```
### Number
```
12345
```
---
```
123.45
```
### Map
```
{
"foo": "bar",
"hello": [
"124",
]
}
```
# Naming Conventions and Restrictions
There are certain rules[^1] that needs to be followed while naming a table in DynamoDB. The rules can be stated as follows:-
The following are the naming rules for DynamoDB:
- All names must be encoded using UTF-8, and are case-sensitive.
- Table names and index names must be between 3 and 255 characters long, and can contain only the following characters:
- `a`-`z`
- `A`-`Z`
- `0`-`9`
- `_` (underscore)
- `-` (dash)
- `.` (dot)
Attribute names must be between 1 and 64,000 (64KB) characters long. However, it is considered a best practice to keep attribute names as short as possible since attribute names will be included in your read/write usage billed by AWS.
These attributes are an exception to the 64KB rule:
1. Secondary index partition key names
2. Secondary index sort key names
3. User-specified projected attributes (when on a secondary index)
[^1]: https://docs.aws.amazon.com/amazondynamodb/latest/developerguide/HowItWorks.NamingRulesDataTypes.html
[^2]: Maps are ideal for storing JSON documents: