kartik
kartik

Reputation: 2135

How to auto scale Amazon DynamoDB throughput?

Amazon DynamoDB doesn’t provide inbuild capabilities to auto tune throughput based on Dynamic Load. It provide API to increase or Decrease throughput. Customers are being charges hourly basis for provisioned read & write throughput.

What are the different ways to alter throughput of dynamodb and achieve cost saving benefits ?

Upvotes: 31

Views: 17628

Answers (11)

Zanon
Zanon

Reputation: 30800

AWS added native auto scaling support for DynamoDB in June 2017. The following code (source) provides an example of how to configure auto scaling using the Java SDK:

package com.amazonaws.codesamples.autoscaling;

import com.amazonaws.services.applicationautoscaling.AWSApplicationAutoScalingClient;
import com.amazonaws.services.applicationautoscaling.model.DescribeScalableTargetsRequest;
import com.amazonaws.services.applicationautoscaling.model.DescribeScalableTargetsResult;
import com.amazonaws.services.applicationautoscaling.model.DescribeScalingPoliciesRequest;
import com.amazonaws.services.applicationautoscaling.model.DescribeScalingPoliciesResult;
import com.amazonaws.services.applicationautoscaling.model.MetricType;
import com.amazonaws.services.applicationautoscaling.model.PolicyType;
import com.amazonaws.services.applicationautoscaling.model.PredefinedMetricSpecification;
import com.amazonaws.services.applicationautoscaling.model.PutScalingPolicyRequest;
import com.amazonaws.services.applicationautoscaling.model.RegisterScalableTargetRequest;
import com.amazonaws.services.applicationautoscaling.model.ScalableDimension;
import com.amazonaws.services.applicationautoscaling.model.ServiceNamespace;
import com.amazonaws.services.applicationautoscaling.model.TargetTrackingScalingPolicyConfiguration;

public class EnableDynamoDBAutoscaling {

    static AWSApplicationAutoScalingClient aaClient = new AWSApplicationAutoScalingClient();

    public static void main(String args[]) {

        ServiceNamespace ns = ServiceNamespace.Dynamodb;
        ScalableDimension tableWCUs = ScalableDimension.DynamodbTableWriteCapacityUnits;
        String resourceID = "table/TestTable";

        // Define the scalable target
        RegisterScalableTargetRequest rstRequest = new RegisterScalableTargetRequest()
            .withServiceNamespace(ns)
            .withResourceId(resourceID)
            .withScalableDimension(tableWCUs)
            .withMinCapacity(5)
            .withMaxCapacity(10)
            .withRoleARN("SERVICE_ROLE_ARN_GOES_HERE");

        try {
            aaClient.registerScalableTarget(rstRequest);
        } catch (Exception e) {
            System.err.println("Unable to register scalable target: ");
            System.err.println(e.getMessage());
        }

        // Verify that the target was created
        DescribeScalableTargetsRequest dscRequest = new DescribeScalableTargetsRequest()
            .withServiceNamespace(ns)
            .withScalableDimension(tableWCUs)
            .withResourceIds(resourceID);

        try {
            DescribeScalableTargetsResult dsaResult = aaClient.describeScalableTargets(dscRequest);
            System.out.println("DescribeScalableTargets result: ");
            System.out.println(dsaResult);
            System.out.println();
        } catch (Exception e) {
            System.err.println("Unable to describe scalable target: ");
            System.err.println(e.getMessage());
        }

        System.out.println();

        // Configure a scaling policy
        TargetTrackingScalingPolicyConfiguration targetTrackingScalingPolicyConfiguration = 
            new TargetTrackingScalingPolicyConfiguration()
            .withPredefinedMetricSpecification(
                new PredefinedMetricSpecification()
                .withPredefinedMetricType(MetricType. DynamoDBWriteCapacityUtilization))
            .withTargetValue(50.0)
            .withScaleInCooldown(60)
            .withScaleOutCooldown(60);

        // Create the scaling policy, based on your configuration
        PutScalingPolicyRequest pspRequest = new PutScalingPolicyRequest()
            .withServiceNamespace(ns)
            .withScalableDimension(tableWCUs)
            .withResourceId(resourceID)
            .withPolicyName("MyScalingPolicy")
            .withPolicyType(PolicyType.TargetTrackingScaling)
            .withTargetTrackingScalingPolicyConfiguration(targetTrackingScalingPolicyConfiguration);

        try {
            aaClient.putScalingPolicy(pspRequest);
        } catch (Exception e) {
            System.err.println("Unable to put scaling policy: ");
            System.err.println(e.getMessage());
        }

        // Verify that the scaling policy was created
        DescribeScalingPoliciesRequest dspRequest = new DescribeScalingPoliciesRequest()
            .withServiceNamespace(ns)
            .withScalableDimension(tableWCUs)
            .withResourceId(resourceID);

        try {
            DescribeScalingPoliciesResult dspResult = aaClient.describeScalingPolicies(dspRequest);
            System.out.println("DescribeScalingPolicies result: ");
            System.out.println(dspResult);
        } catch (Exception e) {
            e.printStackTrace();
            System.err.println("Unable to describe scaling policy: ");
            System.err.println(e.getMessage());
        }            
    }
}

This code requires that you supply an ARN for a valid Application Auto Scaling service role. Replace SERVICE_ROLE_ARN_GOES_HERE with the actual ARN.

Upvotes: 0

Zanon
Zanon

Reputation: 30800

AWS added native auto scaling support for DynamoDB in June 2017. See the announcement here.

You can configure this using code (Java SDK example), but if you have just a few tables, you can use the Management Console. Click in your table configuration and select the Capacity tab. The following image shows what are your options:

auto scaling configuration

Upvotes: 3

NSA
NSA

Reputation: 6057

Amazon just added autoscaling for dynamodb, see the details here

Upvotes: 14

Jesús Carrera
Jesús Carrera

Reputation: 11425

I just discovered this project that will autoscale up and down your Dynamodb and looks better than Dynamic Dynamo, because it uses Lambda functions rather than EC2 instances:

https://github.com/channl/dynamodb-lambda-autoscale

  • 5 minute setup process
  • Serverless design
  • Flexible code over configuration style
  • Autoscale table and global secondary indexes
  • Autoscale multiple tables
  • Autoscale by fixed settings
  • Autoscale by provisioned capacity utilisation
  • Autoscale by throttled event metrics
  • Optimised for large spikes in usage and hotkey issues by incorporating throttled event metrics
  • Optimised performance using concurrent queries
  • RateLimitedDecrement as imposed by AWS
  • Statistics via 'measured'
  • AWS credential configuration via 'dotenv'
  • Optimised lambda package via 'webpack'
  • ES7 code
  • 100% Flow static type checking coverage

Upvotes: 13

angulito
angulito

Reputation: 31

I added new features to Rockeee Dynamic DynamoDB Lambda. You can see this project:

https://github.com/touchvie/dynamic-dynamodb-lambda

  • Global Secondary Index support
  • Enable/Disable read/write autoscaling in config json file
  • Throttle Events in CloudWatch support
  • Enable/Disable throttle-read/throttle-write checking in config json file
  • Added test to lambda

I hope that it can help you.

Upvotes: 3

johnz
johnz

Reputation: 366

The answer from Chris is an accurate answer. Just to add a few points from prior experience using DynamoDB …

The situation with DynamoDB is different from EC2. The elastic compute service has an API supported directly as a web service by Amazon to allow you to program how to scale up or down according some logic such as how much demand exists. You program this by defining a monitoring threshold and automatically triggering creation or deletion of instances in a group.

Data servers do not work in the same way with triggers to adjust their capacity. But the capacity of DynamoDB is very flexible and may be controlled as Chris has pointed out. The API to provide this is good enough to make one off changes. Or equivalent manual changes from the console.

The different language bindings to program create and update actions with DynamoDB is here …

http://docs.aws.amazon.com/cli/latest/reference/dynamodb/index.html

The important operation to modify capacity is here …

http://docs.aws.amazon.com/cli/latest/reference/dynamodb/update-table.html

So this gives you the ability to make an increase or decrease in the ReadCapacityUnits or WriteCapacityUnits of ProvisionedThroughput.

Which is fine for a predicted or one-off change. But that is not the same thing as a flexibility tool to allow you to trigger the change automatically.

Programmatically, what you are most likely to want to do is to adjust capacity in response to change in utilization in the preceding time interval. In particular you may need to scale up rapidly in response to a surge in demand by defining an appropriate time slot and a lower and upper threshold to trigger.

A more complete solution to achieve this is described here …

https://aws.amazon.com/blogs/aws/auto-scale-dynamodb-with-dynamic-dynamodb/

The solution is maintained by Sebastian Dahlgren and may be found with all instructions at …

https://github.com/sebdah/dynamic-dynamodb

I see that the current release is 1.18.5 which is more recent than when I last used it.

Judging from earlier releases it is simple to configure by means of a dynamodb.conf properties style file …

Having provided credentials and region, the most crucial settings are

  • check-interval — to test throughput in seconds
  • min-provisioned-reads, max-provisioned-reads; reads-upper-threshold, reads-lower-threshold; increase-reads-with, decrease-reads-with — These are all percentages
  • min-provisioned-writes, max-provisioned-writes; writes-upper-threshold, writes-lower-threshold; increase-writes-with, decrease-writes-with — These are all percentages

Is this information up to date?

Well if you look at http://aws.amazon.com/new/ you will see just one additional recent change affecting DynamoDB which affects the documents stored. The entry for Dynamic DynamoDB is the last published entry dealing with scaling actions. So this is the best maintained DynamoDB automatic scaling capability at this time.

Upvotes: 25

Buchi
Buchi

Reputation: 1368

I think other answers have done a great job but I have a different approach to autoscale DynamoDB in an event driven fashion by leveraging CloudWatch alarms and DynamoDB's UpdateTable operation to change provisioned capacity. The following approach not only helps to reduce costs, but to scale up capacity for unexpected loads.

Summary:

Configure CloudWatch alarms on DynamoDB metrics to alert you based on thresholds and push the alerts to an SQS queue via SNS topic. A daemon process which polls SQS queue can process those alerts and change table provisioned capacity using DynamoDB's UpdateTable operation and update CloudWatch alarm thresholds.

Detailed version:

Please be advised that this approach would require 1. Understanding of AWS services like CloudWatch, SNS, SQS 2. Good amount of time for implementing in your favorite programming language 3. Maintaining a daemon to process SQS messages and change the provisioned capacity.

One time setup:

  1. Create CloudWatch alarms on ConsumedWriteCapacityUnits and ConsumedReadCapacityUnits metrics of your DynamoDB table. You can use this documentation.
  2. Configure the CloudWatch alarms to alert a SNS topic. Create an AWS SQS queue and subscribe the queue to receive alerts from SNS topic.
  3. Write a daemon in any programming language to poll SQS queue and process all alerts. AWS has SDKs in multiple languages so choosing any of those languages would avoid writing lot of code to communicate with AWS services.

Daemon algorithm:

  1. For every SQS message it receives, calculate the new provisioned capacity to be used and issue an UpdateTable operation with the new value.
  2. Update the CloudWatch alarm with the new thresholds, if required.

You can use above approach to either scale up or down. For example, maintain CloudWatch alarm threshold at 80% of ProvisionedWriteCapacityUnits and every time the usage crosses 80%, increase the capacity and set alarm threshold to 80% of new value. Similarly you can scale down when the consumption falls below x%.

Though this is the crux, there would be lot of points to be considered in a production quality solution.

  1. Understand about DynamoDB partitions and hot key problems.
  2. Be aware of all DynamoDB limits.
  3. Constraints on no.of scale downs per UTC day.
  4. Batching the multiple UpdateTable operations.

Finally, Neptune.io provides a packaged SaaS solution to autoscale DynamoDB by using this architecture. See http://blog.neptune.io/one-click-autoscaling-of-dynamodb/ and http://blog.neptune.io/dos-and-donts-of-dynamodb-autoscaling/ for some reading on that.

P.S: I work for Neptune. And, I can help you if you need more details of implementation.

Upvotes: 4

Robert Mao
Robert Mao

Reputation: 1919

Jeff Bar recently wrote a blog in AWS official blog: "Auto Scale DynamoDB With Dynamic DynamoDB":

https://aws.amazon.com/blogs/aws/auto-scale-dynamodb-with-dynamic-dynamodb/

He introduced Dynamic DynamoDB, an open source tool built by independent developer to handle this automatically with CloudFormation template.

Upvotes: 4

quodlibet
quodlibet

Reputation: 452

Now that AWS has announced scheduled execution of lambda services, these seem a great fit to do time-based auto scaling. I wrote up an example of how to use this on medium. Example code is on github.

Upvotes: 2

kartik
kartik

Reputation: 2135

Guidelines for DynamoDB Auto Scaling Script :

Customers are being charged on hourly basis for provisioned read & write throughput. Below is Amazon Dynamo DB Pricing for EU (Ireland Region).

• Write Throughput: $0.00735 per hour for every 10 units of Write Capacity • Read Throughput: $0.00735 per hour for every 50 units of Read Capacity

Amazon Dynamo DB doesn’t provide in-build capabilities to auto tune throughput based on Dynamic Load. It provides API to increase or Decrease throughput with some restrictions like throughput can be decreased twice in a day and increased any time in a day.

What will be the monthly bill of a Production Table for fixed read capacity 2,000 read/second and 2,000 write/second for 24 hours?

Calculation: $0.00735 X 24hrs X 200 X 30days {write cost for month} + $0.00735X 24hrs X 40 X 30 days {read cost for month} = 1058.4+ 211.68 = Fixed 1270 $/month.

Guidelines for writing utility {amazon supported programming languages} which adjust throughput of table and reduces monthly bills.

(A) Initial Value: Basically, here you have to watch and decide read & write throughput for table as an initialization value after analyzing average usage considering 15 days or 1 month load and add X% extra for read and Y% extra for write on the top to withstand unexpected load. Initial read/write throughput = calculate read throughput based on average usage +X {read} % or Y {write} % X & Y can be anything between 10% and 30% based on observation.

(B) Peak Load Shaping: Alert on tables can be set as when load reaches to 50% to 60 % of provisioned throughput, necessary action can be taken like calling throughput increment API to increase throughput anything between 30 % to 50% of provision throughput.*

(C) Manual Shaping: For known heavy load like batch load/festival season, throughput should be set manually to 200% - 300% extra of normal daily operations until load is complete* * Once business working hours or load is over. Throughput should reduce down to initial value.

Note: Reader can calculate monthly saving considering 1,000 read/write for 16 hrs. + 2,000 read/write for 8 hours, provided utility in place.

Upvotes: 2

Krease
Krease

Reputation: 16225

You can manage throughput programmatically through the updateTable API or manually through the console.

There's also tools like Dynamic DynamoDB, though you could roll your own version as well: you'd use the updateTable API and have some background process running to detect those circumstances and call updateTable as necessary.

Some things to watch out for when changing the scale of DynamoDB:

  1. You get billed for allocated throughput, whether you're actually using it or not.
  2. Wen you scale up, Dynamo may allocate new partitions for you - but it won't remove them when it scales down. This can result in unexpected hot hash key problem where you have a lot of partitions but very low throughput on each of them.

Upvotes: 8

Related Questions