DynamoDB
Index Schema
CRUD Application
Database Design
AWS

What's the recommended index schema for dynamo for a typical crud application?

Master System Design with Codemia

Enhance your system design skills with over 120 practice problems, detailed solutions, and hands-on exercises.

DynamoDB is a NoSQL database offered by Amazon Web Services, renowned for its scalability, low-latency, and high-availability. Designing an efficient index schema for a typical CRUD (Create, Read, Update, Delete) application is pivotal to leveraging DynamoDB's capabilities. This article will explore recommended indexing strategies, including best practices and examples to illustrate key concepts.

Understanding DynamoDB Indexes

In DynamoDB, indexes enable efficient data retrieval beyond traditional primary key access patterns. There are two primary types of indexes:

  1. Primary Key: This can be a simple primary key (partition key) or a composite primary key (partition key and sort key).
  2. Secondary Indexes:
    • Global Secondary Index (GSI): Allows querying on non-primary key attributes across all partitions.
    • Local Secondary Index (LSI): Allows querying on non-primary key attributes within a single partition.

Designing the Primary Key

Partition Key

  • Choice: Select a partition key that distributes traffic evenly. Skewed workload can lead to hot partitions, affecting performance.
  • Example: In a user management system, using userId as a partition key ensures that requests are distributed, assuming user activities are relatively even.

Composite Key

  • Partition Key + Sort Key: Using a composite key allows for more sophisticated querying capabilities.
  • Example: For a blogging platform, use authorId as the partition key and postId as the sort key. This schema efficiently queries posts by a particular author, sorted by post timestamps if postId includes a timestamp component.

Designing Secondary Indexes

Secondary indexes extend query patterns by adding flexibility without altering the primary table's fundamental schema.

Global Secondary Index (GSI)

  • Definition: A GSI comprises a new partition key, optional sort key, and is not constrained by the primary key schema.
  • Use Case: Facilitates querying by attributes not part of the primary key. For instance, querying posts by tag in a blogging application.
  • Example:
yaml
1  {
2    "TableName": "Posts",
3    "KeySchema": [
4      { "AttributeName": "authorId", "KeyType": "HASH" }, // partition key
5      { "AttributeName": "postId", "KeyType": "RANGE" }   // sort key
6    ],
7    "GlobalSecondaryIndexes": [
8      {
9        "IndexName": "TagIndex",
10        "KeySchema": [
11          { "AttributeName": "tag", "KeyType": "HASH" }
12        ],
13        "Projection": {
14          "ProjectionType": "ALL"
15        }
16      }
17    ]
18  }

Local Secondary Index (LSI)

  • Definition: An LSI shares the same partition key as the base table but adds a different sort key.
  • Use Case: It is useful when the requirement is to query different attributes but within the same partition.
  • Example: In a forum table with threadId as the partition key, an LSI can help fetch posts by their timestamp within a thread.

Best Practices for CRUD Applications

  • Workload Distribution: Choose partition keys that distribute the load evenly.
  • Attribute Projections: Use KEYS_ONLY, INCLUDE, or ALL to decide which attributes a secondary index should project. This choice impacts storage consumption and query performance.
  • DynamoDB Streams: Enable streams to respond to data changes, which is crucial for keeping caches or other dependent services in sync.
  • Capacity Planning: Opt for on-demand or provisioned capacity based on traffic patterns. Monitor and adjust capacity units to optimize costs and performance.

Example Summary Table

Index TypeKey ComponentsExample Use Case
Primary KeyPartition Key or Composite Key (e.g., partition + sort key)Basic CRUD operations based on key values.
Global Secondary Index (GSI)New partition key, optional sort keyQueries on non-primary attributes, e.g., searching posts by tags.
Local Secondary Index (LSI)Same partition key, new sort keyFetch specific order of items within a partition, e.g., posts by timestamp.

Conclusion

Designing an index schema for a CRUD application in DynamoDB requires a clear understanding of the access patterns and data distribution needs. The choice of primary and secondary indexes should aim to optimize data retrieval performance while keeping costs and complexity in check. By adhering to best practices and carefully planning your partition keys and indexes, you can create an efficient and robust application using DynamoDB.


Course illustration
Course illustration

All Rights Reserved.