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

340 Views Asked by At

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:

1

There are 1 best solutions below

2
Leeroy Hannigan On

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