It’s a bad idea to model your data in DynamoDB the same way you model your data in a relational database. The entire point of using a NoSQL datastore is to get some benefit you couldn’t get with a relational database. If you model the data in the same way, you not only won’t get that benefit but you will also end up with a solution that’s worse than using the relational database!
from Alex DeBrie: The DynamoDB book
-
Primary key:
- Simple primary key (partition key) or
- Composite primary key (partition key + sort key)
- primary keys are critical: all your access patterns will be driven off your primary key; really think about your primary keys
- for item based actions (write, update, delete) you must provide the entire primary key (if composite both partition key and sort key)
- for queries must provide primary, sort key is optional
-
Secondary Indexes
- define as required/needed by access patterns
-
- Start with an ERD (Entity Relationship Diagram)
- what entities do exist in the application and how do they relate to each other (one-to-one, one-to-many, etc)
- Entities
- Relationships
- Attributes
- Collections?
-
- Define your access patterns
- how will the entities be used, how will they be fetched, how will they be manipulated...write down the access patterns
- examples:
- Shopping: Get user profile, Get single order and order items, Get orders for user by status
- Social: Follow User, View Followers for User
-
- Design your primary keys & secondary indexes (local/global)
- can be tricky to start; however, don't be afraid to iterate on this at this stage
- try to start with a 'core' entity (e.g. Order or User for a shopping system)
- define/map how the access patterns are addressed; see also 'Filtering patterns' in slides/video
- Denormalize
- Build collections (set of items same partition key but different sort key)
- Primary Patterns in Tables (can write)
- Secondary Pattrerns in GSIs
- Iterate
- consider e.g. when the number is 'bound' e.g. a user can have a max of five addresses
- example see slide #50
- use case / relationsship 'User has many Orders' (not bound)
- example see slide #54
- applied after key query expression
- First - reads the entiry set / all the data
- Second - filters the data / applies
- consumption (cost!) associated to 1.
- bulk of 'filtering' should be done using keys
- 'Normalization' - 3rd normal form etc...forget about it; denormalize so it's in the shape you want / need it
- 'JOINs': do not exist
- One entity type per table vs multiple entities/types per table in DynamoDB
- e.g. Primary Key 'PK, Sort Key 'SK' as generic names and then prefix values with "USER#joe" (PK) and "#PROFILE" (SK) for user item
- consider doing an entity chart (approx at 22:30 in below video, slide #46)
- Video AWS re:Invent 2019: Data modeling with Amazon DynamoDB (CMY304)
- Notes above are mostly based on this session; highly recommended to watch this session
- Slides PDF - AWS re:Invent 2019: Data modeling with Amazon DynamoDB (CMY304)
- Web page DynamoDB, explained
- Model a DynamoDB Database for a Mobile App - AWS Virtual Workshop
- AWS Workshop and Lab Content Portal for Amazon DynamoDB