How to Reduce DynamoDB Cost: Practical Tips and Examples
To reduce
DynamoDB cost, optimize your table's capacity mode by using on-demand or provisioned with auto-scaling, and minimize read/write units by efficient data modeling and indexing. Also, use TTL to delete unused data automatically and avoid unnecessary scans or queries.Syntax
Here are key DynamoDB settings and features to control cost:
- Capacity Modes:
PROVISIONED(set read/write units) orPAY_PER_REQUEST(pay per request). - Auto Scaling: Automatically adjusts provisioned capacity based on traffic.
- Time To Live (TTL): Automatically deletes expired items to save storage cost.
- Indexes: Use
Global Secondary Indexes (GSI)andLocal Secondary Indexes (LSI)wisely as they consume additional capacity.
bash
aws dynamodb update-table --table-name YourTableName --billing-mode PROVISIONED --provisioned-throughput ReadCapacityUnits=5,WriteCapacityUnits=5 aws dynamodb update-table --table-name YourTableName --billing-mode PAY_PER_REQUEST aws dynamodb update-time-to-live --table-name YourTableName --time-to-live-specification Enabled=true,AttributeName=ttl
Example
This example shows how to create a DynamoDB table with provisioned capacity and enable TTL to reduce costs by deleting old data automatically.
python
import boto3 # Create DynamoDB client client = boto3.client('dynamodb') # Create table with provisioned capacity client.create_table( TableName='CostOptimizedTable', KeySchema=[{'AttributeName': 'id', 'KeyType': 'HASH'}], AttributeDefinitions=[{'AttributeName': 'id', 'AttributeType': 'S'}], BillingMode='PROVISIONED', ProvisionedThroughput={'ReadCapacityUnits': 5, 'WriteCapacityUnits': 5} ) # Enable TTL on attribute 'expireAt' client.update_time_to_live( TableName='CostOptimizedTable', TimeToLiveSpecification={'Enabled': True, 'AttributeName': 'expireAt'} ) print('Table created with provisioned capacity and TTL enabled.')
Output
Table created with provisioned capacity and TTL enabled.
Common Pitfalls
Many users overspend by:
- Using
ON_DEMANDmode for consistently high traffic instead ofPROVISIONEDwith auto-scaling. - Creating too many or unnecessary indexes that increase write costs.
- Performing full table scans instead of targeted queries, which consume more read capacity.
- Not enabling TTL, causing storage of stale data and higher storage costs.
Correcting these can significantly reduce your bill.
python
Wrong way (excessive scans):
response = table.scan()
Right way (targeted query):
response = table.query(
KeyConditionExpression=Key('id').eq('123')
)Quick Reference
| Tip | Description |
|---|---|
| Choose Capacity Mode | Use PROVISIONED with auto-scaling for steady traffic, PAY_PER_REQUEST for unpredictable traffic. |
| Enable TTL | Automatically delete expired items to save storage costs. |
| Limit Indexes | Create only necessary GSIs and LSIs to reduce write costs. |
| Optimize Queries | Use queries over scans to reduce read capacity usage. |
| Batch Operations | Use batch writes and reads to reduce request overhead. |
Key Takeaways
Use provisioned capacity with auto-scaling for predictable workloads to save costs.
Enable TTL to automatically remove expired data and reduce storage charges.
Avoid unnecessary indexes as they increase write costs.
Prefer queries over scans to minimize read capacity consumption.
Batch your read and write operations to optimize request efficiency.