Certopolis

AWS Certified Developer – Associate

DynamoDB GSI vs LSI for the AWS Developer Associate exam

The DynamoDB Developer Guide says a global secondary index can use a partition key and sort key different from the table's, has its own throughput and supports only eventually consistent reads. A local secondary index keeps the table's partition key with another sort key, shares the table's capacity, allows strongly consistent reads and must be created with the table.

Keys and when each index can be created

A global secondary index (GSI) can use any scalar attributes of the table as its partition key and optional sort key. Queries on it span all the data in the table, across all partitions. A GSI can be created with the table, added to an existing table, or deleted later. The default quota is 20 per table.

A local secondary index (LSI) has the same partition key as the table and a different sort key, so its key is always composite. A query on it covers one partition key value. LSIs are created only when the table is created. You cannot add one to an existing table or delete one. A table can have up to 5.

For a table with at least one LSI, all items with the same partition key value, across the table and its LSIs, are limited to 10 GB. Past that limit DynamoDB can return ItemCollectionSizeLimitExceededException for writes to that partition key. GSIs have no such limit.

Read consistency and projected attributes

GSI queries support eventual consistency only. DynamoDB updates a GSI asynchronously after the table write, normally within a fraction of a second. An LSI query can be eventually or strongly consistent, chosen with the ConsistentRead parameter.

Each index stores the table's key attributes plus a projection: KEYS_ONLY, INCLUDE with named non-key attributes, or ALL. A GSI query can return only projected attributes, because DynamoDB does not fetch from the base table. An LSI query can ask for attributes that are not projected, and DynamoDB fetches them from the table automatically.

You read an index with Query or Scan and the IndexName parameter. GetItem and BatchGetItem cannot be used on a GSI. Index key values do not need to be unique, and an item that lacks the index key attribute is not written to the GSI.

Capacity and throttling

In provisioned mode every GSI has its own read and write capacity settings. Queries on the GSI consume the index's read capacity, and index updates caused by table writes consume the index's write capacity.

If a GSI has too little write capacity, writes to the base table are throttled. AWS says a GSI's provisioned write capacity should be equal to or greater than the table's. Changing the value of an indexed key attribute costs two index writes: one to delete the old entry and one to put the new one.

An LSI has no capacity settings of its own. Reads on it consume the table's read capacity units, and the index updates consume the table's write capacity units.

Where the exam catches people

  • Believing a GSI query can be strongly consistent: GSIs support eventually consistent reads only. Use an LSI or the base table for strong consistency.
  • Believing an LSI can be added to an existing table: LSIs are defined at table creation and cannot be added or deleted afterwards.
  • Believing a GSI query fetches missing attributes from the table: it returns only projected attributes, so change the projection or read the base table.
  • Believing table writes are throttled only by the table's own write capacity: an under-provisioned GSI throttles writes to the base table.
  • Believing an LSI can use a different partition key: it must use the table's partition key. A new partition key needs a GSI.

Written from these sources

Try 3 questions on this

Pick an answer to see why each option is right or wrong.

  1. A developer designs an Amazon DynamoDB table for support tickets with customerId as the partition key and ticketId as the sort key. A new screen must list one customer's tickets ordered by lastUpdated, and it must always show the most recent value immediately after a write. Which solution will meet these requirements?

    Right. In the game that’s Renown for your Development with AWS Services building.

    Not that one: A is right. In the game your Development with AWS Services building would lose a little Renown, and this question would come back later.

    Show the full explanation and doc quotes

    Querying the index by customerId returns the customer's tickets already ordered by lastUpdated.

    Written from docs.aws.amazon.com/amazondynamodb/latest/developerguide/LSI.html

  2. An Amazon DynamoDB table has a global secondary index with a KEYS_ONLY projection. A developer runs a Query against the index with a projection expression that lists two non-key attributes from the base table. The query returns only the key attributes, and the two requested attributes are absent from every item. Which solution will resolve this issue?

    Right. In the game that’s Renown for your Development with AWS Services building.

    Not that one: B is right. In the game your Development with AWS Services building would lose a little Renown, and this question would come back later.

    Show the full explanation and doc quotes

    The projection must therefore be changed to INCLUDE the required attributes, or the application must read the base table by primary key.

    Written from docs.aws.amazon.com/amazondynamodb/latest/developerguide/GSI.html

  3. An Amazon DynamoDB table stores audit events with the account ID as the partition key and has a local secondary index on the event type attribute. Writes for a small number of very large accounts begin failing with ItemCollectionSizeLimitExceededException, while writes for other accounts succeed. Which solution will resolve this issue?

    Right. In the game that’s Renown for your Development with AWS Services building.

    Not that one: C is right. In the game your Development with AWS Services building would lose a little Renown, and this question would come back later.

    Show the full explanation and doc quotes

    Local secondary indexes can be created only at table creation, so the fix is a table without them, using a global secondary index that carries no item collection limit.

    Written from docs.aws.amazon.com/amazondynamodb/latest/developerguide/LSI.html

Ask for something