I have a DynamoDB table that has
- partition key "idA", sort key "idB"
- GSI partition key "idB", sort key "idA"
I am attempting to delete all items with specific "idB", so I query the GSI to get a list of records, but also want to paginate results for scale.
If I was querying against the main index I could probably simply re-run the query with a limit and delete each record but because I need to use the GSI this results in records that have been deleted showing up in subsequent queries due to GSI's eventual consistency, ie a records deletion often does not propogate to the GSI before the next query is invoked.
Another path is to use the LastEvaluatedKey from the previous query response as the ExclusiveStartKey for the next query which should result in only new records being returned, however there is a fair chance the record LastEvaluatedKey points too will no longer exist due to being deleted in the previous iteration.
When that happens weird results are returned, I thought it should work because on a main index if you send a non-existent ExclusiveStartKey Dynamo can still figure out where it should start retrieving records from, due to it's ordering system.
But on the GSI the results often start with the next expected record, but then some records might get skipped, and often the non-existent ExclusiveStartKey query will not return a LastEvaluatedKey, even though not all remaining records have been returned.
I am playing with ideas to handle this strange behaviour:
- not deleting the last record return to decrease the chance ExclusiveStartKey does not exist (and deleting it's record after the next iteration)
- doing an extra check when no LastEvaluatedKey is returned to make sure no records actually remain
But they are messy workarounds.
Anybody understand why the weird results happen, does it happen in any structured way?
Any other advice how to solve the task I am performing?