Getting old data with FastAPI boto3 Dynamodb

58 views Asked by At

I am using boto3 to interact with dynamodb in my fastapi application and once the server is running if I do any edit, delete via application it works perfectly and gets reflected in the dynamodb, but when I do the get via application it gets me the old data not the updated one. But once I stop the server and run it again, it starts to reflect all the changes. What could be the issue here and how can I fix it?

def get_dict_items(table_name, dict):
    try:
        table = dynamodb.Table(f'{table_name}')
        result = [table.query(IndexName='application_id-index', KeyConditionExpression=Key('application_id').eq(app['application_id']))["Items"] for app in dict]
        return 200, result
    except ClientError as ce:
        return 500, f"Error getting batch items from table {table_name}: {ce}"
    except (BotoCoreError, NoCredentialsError) as e:
        return 500, f"Unexpected error getting batch items from table {table_name}: {e}"

I tried running boto3 within the function where I get the data instead of defining it globally but no changes.

1

There are 1 answers

3
Leeroy Hannigan On

You are using eventually consistent reads, which has the potential to return old data. Try use strong consistency and see if it resolves your issue:

result = [table.query(IndexName='application_id-index', KeyConditionExpression=Key('application_id').eq(app['application_id']), ConsistentRead: True)["Items"] for app in dict]

https://docs.aws.amazon.com/amazondynamodb/latest/developerguide/HowItWorks.ReadConsistency.html