How to Use Auto Scaling in DynamoDB for Efficient Capacity Management
To use
auto scaling in DynamoDB, enable it on your table or global secondary index to automatically adjust read and write capacity units based on demand. You configure scaling policies with target utilization percentages, minimum and maximum capacity limits, and AWS Application Auto Scaling manages the rest.Syntax
Auto scaling in DynamoDB is configured using AWS Application Auto Scaling APIs or the AWS Management Console. The key parts include:
- ResourceId: Identifies the DynamoDB table or index.
- ScalableDimension: Specifies whether to scale read or write capacity.
- MinCapacity and MaxCapacity: Define the allowed capacity range.
- TargetTrackingScalingPolicyConfiguration: Sets the target utilization percentage for capacity.
bash
aws application-autoscaling register-scalable-target \ --service-namespace dynamodb \ --resource-id table/YourTableName \ --scalable-dimension dynamodb:table:WriteCapacityUnits \ --min-capacity 5 \ --max-capacity 100 aws application-autoscaling put-scaling-policy \ --service-namespace dynamodb \ --resource-id table/YourTableName \ --scalable-dimension dynamodb:table:WriteCapacityUnits \ --policy-name WriteCapacityAutoScalingPolicy \ --policy-type TargetTrackingScaling \ --target-tracking-scaling-policy-configuration file://config.json
Example
This example shows how to enable auto scaling for write capacity on a DynamoDB table using AWS SDK for Python (boto3). It registers the scalable target and sets a target tracking policy to keep write capacity at 70% utilization.
python
import boto3 client = boto3.client('application-autoscaling') resource_id = 'table/YourTableName' scalable_dimension = 'dynamodb:table:WriteCapacityUnits' # Register scalable target client.register_scalable_target( ServiceNamespace='dynamodb', ResourceId=resource_id, ScalableDimension=scalable_dimension, MinCapacity=5, MaxCapacity=100 ) # Put scaling policy client.put_scaling_policy( PolicyName='WriteCapacityAutoScalingPolicy', ServiceNamespace='dynamodb', ResourceId=resource_id, ScalableDimension=scalable_dimension, PolicyType='TargetTrackingScaling', TargetTrackingScalingPolicyConfiguration={ 'TargetValue': 70.0, 'PredefinedMetricSpecification': { 'PredefinedMetricType': 'DynamoDBWriteCapacityUtilization' }, 'ScaleOutCooldown': 60, 'ScaleInCooldown': 60 } ) print('Auto scaling enabled for write capacity on YourTableName')
Output
Auto scaling enabled for write capacity on YourTableName
Common Pitfalls
Common mistakes when using DynamoDB auto scaling include:
- Not setting appropriate MinCapacity and MaxCapacity limits, which can cause throttling or overspending.
- Using too high or too low TargetValue for utilization, leading to unstable scaling.
- Forgetting to enable auto scaling on both the table and its global secondary indexes if needed.
- Not allowing enough cooldown time, causing rapid scaling up and down.
python
## Wrong: No min/max capacity limits client.register_scalable_target( ServiceNamespace='dynamodb', ResourceId='table/YourTableName', ScalableDimension='dynamodb:table:WriteCapacityUnits', MinCapacity=0, # Too low MaxCapacity=1000 # Too high ) ## Right: Balanced min/max capacity client.register_scalable_target( ServiceNamespace='dynamodb', ResourceId='table/YourTableName', ScalableDimension='dynamodb:table:WriteCapacityUnits', MinCapacity=5, MaxCapacity=100 )
Quick Reference
| Parameter | Description | Example |
|---|---|---|
| ServiceNamespace | The AWS service to scale | dynamodb |
| ResourceId | Table or index identifier | table/YourTableName |
| ScalableDimension | Capacity type to scale | dynamodb:table:ReadCapacityUnits |
| MinCapacity | Minimum capacity units | 5 |
| MaxCapacity | Maximum capacity units | 100 |
| TargetValue | Target utilization percentage | 70.0 |
| ScaleOutCooldown | Cooldown seconds after scaling out | 60 |
| ScaleInCooldown | Cooldown seconds after scaling in | 60 |
Key Takeaways
Enable auto scaling on your DynamoDB table or index to adjust capacity automatically.
Set sensible minimum and maximum capacity limits to avoid throttling or overspending.
Use target tracking policies with a utilization target around 70% for stable scaling.
Remember to configure cooldown periods to prevent rapid scaling fluctuations.
Apply auto scaling to both tables and global secondary indexes if needed.