3
votes

I have been reading the Amazon DynamoDB documentation to compare Global Secondary Index (GSI) and Local Secondary Index (LSI). I am still unclear that in the below use case, does it matter to me what I use? I am familiar with things like LSI ought to use the same partition key etc.

Here is the use case:

  1. I already know the sort key for my index.
  2. My partition key is the same in both cases
  3. I want to project ALL the attributes from original table onto my index
  4. I know already prior to creating the table what index I need for my use case.

In the above use case, there is absolutely no difference apart from minor latency gain in LSI Vs GSI because LSI might end up in the same shard. I want to understand the Pro Vs Con in my use case.

Here are some questions that I am trying to find the answer to and I have not encountered a blog that is explicit about these:

  1. Use GSI only because the partition key is different?
  2. Use GSI even if the partition key is same but I did not know during table creation that I would need such an index?

Are there any other major reasons where one is superior than the other (barring basic stuff like count limit of 5 vs 20 and all).

1

1 Answers

4
votes

There are two more key differences that are not mentioned. You can see a full comparison between the two index types in the official documentation.

  • If you use a LSI, you can have a maximum of 10 Gb of data per partition key value (table plus all LSIs). For some use cases, this is a deal breaker. Before you use a LSI, make sure this isn’t the case for you.
  • LSIs allow you to perform strongly consistent queries. This is the only real benefit of using a LSI.

The AWS general guidelines for indexes say

In general, you should use global secondary indexes rather than local secondary indexes. The exception is when you need strong consistency in your query results, which a local secondary index can provide but a global secondary index cannot (global secondary index queries only support eventual consistency).

You may also find this SO answer a helpful discussion about why you should prefer a GSI over a LSI.