DynamoDB create index on map or list type
System Design practice on Codemia
Work through 120+ system design problems with detailed solutions, from rate limiters to multi-region storage.
DynamoDB is a fully managed NoSQL database service provided by AWS that offers high performance and scalability for web-scale applications. One of its powerful features is the ability to create secondary indexes to improve query performance. However, when working with complex data types such as maps and lists, there are specific considerations and limitations to account for when creating indexes.
Secondary Indexes in DynamoDB
DynamoDB supports two types of secondary indexes:
- Global Secondary Index (GSI): These indexes can span all items in a table, are eventually consistent, and can be queried separately from the table.
- Local Secondary Index (LSI): These indexes are scoped to the partition key of the base table, providing an alternative sort key. They must be created during table creation and support only strongly consistent reads.
Both index types can be used to enhance read operations by providing alternate access patterns. However, indexing on map or list types introduces challenges, which AWS DynamoDB approaches with specific strategies.
Challenges with Indexing Map and List Types
DynamoDB’s data model accommodates attributes of complex types like maps and lists. However, directly using these types for key attributes in an index involves some constraints:
- Map Type:
- Maps are a data type that allows nesting of attributes.
- They are not directly indexable since their inner structure is not directly comparable or sortable.
- List Type:
- Lists hold an ordered collection of values, which may include different data types.
- The variability and nested nature of lists make them unsuitable for direct indexing.
Strategies for Indexing Complex Types
DynamoDB offers some approaches for effectively working with maps and lists in the context of indexing:
Flattening Attributes
One strategy to address index limitations with maps and lists is by flattening the structure. This involves creating string representations or extracted values of map or list attributes for the indexing purpose. For instance:
- Map Example: Given a map attribute
Addresswith fieldsCityandZipCode, create separate string fields likeAddress_CityandAddress_ZipCodeto be indexed. - List Example: For a list of tags, you might transform it into a concatenated string and store it in a separate attribute such as
TagsIndexable.
Using Projections
When it is necessary to query on nested attributes, projections in secondary indexes can be useful. Secondary indexes can project inner attributes to avoid issues with the data structure complexity:
- Global Secondary Index Projection: Define projected attributes within a composite attribute index to facilitate queries on nested data.
Utilizing JSON Serialization
Another practical approach is serializing complex types into JSON strings. This enables:
- Storage of complex data in a flexible, flat string format.
- Easier handling during the indexing process, as serialized JSON can be indexed like any other string datatype.
Related reading
- DynamoDB createTable if not exists
- DynamoDb Delete all items having same Hash Key
- DynamoDb Delete all items having same \`Hash\` Key
- DynamoDB error when update Date Unsupported type passed ... Pass options.convertClassInstanceToMaptrue to marshall typeof object as map attribute
- DynamoDB for PHP sessions
- DynamoDB get item TypeScript hell
- DynamoDB get item TypeScript hell
- DynamoDB GSI BatchGetItem

System Design Fundamentals
Build a strong foundation in designing scalable, reliable distributed systems.
View the courseSystem Design practice on Codemia
Work through 120+ system design problems with detailed solutions, from rate limiters to multi-region storage.