This browser is no longer supported.
Upgrade to Microsoft Edge to take advantage of the latest features, security updates, and technical support.
Note
Access to this page requires authorization. You can trysigning in orchanging directories.
Access to this page requires authorization. You can trychanging directories.
When an application transfers data using the Azure Storage client library for Python, there are several factors that can affect speed, memory usage, and even the success or failure of the request. To maximize performance and reliability for data transfers, it's important to be proactive in configuring client library transfer options based on the environment your app runs in.
This article walks through several considerations for tuning data transfer options. When properly tuned, the client library can efficiently distribute data across multiple requests, which can result in improved operation speed, memory usage, and network stability.
Properly tuning data transfer options is key to reliable performance for uploads. Storage transfers are partitioned into several subtransfers based on the values of these arguments. The maximum supported transfer size varies by operation and service version, so be sure to check the documentation to determine the limits. For more information on transfer size limits for Blob storage, seeScale targets for Blob storage.
The following arguments can be tuned based on the needs of your app:
max_concurrency: The maximum number of subtransfers that may be used in parallel.Note
The client libraries will use defaults for each data transfer option, if not provided. These defaults are typically performant in a data center environment, but not likely to be suitable for home consumer environments. Poorly tuned data transfer options can result in excessively long operations and even request timeouts. It's best to be proactive in testing these values, and tuning them based on the needs of your application and environment.
Themax_single_put_size argument is the maximum blob size in bytes for a single request upload. If the blob size is less than or equal tomax_single_put_size, the blob is uploaded with a singlePut Blob request. If the blob size is greater thanmax_single_put_size, or if the blob size is unknown, the blob is uploaded in chunks using a series ofPut Block calls followed byPut Block List.
It's important to note that the value you specify formax_block_sizedoes not limit the value that you define formax_single_put_size. Themax_single_put_size argument defines a separate size limitation for a request to perform the entire operation at once, with no subtransfers. It's often the case that you wantmax_single_put_size to beat least as large as the value you define formax_block_size, if not larger. Depending on the size of the data transfer, this approach can be more performant, as the transfer is completed with a single request and avoids the overhead of multiple requests.
If you're unsure of what value is best for your situation, a safe option is to setmax_single_put_size to the same value used formax_block_size.
Themax_block_size argument is the maximum length of a transfer in bytes when uploading a block blob in chunks. As mentioned earlier, this valuedoes not limitmax_single_put_size, which can be larger thanmax_block_size.
To keep data moving efficiently, the client libraries may not always reach themax_block_size value for every transfer. Depending on the operation, the maximum supported value for transfer size can vary. For more information on transfer size limits for Blob storage, see the chart inScale targets for Blob storage.
The following code example shows how to specify data transfer options when creating aBlobClient object, and how to upload data using that client object. The values provided in this sample aren't intended to be a recommendation. To properly tune these values, you need to consider the specific needs of your app.
def upload_blob_transfer_options(self, account_url: str, container_name: str, blob_name: str): # Create a BlobClient object with data transfer options for upload blob_client = BlobClient( account_url=account_url, container_name=container_name, blob_name=blob_name, credential=DefaultAzureCredential(), max_block_size=1024*1024*4, # 4 MiB max_single_put_size=1024*1024*8 # 8 MiB ) with open(file=os.path.join(r'file_path', blob_name), mode="rb") as data: blob_client = blob_client.upload_blob(data=data, overwrite=True, max_concurrency=2)In this example, we set the number of parallel transfer workers to 2, using themax_concurrency argument on the method call. This configuration opens up to two connections simultaneously, allowing the upload to happen in parallel. During client instantiation, we set themax_single_put_size argument to 8 MiB. If the blob size is smaller than 8 MiB, only a single request is necessary to complete the upload operation. If the blob size is larger than 8 MiB, the blob is uploaded in chunks with a maximum chunk size of 4 MiB, as set by themax_block_size argument.
During an upload, the Storage client libraries split a given upload stream into multiple subuploads based on the configuration options defined during client construction. Each subupload has its own dedicated call to the REST operation. For aBlobClient object, this operation isPut Block. The Storage client library manages these REST operations in parallel (depending on transfer options) to complete the full upload.
You can learn how the client library handles buffering in the following sections.
Note
Block blobs have a maximum block count of 50,000 blocks. The maximum size of your block blob, then, is 50,000 timesmax_block_size.
The Storage REST layer doesn’t support picking up a REST upload operation where you left off; individual transfers are either completed or lost. To ensure resiliency for stream uploads, the Storage client libraries buffer data for each individual REST call before starting the upload. In addition to network speed limitations, this buffering behavior is a reason to consider a smaller value formax_block_size, even when uploading in sequence. Decreasing the value ofmax_block_size decreases the maximum amount of data that is buffered on each request and each retry of a failed request. If you're experiencing frequent timeouts during data transfers of a certain size, reducing the value ofmax_block_size reduces the buffering time, and may result in better performance.
By default, the SDK buffers data ofmax_block_size bytes per concurrent subupload request, but memory use can be limited to 4 MiB per request if the following conditions are met:
max_block_size argument must be greater thanmin_large_block_upload_threshold. Themin_large_block_upload_threshold argument can be defined during client instantiation, and is the minimum chunk size in bytes required to use the memory efficient algorithm. Themin_large_block_upload_threshold argument defaults to4*1024*1024 + 1.While this strategy applies to most situations, it's still possible for more buffering to occur if your code is using other client library features that require buffering.
Properly tuning data transfer options is key to reliable performance for downloads. Storage transfers are partitioned into several subtransfers based on the values of these arguments.
The following arguments can be tuned based on the needs of your app:
max_chunk_get_size: The maximum chunk size used for downloading a blob. Defaults to 4 MiB.max_concurrency: The maximum number of subtransfers that may be used in parallel.max_single_get_size: The maximum size for a blob to be downloaded in a single call. If the total blob size exceeds themax_single_get_size, the remainder of the blob data is downloaded in chunks. Defaults to 32 MiB.connection_data_block_size: Specifies the size of data blocks read from the network stream. Defaults to 4 KiB.def download_blob_transfer_options(self, account_url: str, container_name: str, blob_name: str): # Create a BlobClient object with data transfer options for download blob_client = BlobClient( account_url=account_url, container_name=container_name, blob_name=blob_name, credential=DefaultAzureCredential(), max_single_get_size=1024*1024*32, # 32 MiB max_chunk_get_size=1024*1024*4, # 4 MiB connection_data_block_size=1024*4, # 4 KiB ) with open(file=os.path.join(r'file_path', 'file_name'), mode="wb") as sample_blob: download_stream = blob_client.download_blob(max_concurrency=2) sample_blob.write(download_stream.readall())During a download, the Storage client libraries split a given download request into multiple subdownloads based on the configuration options defined during client construction. Each subdownload has its own dedicated call to the REST operation. Depending on transfer options, the client libraries manage these REST operations in parallel to complete the full download.
During a download, the Storage client libraries make one download range request usingmax_single_get_size before doing anything else. During this initial download request, the client libraries know the total size of the resource. If the initial request successfully downloaded all of the content, the operation is complete. Otherwise, the client libraries continue to make range requests up tomax_chunk_get_size until the full download is complete.
During a download, theconnection_data_block_size setting can significantly influence download performance. Its impact is highly dependent on your specific environment and hardware configuration. Try starting with a value such as 64KiB and gradually adjusting it to find the optimal balance while monitoring performance and resource usage.
Was this page helpful?
Need help with this topic?
Want to try using Ask Learn to clarify or guide you through this topic?
Was this page helpful?
Want to try using Ask Learn to clarify or guide you through this topic?