NoSQL with DynamoDB
Design DynamoDB tables around access patterns with partition and sort keys, indexes, capacity modes, TTL and streams.
Intermediate · about 6 hours · difficulty 3/5
Learn first: IAM Users, Groups, Roles and Policies
Amazon DynamoDB is a fully managed key-value and document database with single-digit millisecond latency at any scale and no servers to manage. Azure Cosmos DB and Google Firestore or Bigtable are the closest equivalents.
Data model
- A table holds items (rows) made of attributes. There is no fixed schema beyond the key.
- Partition key (required) decides where data lives. Sort key (optional) orders items within a partition, enabling range queries.
- Query reads items by key efficiently. Scan reads the whole table and should be rare.
Indexes
- Global Secondary Index (GSI): a different partition and sort key over the same data, for a second access pattern.
- Local Secondary Index (LSI): same partition key, different sort key; must be created with the table.
Design approach
Start from access patterns, not entities. List every query the app needs ("get orders for a user by date", "get order by ID"), then design keys and GSIs to serve each with a Query. Single-table design packs several entity types into one table using generic keys like PK and SK.
Operations
- On-demand capacity for unpredictable traffic; provisioned with auto scaling for steady, cost-sensitive workloads.
- TTL deletes expired items for free.
- Streams feed changes to Lambda for event-driven processing.
- Point-in-time recovery and on-demand backups.
Pitfalls
- Hot partitions from low-cardinality keys like "status".
- Treating DynamoDB like SQL and scanning with filters.
You are ready to move on when you can design keys for a URL shortener or an order system and serve every access pattern with a Query.
Key concepts
- Tables, items, attributes
- Partition and sort keys
- Query vs Scan
- GSIs and LSIs
- Access-pattern-first design
- On-demand vs provisioned capacity
- TTL and Streams
- Single-table design
- DAX caching
Mini project
Design and build a DynamoDB table for a college event-registration app: get events by date, get registrations by student, get registrations for an event. Write the access patterns first, then load sample data with a script and query each pattern. Use on-demand mode with a few hundred items to stay in free tier, and delete the table after.
Check yourself
Why is choosing 'country' as the partition key for an Indian user table a bad idea?
Free resources
- Amazon DynamoDB documentation (AWS)
- Best practices for designing with DynamoDB (AWS)
- The Open Guide to AWS: DynamoDB (open-guides)