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

mistake

Using Data Stores in AWS

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)

ops

Using Data Stores in AWS

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.

scan

Using Data Stores in AWS

Preventing race conditions with conditional expressions

  • Problem: Two users update same item simultaneously
  • Solution: Conditional expressions
  • Example: Update only if current value matches expected

race

Using Data Stores in AWS

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

expressions

Using Data Stores in AWS

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

pagination

Using Data Stores in AWS

Let's practice!

Using Data Stores in AWS

Preparing Video For Download...