DynamoDB
secondary indexes
optional indexes
AWS
database management

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.

json
1{
2  "AttributeDefinitions": [
3    { "AttributeName": "UserId", "AttributeType": "S" },
4    { "AttributeName": "TransactionType", "AttributeType": "S" }
5  ],
6  "TableName": "UserTransactions",
7  "KeySchema": [
8    { "AttributeName": "UserId", "KeyType": "HASH" },
9    { "AttributeName": "TransactionId", "KeyType": "RANGE" }
10  ],
11  "LocalSecondaryIndexes": [
12    {
13      "IndexName": "TransactionTypeIndex",
14      "KeySchema": [
15        { "AttributeName": "UserId", "KeyType": "HASH" },
16        { "AttributeName": "TransactionType", "KeyType": "RANGE" }
17      ],
18      "Projection": { "ProjectionType": "ALL" }
19    }
20  ]
21}

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.

bash
1aws dynamodb query --table-name UserTransactions --index-name TransactionTypeIndex \
2    --key-condition-expression "UserId = :uid and TransactionType = :type" \
3    --expression-attribute-values {
4      ":uid": { "S": "User123" },
5      ":type": { "S": "Purchase" }
6    }

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

FeatureLocal Secondary Index (LSI)Global Secondary Index (GSI)
Creation TimeAt table creationAny time
Partition KeySame as base tableDifferent from base table
Sort KeyDifferent from base tableOptional, different from base table
ThroughputShared with base table (potentially impacted)Independent of base table
Size Limits10GB per partitionNo inherent limits
Cost ImpactExtra storage requiredExtra 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.


Course illustration
Course illustration

All Rights Reserved.