In this blog post, we will be learning how to programmatically update the auto-scaling policy settings of a DynamoDB table. The idea is to scale it smoothly (minimal write request throttling) irrespective of the anticipated traffic spikes it receives. We do this using AWS Application Auto Scaling and Lambda (boto3).
Understanding how DynamoDB auto-scales
DynamoDB auto scaling works based on Cloudwatch metrics and alarms built on top of 3 parameters:
- Provisioned capacity
- Consumed capacity
- Target utilization
Let us understand these parameters briefly, Firstly Provisioned capacity is how many units are allocated for the table/index, while Consumed capacity is how much of it is utilized. Lastly, Target utilization is the threshold we specify at how much utilization(%) we want autoscaling to happen. Accordingly provisioned capacity is increased.
Note: Capacity can be for either writing or reading, sometimes both.
Let us say, you provisioned 50 write units for the table:
- Autoscale by 30 units when 40 units (80% ) are consumed. Then your autoscaling policy would look something like this.
Provisioned write capacity: 50 units
Target utilization: 80%
max write units: 10,000
min write units: 30
This auto-scaling policy looks fine yet it is static and can it cope up with unpredictable traffic spikes? If so how much time does it take to autoscale (15min)? What if the increased units were less than required, how do you reduce throttling?
Traffic spikes
Broadly traffic spikes could be categorized into 2 categories:
- Anticipated spike
- Sudden spike
And the traffic fluctuation curve can be any of the following:
- Steady rise and fall.
- Steep peaks and valleys.
Having a single static autoscaling policy that copes up with all the above cases or a combination of them is not possible. DynamoDB auto-scales capacity based on target capacity utilization instead of write requests. This makes it inefficient and slow.
Target capacity utilization is the percentage of consumption over Provisioned. Mathematically put (consumed/provisioned)*100.
If the traffic is unpredictable and you have to understand and improve (tweak) DynamoDB autoscaling’s internal workflow, here is a wonderful blog post that I would highly recommend.
Anticipated traffic is in direct correlation with your actions, be it a scheduled marketing event, data ingestion or even sale transactions on a festive day. In such scenarios, a workaround would be the ability to programmatically update the autoscaling policy.
Updating autoscaling policy using AWS Application Auto Scaling.
AWS has this service called AWS Application Auto Scaling. With Application Auto Scaling, you can configure/update automatic scaling for the following resources:
- Amazon ECS services
- Amazon EC2 Spot Fleet requests
- Amazon EMR clusters
- Amazon AppStream 2.0 fleets
- Amazon DynamoDB tables and global secondary indexes throughput capacity
- Amazon Aurora Replicas
- Amazon SageMaker endpoint variants
- Custom resources provided by your own applications or services
- Amazon Comprehend document classification endpoints
- AWS Lambda function provisioned concurrency
The scaling of the provisioned capacity of these services is managed by the autoscaling policy that is in place. AWS Application Auto Scaling service can be used to modify/update this autoscaling policy.
Here is a sample Lambda (python) code that updates DynamoDB autoscaling settings:
# update-dynamo-autoscale-settings
import os
import sys
import boto3
from botocore.exceptions import ClientError
# Add path for additional imports
sys.path.append('./lib/python3.7/site-packages')
# Initialize boto3 clients
dynamodb = boto3.resource('dynamodb')
dynamodb_scaling = boto3.client('application-autoscaling')
# Initialize variables
table_name = "your dynamo table name"
table = dynamodb.Table(table_name)
def update_auto_scale_settings(min_write_capacity: int, table_name: str):
max_write_capacity = 40000 #default number
dynamodb_scaling.register_scalable_target(ServiceNamespace = "dynamodb",
ResourceId = "table/{}".format(table_name),
ScalableDimension = "dynamodb:table:WriteCapacityUnits",
MinCapacity = min_write_capacity,
MaxCapacity = max_write_capacity)
# if you have indexes on the table, add their names to indexes list below
# indexes = ["index1", "index2", "index3"]
# for index_name in indexes:
# scaling_dynamodb.register_scalable_target(ServiceNamespace = "dynamodb",
# ResourceId = "table/{table_name}/index/{index_name}".format(table_name = table_name, index_name = index_name),
# ScalableDimension = "dynamodb:index:WriteCapacityUnits",
# MinCapacity = min_write_capacity,
# MaxCapacity = max_write_capacity)
#put_scaling policy is another call that needs to be made, if you want a different target utilization value.
def lambda_handler(event, context):
try:
# logic before updating
write_units = #number
update_auto_scale_settings(table_name,write_units)
# logic after updating
# Insert item into dynamo
# table.put_item(Item = record)
except Exception as e:
raise e
else:
print("success")
I hope it was helpful. Thanks for the read!
This story is authored by Koushik. Koushik is a software engineer and a keen data science and machine learning enthusiast.
Comments