Increase number of shards in DynamoDB to spin up more lambdas in parallel
Master System Design with Codemia
Enhance your system design skills with over 120 practice problems, detailed solutions, and hands-on exercises.
Introduction
Amazon DynamoDB is a fully managed NoSQL database service that provides fast and predictable performance with seamless scalability. One of the compelling features of DynamoDB is its support for triggering AWS Lambda functions through DynamoDB Streams. The execution of these Lambdas can be controlled by the number of shards in the DynamoDB Stream, contributing to parallel processing workflows. Increasing the number of shards can thus enable more Lambda functions to run in parallel, improving system throughput. This article explores the intricacies of managing DynamoDB shards to effectively spin up more Lambda functions concurrently.
Understanding DynamoDB Streams and Shards
DynamoDB Streams
DynamoDB Streams capture information about every modification to DynamoDB tables. The streams store this information for 24 hours, offering a near-real-time view of alterations. Developers can use these streams to trigger AWS Lambda functions, essentially rendering automated responses to data changes.
Shards in DynamoDB Streams
DynamoDB Streams are composed of multiple shards, which are containers for stream records. Each shard comprises a sequence of events and has a unique iterator. When AWS Lambda processes DynamoDB Streams, it reads from these shards concurrently. More shards imply more parallel read operations, which translates into an increased number of parallel Lambda executions.
Increasing Shards for Parallel Lambda Processing
Automatic Shard Creation
DynamoDB does not allow users to directly control the number of shards. Instead, shard creation is automatically managed based on table partitioning. Whenever a table is split due to a burst in write capacity, a new shard is created to handle the additional data traffic. Thus, increasing table partitions through deliberate load distribution results in more shards.
Deliberate Load Distribution
- Write Capacity Adjustment: By manipulating the write throughput settings or using the
auto-scalingoption, you can influence the partitioning process indirectly. - Distribute Writes Evenly: Ensuring that the write operations are spread across multiple keys will lead to additional partitions. This can be accomplished via hashed keys to distribute the writes uniformly.
Example Scenario
Imagine you have a DynamoDB table with a single partition handling all writes:

