Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Is boto3 Client taken from a Resource thread-safe? Client: `boto3.resource('dynamodb').meta.client`

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_statement with 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:
    1. Initialise the Boto3 Resource of dymanodb like
    dynamodb_resource_object = boto3.resource('dynamodb')
    
    1. Create partiql update statements in a batch of 25
    2. Then using concurrent.futures.ThreadPoolExecutor, submit batches to call batch_execute_statement like:
    dynamodb_resource_object.meta.client.batch_execute_statement(Statements=statements)
    
    1. 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
like image 463
Nomad14 Avatar asked Jan 17 '26 18:01

Nomad14


1 Answers

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

like image 181
Lee Hannigan Avatar answered Jan 19 '26 20:01

Lee Hannigan



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!