DynamoDB
What is Amazon DynamoDB?
Amazon DynamoDB is a fully managed NoSQL database service that provides fast and predictable performance with seamless scalability. It supports both document and key-value store models, and is designed for applications that need consistent, single-digit millisecond latency at any scale. DynamoDB handles all the administrative burdens of operating and scaling a distributed database.
Key Features
- Serverless: No servers to manage or provision
- Scalability: Automatic scaling based on throughput requirements
- Performance: Single-digit millisecond latency
- Durability: Data stored across multiple Availability Zones
- Security: Encryption at rest and in transit
- Integration: Works with other AWS services
Prerequisites
- AWS Account
- Understanding of NoSQL database concepts
- Knowledge of partition keys and sort keys
- IAM permissions for DynamoDB operations
- Basic understanding of read/write capacity units
Step-by-Step: Creating a DynamoDB Table
- Sign in to AWS Management Console: Go to console.aws.amazon.com
- Navigate to DynamoDB: Search for "DynamoDB" in services
- Create Table: Click "Create table" button
- Table Details:
- Table name: Enter a unique table name
- Partition key: Primary key attribute (required)
- Sort key: Optional secondary key for composite primary key
- Table Settings:
- Capacity mode: On-demand or Provisioned
- Read/write capacity: For provisioned mode (RCUs/WCUs)
- Auto scaling: Enable for provisioned mode
- Secondary Indexes:
- Global Secondary Index (GSI): For different access patterns
- Local Secondary Index (LSI): Same partition key, different sort key
- Encryption: Enable encryption at rest (default: AWS owned key)
- Tags: Add key-value pairs for organization
- Create Table: Review and create the table
Adding Items to DynamoDB Table
- Navigate to Table: Select your table from the Tables list
- Create Item: Click "Create item" or "Explore items" → "Create item"
- Enter Attributes:
- Partition key (required)
- Sort key (if defined)
- Additional attributes as needed
- Data Types: Choose appropriate types (String, Number, Boolean, etc.)
- Save Item: Click "Create item" to save
Querying and Scanning Data
- Query Operation: Use partition key and optional sort key conditions
- Scan Operation: Examine all items in the table (expensive, use sparingly)
- Use Indexes: Query GSIs and LSIs for different access patterns
- Filter Expressions: Apply additional filtering on results
- Pagination: Handle large result sets with pagination
Best Practices
- Design partition keys for even distribution
- Use sort keys to enable range queries
- Choose appropriate capacity mode (On-demand vs Provisioned)
- Use GSIs for alternative access patterns
- Implement proper error handling and retries
- Monitor performance with CloudWatch metrics
- Use DynamoDB Streams for change data capture
- Implement backup and restore strategies
Capacity Modes
- On-Demand: Pay per request, automatic scaling
- Provisioned: Set RCUs/WCUs, cost-effective for predictable workloads
- Auto Scaling: Automatically adjust capacity based on usage
Data Types
- Scalar Types: String, Number, Boolean, Null, Binary
- Document Types: List, Map
- Set Types: String Set, Number Set, Binary Set
Common Use Cases
- Serverless web applications
- Real-time gaming applications
- IoT data ingestion and processing
- User session management
- Shopping cart and user preferences
- Time-series data storage
- Metadata storage for S3 objects