LibraryGlobal Secondary Indexes

Global Secondary Indexes

Learn about Global Secondary Indexes as part of Serverless Architecture with AWS Lambda

Understanding Global Secondary Indexes (GSIs) in DynamoDB

In the world of serverless architectures, efficient data retrieval is paramount. Amazon DynamoDB, a NoSQL database service, offers powerful indexing capabilities to optimize query performance. While primary keys are essential for uniquely identifying items, Global Secondary Indexes (GSIs) unlock flexible data access patterns, especially when working with AWS Lambda functions.

What is a Global Secondary Index (GSI)?

A Global Secondary Index (GSI) is a collection of item attributes that provides a different queryable view of your data. It has a partition key and an optional sort key, which can be different from the primary key attributes of the base table. GSIs allow you to query data based on attributes other than the primary key, enabling more diverse access patterns.

GSIs enable querying data by attributes other than the primary key.

Think of a GSI as a secondary way to organize and access your data. If your primary key is like a book's ISBN, a GSI could be like an index by author or subject, allowing you to find books without knowing the ISBN.

Each GSI has its own provisioned throughput settings, separate from the base table. When you create a GSI, DynamoDB copies the data from the base table to the index. This means that queries against a GSI are independent of queries against the base table, offering flexibility in how you structure your application's data access.

Key Characteristics of GSIs

FeatureGlobal Secondary Index (GSI)Local Secondary Index (LSI)
Partition KeyCan be different from the base table's partition keyMust be the same as the base table's partition key
Sort KeyOptional, can be different from the base table's sort keyOptional, must be different from the base table's sort key
ThroughputIndependent provisioned throughputShares throughput with the base table
Data ProjectionCan project all attributes, specific attributes, or only keysCan project all attributes, specific attributes, or only keys
ConsistencyEventually consistent by defaultStrongly consistent by default

When to Use Global Secondary Indexes with Lambda

GSIs are particularly useful in serverless architectures where AWS Lambda functions frequently interact with DynamoDB. Consider using GSIs when:

  • Your Lambda function needs to query items based on attributes that are not part of the primary key.
  • You need to support multiple, diverse query patterns for the same data.
  • You want to avoid complex scan operations, which can be inefficient and costly.
  • You need to perform queries that require filtering or sorting on attributes different from the base table's keys.

GSIs are essential for building flexible and performant serverless applications, allowing your Lambda functions to efficiently retrieve data based on various criteria.

Creating and Querying GSIs

You can create GSIs when you create a table or add them to an existing table. When querying a GSI, you use the

code
Query
operation, specifying the GSI name and the partition key (and optionally the sort key) of the GSI. You can also use
code
FilterExpression
to further refine your results.

Imagine a DynamoDB table storing user profiles with a primary key of userId. If you frequently need to find users by their emailAddress or city, you would create GSIs with emailAddress or city as the partition key. A Lambda function could then efficiently query these GSIs to retrieve the desired user data without scanning the entire table.

📚

Text-based content

Library pages focus on text content

Best Practices for GSIs

  • Choose GSI keys wisely: Design your GSI partition and sort keys to match your most frequent query patterns.
  • Attribute Projection: Project only the attributes your application needs to minimize storage costs and improve read performance.
  • Monitor Throughput: Keep an eye on GSI read/write capacity to avoid throttling.
  • Avoid Over-Indexing: While GSIs are powerful, creating too many can increase complexity and cost. Design them strategically.
What is the primary benefit of using a Global Secondary Index (GSI) in DynamoDB for your Lambda functions?

GSIs allow Lambda functions to query data efficiently based on attributes other than the primary key, enabling flexible access patterns and avoiding costly table scans.

Learning Resources

Amazon DynamoDB Global Secondary Indexes(documentation)

The official AWS documentation providing a comprehensive overview of Global Secondary Indexes, their creation, and usage.

DynamoDB Best Practices(documentation)

Learn about best practices for designing and managing DynamoDB tables, including advice on indexing strategies.

Querying with Global Secondary Indexes(documentation)

Detailed guide on how to query data using Global Secondary Indexes in DynamoDB.

AWS Lambda and DynamoDB: A Deep Dive(blog)

A blog post exploring common patterns and best practices for integrating AWS Lambda with Amazon DynamoDB.

DynamoDB GSI vs LSI: When to Use Which(blog)

A practical comparison between Global Secondary Indexes (GSIs) and Local Secondary Indexes (LSIs) to help you choose the right one.

Understanding DynamoDB Indexes(video)

A video tutorial explaining the concepts of primary keys, secondary indexes, and their importance in DynamoDB.

DynamoDB: Design Patterns for Accessing Data(video)

This video covers various DynamoDB access patterns and how to implement them effectively, including the use of GSIs.

DynamoDB(wikipedia)

Wikipedia's overview of Amazon DynamoDB, its features, and its role as a managed NoSQL database service.

Serverless Data Patterns with DynamoDB(video)

This video discusses common data patterns in serverless architectures, highlighting the role of DynamoDB and its indexing capabilities.

AWS Lambda Integration with DynamoDB(documentation)

Official AWS Lambda documentation on how to integrate Lambda functions with Amazon DynamoDB, including code examples.