Optional secondary indexes in DynamoDB
Master System Design with Codemia
Enhance your system design skills with over 120 practice problems, detailed solutions, and hands-on exercises.
Welcome to the detailed exploration of optional secondary indexes in DynamoDB, an essential feature for many developers and database administrators seeking efficient querying options beyond primary-key attributes. This article will delve into the nature of secondary indexes, their types, setup process, use cases, and their implications on both performance and cost.
Understanding Optional Secondary Indexes
Amazon DynamoDB facilitates the creation of scalable applications requiring fast, predictable performance. However, its schema-less nature originally supports querying data only by the primary key. This limitation can lead to inefficiencies when diverse querying needs arise. Optional secondary indexes provide a solution by allowing additional querying options on non-primary key attributes.
Types of Secondary Indexes
1. Local Secondary Index (LSI)
- Definition: An LSI is an index with the same partition key as the base table but a different sort key. It provides an alternative attribute for searching and organizing the data within a single partition.
- Characteristics:
- Must be created at the same time as the base table.
- Uses the table’s provisioned throughput.
- Inherits a strict 10GB limit on partition size.
- Use Case: Suitable when additional querying capabilities within a partition are required. For example, querying a user's transactions based on a transaction type where the user ID is the partition key and the transaction type is the LSI.
2. Global Secondary Index (GSI)
- Definition: A GSI contains a partition key and an optional sort key, which can be different from the base table's primary key. This index can span across multiple partitions.
- Characteristics:
- Can be added or deleted at any time.
- Has its dedicated provisioned throughput settings.
- No size limits as compared to LSI.
- Use Case: Ideal for queries that need to span multiple partitions or use entirely different attributes from the primary key. For instance, a database allowing users to search for transaction details by transaction ID, irrespective of their user ID.
Creating and Utilizing Secondary Indexes
Creating LSIs and GSIs
Creating an LSI requires specifying it during table creation, while GSIs can be defined at any point in the table's lifecycle. The AWS Management Console, AWS CLI, and AWS SDKs provide interfaces to set up and manage these indexes.
Querying Using Secondary Indexes
Once defined, these indexes can be used within AWS API calls to query the database efficiently through specifying the IndexName parameter.
Performance and Cost Considerations
Performance
Secondary indexes improve read performance by reducing the complexity of query operations, leading to faster retrieval times. However, they introduce overhead on write operations because any modification to the base table's data requires updates to the indexes.
- LSI: Shares throughput with the base table, possibly impacting read/write performance if the table is highly dynamic.
- GSI: Operates on independent capacity units, providing isolation from the base table and often better performance.
Cost
Implementing secondary indexes increases storage costs and, particularly in the case of GSIs, provisioned throughput costs.
- LSI: Extra costs primarily stem from the additional storage required.
- GSI: Costs associated with both storage and the independently managed provisioned throughput.
Summary Table of Key Points
| Feature | Local Secondary Index (LSI) | Global Secondary Index (GSI) |
| Creation Time | At table creation | Any time |
| Partition Key | Same as base table | Different from base table |
| Sort Key | Different from base table | Optional, different from base table |
| Throughput | Shared with base table (potentially impacted) | Independent of base table |
| Size Limits | 10GB per partition | No inherent limits |
| Cost Impact | Extra storage required | Extra storage and throughput costs |
Conclusion
Optional secondary indexes in DynamoDB provide flexibility and efficiency for various querying requirements by enabling different access patterns beyond the primary key. By carefully considering their types, setup procedures, and performance/cost impacts, developers can significantly enhance the querying capabilities of DynamoDB to meet the needs of dynamic applications.

