0
0
DynamodbHow-ToBeginner · 4 min read

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

ParameterDescriptionExample
ServiceNamespaceThe AWS service to scaledynamodb
ResourceIdTable or index identifiertable/YourTableName
ScalableDimensionCapacity type to scaledynamodb:table:ReadCapacityUnits
MinCapacityMinimum capacity units5
MaxCapacityMaximum capacity units100
TargetValueTarget utilization percentage70.0
ScaleOutCooldownCooldown seconds after scaling out60
ScaleInCooldownCooldown seconds after scaling in60

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.