Capacity & Autoscaling in AWS DynamoDB
Introduction
AWS DynamoDB is a fully managed NoSQL database service that provides fast and predictable performance with seamless scalability. Understanding capacity and autoscaling is essential for optimizing performance and cost.
Understanding Capacity
DynamoDB allows you to specify the read and write capacity units (RCUs and WCUs) for your tables. You can choose between:
- **Provisioned Capacity**: You set the number of RCUs and WCUs.
- **On-Demand Capacity**: DynamoDB automatically adjusts the capacity based on traffic.
Provisioned Capacity
When you set a provisioned capacity, you are allocating a specific number of units for both reads and writes. For example:
# Setting up provisioned capacity using AWS CLI
aws dynamodb create-table \
--table-name MyTable \
--attribute-definitions AttributeName=Id,AttributeType=S \
--key-schema AttributeName=Id,KeyType=HASH \
--provisioned-throughput ReadCapacityUnits=5,WriteCapacityUnits=5
On-Demand Capacity
In on-demand mode, DynamoDB automatically allocates the necessary capacity for your workload. You can switch to on-demand from provisioned capacity:
# Updating table to on-demand capacity
aws dynamodb update-table \
--table-name MyTable \
--billing-mode PAY_PER_REQUEST
Autoscaling
Autoscaling allows you to automatically adjust the provisioned capacity of your DynamoDB tables based on the usage patterns.
Enabling Autoscaling
To enable autoscaling, you need to define a target utilization percentage and specify minimum and maximum capacity limits.
# Enabling autoscaling using AWS CLI
aws application-autoscaling register-scalable-target \
--service-namespace dynamodb \
--resource-id table/MyTable \
--scalable-dimension dynamodb:table:ReadCapacityUnits \
--min-capacity 1 \
--max-capacity 10
aws application-autoscaling put-scaling-policy \
--service-namespace dynamodb \
--resource-id table/MyTable \
--scalable-dimension dynamodb:table:ReadCapacityUnits \
--policy-name MyReadCapacityScalingPolicy \
--policy-type TargetTrackingScaling \
--target-tracking-scaling-policy-configuration '{
"TargetValue": 70.0,
"PredefinedMetricSpecification": {
"PredefinedMetricType": "DynamoDBReadCapacityUtilization"
},
"ScaleInCooldown": 60,
"ScaleOutCooldown": 60
}'
Best Practices
Here are some best practices to consider:
- Monitor your usage patterns continuously.
- Set appropriate minimum and maximum limits for autoscaling.
- Use CloudWatch alarms to track capacity usage.
- Regularly review and adjust your capacity settings.
FAQ
What is the difference between RCUs and WCUs?
RCUs are used for reading data from DynamoDB, while WCUs are used for writing data to DynamoDB. 1 RCU allows you to read one strongly consistent item per second or two eventually consistent items per second.
How does autoscaling help with cost management?
Autoscaling automatically adjusts the capacity based on traffic, which helps to avoid over-provisioning and reduces unnecessary costs.
Can I switch from provisioned to on-demand capacity?
Yes, you can switch from provisioned to on-demand capacity at any time through the AWS Management Console or CLI.