AWS Database Blog

Optimize costs with scheduled scaling of Amazon DocumentDB for read workloads

In this post, we show you two ways to schedule the scaling of your Amazon DocumentDB (with MongoDB compatibility) instance-based clusters to address anticipated read traffic patterns.

Amazon DocumentDB is a fully managed native JSON document database that makes it straightforward and cost-effective to operate critical document workloads at virtually any scale without managing infrastructure. Amazon DocumentDB simplifies your architecture by providing built-in security best practices, continuous backups, and native integrations with other AWS services. You can enhance your applications with generative artificial intelligence (AI) and machine learning (ML) capabilities using vector search for Amazon DocumentDB and integration with Amazon SageMaker Canvas. As a document database, Amazon DocumentDB makes it straightforward to store, query, and index JSON data.

To learn more about how Amazon Document automatically distributes reads across available replicas in the cluster, see Scaling Amazon DocumentDB (with MongoDB compatibility), Part 1: Scaling reads

By aligning your Amazon DocumentDB cluster scaling operations with the anticipated read traffic patterns, you can achieve optimal performance during peak loads and save costs by reducing the need to overprovision your cluster. Amazon DocumentDB provides per-second billing for compute instances, with a 10-minute minimum billing period, so you pay only for what you use. For more information, see Amazon DocumentDB pricing.

Solution overview

You can use the following methods to scale your Amazon DocumentDB cluster based on a schedule:

  • Scheduled scaling with multiple replicas – With this approach, you can smoothly handle high read traffic by scaling out your Amazon DocumentDB cluster during peak times with a predefined number of instances and scaling in to remove additional capacity during off-peak hours to save costs. As an example use case, consider that you manage an e-learning platform or a similar platform where your read traffic peaks during school or business hours and drops significantly during nonbusiness hours and weekends. In such scenarios, your need to scale out your platform just before the start of business hours and scale in after business hours to save costs.
  • Targeted replica – This approach adds a dedicated replica instance with a fixed name on a schedule for running analytical queries and removes it later. This approach confines the analytics or asymmetric workloads to the specific replica instance. For example, your organization may need to run complex analytical reports on Amazon DocumentDB data every Friday that can scan a large amount of data with the least performance overhead on the cluster serving your live customer traffic.

To implement these approaches, you can use AWS Lambda functions from the lambda-samples GitHub repo. To run the Lambda functions on a schedule, we use Amazon EventBridge rules. The following diagram illustrates the solution architecture.

In the following sections, we walk through the steps to deploy the Lambda functions and create the EventBridge rules.

Prerequisites

To implement the solution, you need to have the following resources set up:

{
    "Version": "2012-10-17",
    "Statement": [
        {
            "Effect": "Allow",
            "Action": [
                "rds:DeleteDBInstance",
                "rds:CreateDBInstance",
                "rds:ListTagsForResource",
                "rds:AddTagsToResource",
                "rds:DescribeDBClusters"
            ],
            "Resource": "<Amazon DocumentDB resource ARN>"
        }
    ]
}

Deploy Lambda functions

To schedule scaling with multiple replicas, we use the docdb-scaleOnSchedule.py script and for targeted replicas, we use the docdb-add-delete-targetedReplica.py script. You may choose to deploy either of these functions or both based on your requirements.

The steps for deploying both Lambda functions are the same, except for the environment variables that are specific to each function.

Complete the following steps to deploy the Lambda function for scheduled scaling with replicas:

  1. On the Lambda console, choose Functions in the navigation pane.
  2. Choose Create function.
  3. Select the option Author from scratch.
  4. For Function name, enter a valid function name, such as scheduled-scaling-docdb.
  5. For Permissions, expand Change default execution role, choose Use existing role, and choose the IAM role that matches the requirements stated in the prerequisites.
  6. Choose Create function.
  7. Copy the script docdb-scaleonschedule.py in raw format, enter it into the Code source, and choose Deploy.
  8. On the Configuration tab, under General configuration, choose Edit.
  9. Change Timeout to 30 seconds and choose Save.
  10. Under Environment variables, choose Edit.
  11. Choose Add environment variable and add the variables in the following table (these are for the scheduled scaling Lambda: docdb-scaleOnSchedule.py).
Key Value
CLUSTER_IDENTIFIER Your Amazon DocumentDB cluster identifier.
INSTANCE_CLASS Your database instance class. For more information about supported classes, see Managing Instance Classes.
INSTANCES_TO_ADD The number of instances to add to your clusters at a time.
INSTANCES_TO_DELETE The number of instances to be removed from your clusters at a time.

For more information, see the Readme.

  1. Choose Save.Add Lambda function

To create the Lambda function for the targeted read replica, complete the preceding steps with the following changes:

Key Value
CLUSTER_ID Your Amazon DocumentDB cluster identifier.
INSTANCE_CLASS Your database instance class. For more information about supported classes, see Managing Instance Classes.
INSTANCE_NAME The name of the instance to be added and removed.

For more information, see the Readme.

Next, you create a schedule to invoke these Lambda functions using the EventBridge rules.

Create EventBridge rules

You need to create two EventBridge rules for each Lambda function: one rule for adding the instances and the other for deleting the instances.

Complete the following steps steps to create an EventBridge rule to add instances on every weekday from Monday to Friday:

  1. On the EventBridge console, choose Rules in the navigation pane.
  2. Choose Create rule.
  3. On the Define rule detail page, enter a name (for example, scale-docdb-mon-fri) and optional description.
  4. Select Rule type: Schedule and choose Continue to create rule.
  5. For the scheduled pattern, leave the default (fine-grained schedule).
  6. For Cron expression, enter the expression that applies to your workload. For example, the expression to run the functions every weekday at 8:00 AM is cron( 00 8 ? * 2-6 *).
  7. Choose Next.
    • If your expression is correct, you will see the next dates to invoke the function. For more information, see Cron expressions.
  8. On the Select targets page, choose AWS Service (default) and choose Lambda Function from the dropdown.
  9. For the function, choose the Lambda function you created earlier.
  10. Expand Additional settings and in the Configure target input section, choose Constant (JSON text) and enter {"Action": "Add"} as input to add instances.
  11. Choose Next.
  12. If you want to add tags to the EvenBridge rule, add them on the Configure tags page.
  13. Choose Next.
  14. On the Review and create page, review the values and choose Create rule.

Add EventBridge Rules

To create an EventBridge rule to delete the instances, repeat these steps but provide a different name for the rule and enter {"Action": "Delete"} for Constant (JSON text).

Clean up

If you created a new Amazon DocumentDB cluster and are no longer using it, you can stop the cluster or delete the cluster. You can also delete the Lambda functions and EventBridge rules associated with the cluster. If you created a new IAM role, you can delete the role if you’re not using that role elsewhere.

Summary

In this post, we showed you a cost-effective way to schedule scaling activities on an Amazon DocumentDB cluster using Lambda and EventBridge to handle anticipated read traffic patterns.

For more information about recent launches and blog posts, see Amazon DocumentDB (with MongoDB compatibility) resources.


About the Author

Kaarthiik Thota is a Senior DocumentDB Specialist Solutions Architect at AWS. He is passionate about database technologies and enjoys helping customers solve problems and modernize applications using NoSQL databases. Before joining AWS, he worked extensively with Relational databases, NoSQL databases, and Business Intelligence (BI) technologies for over 15 years.