Best practices when working with DynamoDB. Based on very good course from A Cloud Guru https://acloud.guru/learn/aws-dynamodb.
Tables Best Practice
- Understand your data. How, what and when it’s accessed. Understand clients of the data. How are they using it? Think about keys and identifiers. Are there pick usage?
- Do your applications and users care about strong consistency?
- Understand DynamoDB partitions. How db scale and create new partitions. Plan number of partitions for the whole lifecycle of your application
- Choose a good partition key — aim for key and time uniformity
- In case you don’t have good partition key, create one
- DynamoDB provides burst performance — try not to use it
- Uploading data — distribute and don’t over-provision
- Split data based on access frequency, patterns of data, archive
- Store your binary data in s3 not in DynamoDB
- Test at scale, use caching
- Don’t use counter tables, use UUID for ids
Items Best Practice
- Avoid set attributes where possible — consider one-to-many tables
- Avoid large items if possible — minimum aggregate
- Compress large attribute prior to writing
- Store your binary data in S3 rather then binary data types
- Split large attributes into chunks as separate items
- Split groups of attribute into multiple tables
- Use IAM access control to offer security profiles
Indexes
- Indexes don’t come for free, there is a cost for read and writes
- Cost for creating an index vs scan operation
- Consider projections, be sure they are suitable
- Always optimise
- Use sparse indexes
- ItemCollections — monitor, do not use unless there are needed
- Separate WCU and RCU for GSI
- GSI’s quick lookups — item subset or readreplica-like mechanic
Items info
- Max Partition key attribute value length is 2048 bytes
- Max Sort key attribute value length is 1024 bytes
- Most attribute data types do not have a max size
- Item size — binary length
- Max item size is 400KB
- Unlimited items in a table, not recommended
AWS documentation
https://docs.aws.amazon.com/amazondynamodb/latest/developerguide/best-practices.html