DynamoDB CRUD operations and Query vs Scan
Using Data Stores in AWS
Dunieski Otano
AWS Solutions Architect
The expensive mistake
- Developer uses Scan to find one user
- Reads entire 1 million item table
- Bill: $500 for a single lookup
- Query would have cost $0.01
CRUD operations in dynamoDB
- PutItem: Create or replace item
- GetItem: Read item by primary key
- UpdateItem: Modify specific attributes
- DeleteItem: Remove item
- Query: Retrieve items by partition key (efficient)
- Scan: Read entire table (expensive)
Query vs Scan: the critical difference
- Query: Requires partition key, reads specific items
- Fast, efficient, low cost
- Scan: Reads entire table, filters after
- Slow, expensive, avoid when possible
- Rule: If you know the key, Query. Otherwise, rethink your design.
Preventing race conditions with conditional expressions
- Problem: Two users update same item simultaneously
- Solution: Conditional expressions
- Example: Update only if current value matches expected
UpdateItem expressions: modifying attributes
- SET: Set attribute value or create new attribute
- ADD: Increment/decrement numbers, add to sets
- REMOVE: Delete attributes from item
- DELETE: Remove elements from sets
Pagination with Query and Scan
- Problem: Large result sets exceed 1MB limit
- Solution: Pagination with LastEvaluatedKey
- Limit: Control items per page
- Continue until LastEvaluatedKey is null
Let's practice!
Using Data Stores in AWS
Preparing Video For Download...