javaamazon-dynamodbintegration-testingamazon-dynamodb-indexdynamo-local

Facing issue while writing Java Integration Tests due to GSI (dynamoDB)


I've a dynamo table in on which I've configured a Global Secondary Index.

tripID -> Partition Key
userId -> Range Key
dateScheduledAt -> Partition Key of the GSI
timeScheduledAt -> Range Key of the GSI

I've written a method in my code which queries the index and fetches the corresponding records.

While writing ITs to test this feature, I created a local instance of dynamoDB. In the

It returned an empty array and my test failed when I applied validation. Seems like even tho the values were created in the table, it wasn't present in the GSI (I checked by querying directly the table before step 2)

I added a delay of 5 minutes before step 1 & 2 (Thread.sleep()) and this time, the same method returned the correct response and my test was passed.

This seems to me is happening because GSI doesn't guarantee consistency in the system. But the current hack applied by me is not a scalable solution as the number of tests increases. Does anyone have a better solution to writing tests when GSI is involved ?


Solution

  • DynamoDB replication to a Global Secondary Index is asynchronous and can take several milliseconds to converge. You must handle this eventual consistency in your test cases, as GSI will not provide strong read after write consistency.