DynamoDB¶
Introduction to DynamoDB¶
Amazon DynamoDB is a fully managed, serverless, key-value NoSQL database designed for high-performance, scalable applications. Developed by Amazon Web Services, it provides seamless and consistent single-digit millisecond latency at any scale, making it an ideal choice for modern cloud-native and distributed applications.
Core Architectural Concepts¶
Table Structure¶
DynamoDB organizes data in tables, which are collections of items sharing a similar structure. Each item in a table is identified by a primary key, which can be simple (partition key) or composite (partition key and sort key). This design enables efficient data retrieval and supports complex querying strategies.
Primary Key Types¶
- Simple Primary Key: Consists of only a partition key, ensuring unique identification of items within the table.
- Composite Primary Key: Combines a partition key with a sort key, allowing multiple items to share the same partition key while maintaining unique identification through the sort key combination.
Capacity Unit Calculations¶
Read Capacity Units (RCUs)¶
RCUs represent the number of reads per second for items up to 4 KB in size.
Calculation Formula:
Strongly Consistent RCUs = (Size of Item / 4 KB) × Number of Reads per Second
Eventual Consistent RCUs = (Size of Item / 4 KB) × Number of Reads per Second × 0.5
Read Capacity Examples:¶
- 4 KB item, 1 strongly consistent read/second: 1 RCU
- 4 KB item, 1 eventual consistent read/second: 0.5 RCU
- 8 KB item, 1 strongly consistent read/second: 2 RCUs
- 8 KB item, 1 eventual consistent read/second: 1 RCU
- 4 KB item, 10 strongly consistent reads/second: 10 RCUs
- 4 KB item, 10 eventual consistent reads/second: 5 RCUs
Write Capacity Units (WCUs)¶
WCUs represent the number of writes per second for items up to 1 KB in size.
Calculation Formula:
WCUs = (Size of Item / 1 KB) × Number of Writes per Second
Write Capacity Examples:¶
- 1 KB item, 1 write/second: 1 WCU
- 2 KB item, 1 write/second: 2 WCUs
- 1 KB item, 10 writes/second: 10 WCUs
Practical Capacity Planning¶
- Estimate average item size
- Determine peak read/write requirements
- Calculate base RCUs and WCUs
- Add buffer for unexpected traffic
- Consider using auto-scaling
Data Consistency and Pricing¶
Consistency Models¶
Eventual Consistent Reads¶
- Default read model in DynamoDB
- Consumes 0.5 Read Capacity Units (RCUs) per 4 KB
- Typical cost: Approximately 50% cheaper than strong consistent reads
- Reflects changes within 1 second across database replicas
Strong Consistent Reads¶
- Guarantees most recent write
- Consumes 1 Read Capacity Unit (RCU) per 4 KB
- Provides immediate data consistency
- Approximately double the cost of eventual consistent reads
Detailed Cost Breakdown¶
Read Capacity Unit Pricing¶
- Eventual Consistent Reads: $0.25 per million read request units
- Strong Consistent Reads: $0.50 per million read request units
- On-Demand Mode: Pricing varies by region and request volume
- Provisioned Mode: Predictable pricing based on pre-allocated capacity
Write Capacity Pricing¶
- Standard Write Units: $0.47 per million write request units
- Pricing varies by region and specific AWS configuration
Storage Costs¶
- First 25 TB per month: $0.25 per GB
- Over 25 TB: Reduced rates apply
- Incremental storage charges for backups and global tables
Data Model and Attributes¶
Items in DynamoDB can contain attributes of various types, including: - String - Number - Binary - Boolean - List - Map - String Set - Number Set - Binary Set
Each attribute supports flexible schema design, enabling developers to adapt data structures without extensive migrations.
Performance and Scaling¶
Read/Write Capacity Modes¶
DynamoDB offers two capacity modes to manage performance and cost:
Provisioned Mode¶
Developers specify expected read and write capacity units in advance. The system allocates dedicated resources to maintain performance, with options for manual or auto-scaling adjustments.
On-Demand Mode¶
Automatically scales to accommodate varying workloads without pre-planning capacity. Ideal for unpredictable traffic patterns and applications with sporadic access patterns.
Secondary Indexes¶
Global Secondary Indexes (GSI)¶
GSIs provide alternative query paths across the entire table, independent of the primary key. Key characteristics include: - Can be created on any table attribute - Support different partition and sort keys from the base table - Consume additional read capacity units - Enable complex querying strategies beyond the primary key
Local Secondary Indexes (LSI)¶
LSIs share the table’s partition key but offer alternative sort key configurations. Distinguishing features: - Created during table creation - Limited to five per table - Use the same partition key as the base table - Consume storage from the base table’s provisioned capacity
Data Consistency and Replication¶
Consistency Models¶
- Eventually Consistent Reads: Default mode with lower latency
- Strong Consistent Reads: Guarantees retrieval of the most recent write, with slightly higher latency
Global Tables¶
Supports multi-region, multi-master replication, enabling: - Active-active database configurations - Low-latency global access - Automatic conflict resolution
Security and Access Control¶
Authentication and Authorization¶
- Integrates with AWS Identity and Access Management (IAM)
- Granular access controls at table and item levels
- Support for encryption at rest using AWS Key Management Service
Use Cases¶
DynamoDB excels in scenarios requiring: - High-velocity web and mobile applications - Real-time bidding platforms - Gaming leaderboards - IoT data storage - Session management - Metadata caching
Cost Optimization Strategies¶
- Utilize on-demand capacity for unpredictable workloads
- Implement Time-to-Live (TTL) for automatic data expiration
- Use compression and efficient indexing
- Monitor and adjust capacity settings regularly
Limitations and Considerations¶
- Maximum item size: 400 KB
- Maximum attribute name length: 64 KB
- Complex joins not natively supported
- Scan operations can be costly for large datasets
Best Practices¶
- Design with access patterns in mind
- Minimize the number of secondary indexes
- Distribute partition key values evenly
- Use compression for large attributes
- Implement caching layers for read-heavy workloads
Conclusion¶
AWS DynamoDB represents a powerful, flexible NoSQL database solution that combines scalability, performance, and ease of management. By understanding its architectural principles and leveraging its advanced features, developers can build robust, high-performance distributed applications.