Welcome to the Microsoft Q&A and thank you for posting your questions here.
You were asking about the performance issues when using Parallel.ForEachAsync to upload 3500 records to Cosmos DB and if to use AllowBulkExecution option in Cosmos Client for Parallel.ForEachAsync method or it only works with Task.WhenAll.
In your scenario, you're using Parallel.ForEachAsync to upload 3500 records to Cosmos DB. This method distributes work across multiple tasks, which can potentially benefit from bulk execution if the SDK is allowed to batch operations together.
The fact that you're seeing similar performance with and without AllowBulkExecution suggests that either the operations are not being effectively batched, or the overhead of batching is negligible compared to the overall operation time.
It's generally recommended to use AllowBulkExecution when performing bulk operations, as it can improve performance by reducing the number of round trips to the database. However, its effectiveness can depend on various factors such as the size of the documents being inserted, network latency, and the characteristics of the Cosmos DB instance.
About your question, whether AllowBulkExecution only works with Task.WhenAll, that's not necessarily the case. AllowBulkExecution affects how individual requests are handled by the SDK, regardless of whether they are executed concurrently using Parallel.ForEachAsync, Task.WhenAll, or any other asynchronous mechanism.
You might consider experimenting with different batch sizes and profiling the performance of your application under various conditions to optimize performance further. In addition, ensure that your Cosmos DB instance is properly provisioned to handle the expected workload.
I hope this is helpful! Do not hesitate to let me know if you have any other questions.
Please remember to "Accept Answer" if answer helped, so that others in the community facing similar issues can easily find the solution.
Best Regards,
Sina