Search code examples
amazon-dynamodbeventual-consistency

Integration testing a DynamoDB client which uses inconsistent reads?


Situation:

  1. A web service with an API to read records from DynamoDB. It uses eventually consistent reads (GetItem default mode)
  2. An integration test consisting of two steps:
    • create test data in DynamoDB
    • call the service to verify that it is returning the expected result

I worry that this test is bound to be fragile due to eventual consistency of the data.

If I attempt to verify the data immediately after writing using GetItem withConsistenRead=true it only guarantees that the data has been written to the majority of DB copies, but not all, so the service under test still has a chance to read from the non-updated copy on the next step.

Is there a way to ensure that the data has been written to all DynamoDB copies before proceeding?


Solution

  • The data usually reach all geographically distributed replicas in a second.

    My suggestion is to wait (i.e. in Java terms sleep for few seconds) for couple of seconds before calling the web service should produce the desired result.

    After inserting the data into DynamoDB table, wait for few seconds before calling the web service.

    Eventually Consistent Reads (Default) – the eventual consistency option maximizes your read throughput. However, an eventually consistent read might not reflect the results of a recently completed write. Consistency across all copies of data is usually reached within a second. Repeating a read after a short time should return the updated data.