Dynamo DB Cheat Sheet
Partitions - 10% Rule
Use Range Keys to make querying data easier
Using Compound Sort Keys for grouping data
One Table - Multiple stored datatypes
Partitions - 10% Rule
Never have a partition that contains more than 10% of the table data.
When Dynamo is storing your data it groups it all by the partition key. Multiple partitions can be
stored on a single SSD, but one partition can never be split across multiple SSDs. This means
that you want to make sure that you always create small partition keys. You don’t want to have
one partition, and therefore one SSD, getting hit with most of the requests and becoming
throttled.
Use Range Keys to make querying data easier
As well as a Partition Key you can have a Range Key. This key is different from a partition key
in that you can query on more than an exact match.
Condition Example
= rangeKey = 1598942275
< rangeKey < 1598942275
<= rangeKey <= 1598942275
> rangeKey > 1598942275
>= rangeKey >= 1598942275
BETWEEN rangeKey BETWEEN 1598242275 AND 1598942275
begins_with() begins_with( rangeKey, ‘15983’ )
contains() contains(rangeKey, ‘82422’)
Using Compound Sort Keys for grouping data
Sort keys can be used for more than querying on a date. It can be used for hierarchical
grouping. This is where you chain increasingly small categories into the sort key. This is easier
explained with examples.
If you have a table containing store information for stores that you sell your products to.
Partition Key Store Brand
Sort Key {country}#{state}#{city}#{town/area}#{ID}
This way you can query make these queries:
Request Query
All Target in New York PartitionKey = Target AND begins_with(SortKey, USA#NY)
All Wallmart in Seattle PartitionKey = Wallmart AND begins_with(SortKey,
USA#WA#seattle)
All Wallmart in Beacon Hill, Seattle PartitionKey = Wallmart AND begins_with(SortKey,
USA#WA#seattle#beacon_hill)
A specific Target that is in Roxhil, PartitionKey = Wallmart AND SortKey =
Seattle USA#WA#seattle#roxhill#02349283
Using this you are able to get groups of data but still get a specific item of information.
One Table - Multiple stored datatypes
In another way to use compound keys are to use them to store multiple types of data in a single
table.
An example would be if you are storing data about a customer, traditionally you would have a
table for each type of data you are storing. A table for addresses, a table for orders, etc. With
Dynamo, you can have this all in one table if you create your Sort key correctly.
Partition Key Customer ID
Sort Key {DataType}#{date}#{ID}
This would end up with a table like this:
Partition Key Sort Key Data… (more fields)
12345 address#0321 ....
12345 order#1593115224#10352 ....
12345 order#1597115224#10367 ...
12345 order#1599115224#10394 ...
12345 complaint#1597815224#0031 ...
12345 complaint#1599995224#0031 ...
.... ... ...
You could then do queries like this:
Request Query Result
Get all orders for user 12345 PartitionKey = 12345 AND begins_with(SortKey, 3 orders
order)
Get all orders for 12345 in PartitionKey = 12345 AND SortKey BETWEEN 1 order
August 1596236400 AND 1598914800
Get order 10367 PartitionKey = 12345 AND SortKey = 10367 1 order
Get all details for user 12345 PartitionKey = 12345 1 address, 3
orders, 2
complaints