I am using batch_execute_statement to update the data in dynamodb in AWS Lambda. To do so, I am getting the boto3 client from a resource like this:
boto3.resource('dynamodb').meta.client.batch_execute_statement(Statements=statements)
As batch_execute_statement has a limitation to take 25 statements at a time, I want to use multi-treading to make this faster.
[What I have tried]
- I have tried serial approach to call
batch_execute_statementwith a batch of 25 statements at a time. It is working as expected and updating the data in dynamodb. - I have implemented the concurrent approach and it is also working. Here are the steps I used:
- Initialise the Boto3 Resource of dymanodb like
dynamodb_resource_object = boto3.resource('dynamodb')- Create partiql update statements in a batch of 25
- Then using
concurrent.futures.ThreadPoolExecutor, submit batches to callbatch_execute_statementlike:
dynamodb_resource_object.meta.client.batch_execute_statement(Statements=statements)- Then process on completed tasks.
- Even though it works, is it safe to use it this way? If not, is there a safe way to use concurrency?
[QUE]
- Is boto3 client taken from a resource thread-safe? If yes, then what are the things I need to be cautious about? If no, then is there any other way to achieve batch Update in dynamodb?
I followed these references:
- https://docs.aws.amazon.com/amazondynamodb/latest/developerguide/example_dynamodb_Scenario_PartiQLBatch_section.html https://boto3.amazonaws.com/v1/documentation/api/latest/reference/services/dynamodb/client/batch_execute_statement.html#batch-execute-statement https://docs.aws.amazon.com/amazondynamodb/latest/APIReference/API_BatchExecuteStatement.html
DynamoDB boto3 resource client is not thread safe. You should create a new client for each process.
DynamoDB does not provide an official way to do BatchUpdate. I would suggest to use the UpdateItem API and perhaps an async client, such as boto3aio