DynamoDB supports composite primary keys, Partition key and a Sort key. Records can have same Partition key but different Sort keys. DynamoDB Global Secondary Index (GSI) can be used to create an index with different attributes as Partition Key and Sort Key.

What’s the magic?

When a record is added to a DynamoDB table, it is propagated to an index only if both Partition key and Sort key is present. If the Sort key is not present the record doesn’t appear on the index.

How is this useful?

Example: Consider an application which uses a ‘User’ table. Every time a new user is created, a new DynamoDB record is inserted in User table and an email notification is sent to the user. The email notification application is an async application which queries the ‘UserNotificationIndex’ index and sends an email to the user.

User table and GSI schema

Example User record

On new user creation, DynamoDB inserts the record into the User table and also propagates it into UserNotificationIndex. EmailNotification application scans the ‘UserNotificationIndex’ and sees a new record for AmazingUserName. After sending the email notification it updates the DynamoDB record with ‘IsNotificationNeeded’ attribute removed. This update is allowed as ‘IsNotificationNeeded’ is not a Key in the User table. Once ‘IsNotificationNeeded’ attribute is removed, the DynamoDB record for ‘AmazingUserName’ is no longer visible in IsNotificationNeeded but is still present in the User table. Next time EmailNotification application queries the index, it wouldn’t see any record till a new User record is inserted into the table.

Takeaway

Use SortKey on GSI’s to make records appear and disappear from your application.

Reference

https://docs.aws.amazon.com/amazondynamodb/latest/developerguide/bp-indexes-general-sparse-indexes.html#bp-indexes-sparse-examples