|
| 1 | +--- |
| 2 | +title: Performance tuning for uploads and downloads with Azure Storage client library for JavaScript |
| 3 | +titleSuffix: Azure Storage |
| 4 | +description: Learn how to tune your uploads and downloads for better performance with Azure Storage client library for JavaScript. |
| 5 | +services: storage |
| 6 | +author: pauljewellmsft |
| 7 | +ms.author: pauljewell |
| 8 | +ms.service: azure-blob-storage |
| 9 | +ms.topic: how-to |
| 10 | +ms.date: 06/04/2024 |
| 11 | +ms.devlang: javascript |
| 12 | +ms.custom: devx-track-js, devguide-js, devx-track-js, devx-track-extended-js |
| 13 | +--- |
| 14 | + |
| 15 | +# Performance tuning for uploads and downloads with JavaScript |
| 16 | + |
| 17 | +When an application transfers data using the Azure Storage client library for JavaScript, 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. |
| 18 | + |
| 19 | +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. |
| 20 | + |
| 21 | +## Performance tuning for uploads |
| 22 | + |
| 23 | +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, see [Scale targets for Blob storage](scalability-targets.md#scale-targets-for-blob-storage). |
| 24 | + |
| 25 | +### Set transfer options for uploads |
| 26 | + |
| 27 | +You can configure properties in [BlockBlobParallelUploadOptions](/javascript/api/@azure/storage-blob/blockblobparalleluploadoptions) to improve performance for data transfer operations. The following table lists the properties you can configure, along with a description: |
| 28 | + |
| 29 | +| Property | Description | |
| 30 | +| --- | --- | |
| 31 | +| [`blockSize`](/javascript/api/@azure/storage-blob/blockblobparalleluploadoptions#@azure-storage-blob-blockblobparalleluploadoptions-blocksize) | The maximum block size to transfer for each request as part of an upload operation. To learn more, see [blockSize](#blocksize). | |
| 32 | +| [`maxSingleShotSize`](/javascript/api/@azure/storage-blob/blockblobparalleluploadoptions#@azure-storage-blob-blockblobparalleluploadoptions-maxsingleshotsize) | If the size of the data is less than or equal to this value, it's uploaded in a single put rather than broken up into chunks. If the data is uploaded in a single shot, the block size is ignored. Default value is 256 MB. If you customize this property, you must use a value less than or equal to 256 MB. To learn more, see [maxSingleShotSize](#maxsingleshotsize). | |
| 33 | +| [`concurrency`](/javascript/api/@azure/storage-blob/blockblobparalleluploadoptions#@azure-storage-blob-blockblobparalleluploadoptions-concurrency) | The maximum number of parallel requests that are issued at any given time as a part of a single parallel transfer. | |
| 34 | + |
| 35 | +> [!NOTE] |
| 36 | +> 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. |
| 37 | +
|
| 38 | +#### maxSingleShotSize |
| 39 | + |
| 40 | +The `maxSingleShotSize` value is the maximum blob size in bytes for a single request upload. |
| 41 | + |
| 42 | +If the size of the data is less than or equal to `maxSingleShotSize`, the blob is uploaded with a single [Put Blob](/rest/api/storageservices/put-blob) request. If the blob size is greater than `maxSingleShotSize`, or if the blob size is unknown, the blob is uploaded in chunks using a series of [Put Block](/rest/api/storageservices/put-block) calls followed by [`Put Block List`](/rest/api/storageservices/put-block-list). |
| 43 | + |
| 44 | +It's important to note that the value you specify for `blockSize` *does not* limit the value that you define for `maxSingleShotSize`. The `maxSingleShotSize` 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 want `maxSingleShotSize` to be *at least* as large as the value you define for `blockSize`, 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. |
| 45 | + |
| 46 | +If you're unsure of what value is best for your situation, a safe option is to set `maxSingleShotSize` to the same value used for `blockSize`. |
| 47 | + |
| 48 | +#### blockSize |
| 49 | + |
| 50 | +The `blockSize` value is the maximum length of a transfer in bytes when uploading a block blob in chunks. |
| 51 | + |
| 52 | +As mentioned earlier, this value *does not* limit `maxSingleShotSize`, which can be larger than `blockSize`. |
| 53 | + |
| 54 | +To keep data moving efficiently, the client libraries might not always reach the `blockSize` 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 in [Scale targets for Blob storage](scalability-targets.md#scale-targets-for-blob-storage). |
| 55 | + |
| 56 | +#### Code example |
| 57 | + |
| 58 | +The following code example shows how to set values for [BlockBlobParallelUploadOptions](/javascript/api/@azure/storage-blob/blockblobparalleluploadoptions) and include the options as part of an upload method call. The values provided in the samples aren't intended to be a recommendation. To properly tune these values, you need to consider the specific needs of your app. |
| 59 | + |
| 60 | +```javascript |
| 61 | +// Specify data transfer options |
| 62 | +const uploadOptions = { |
| 63 | + blockSize: 4 * 1024 * 1024, // 4 MiB max block size |
| 64 | + concurrency: 2, // maximum number of parallel transfer workers |
| 65 | + maxSingleShotSize: 8 * 1024 * 1024, // 8 MiB initial transfer size |
| 66 | +} |
| 67 | + |
| 68 | +// Create blob client from container client |
| 69 | +const blockBlobClient = containerClient.getBlockBlobClient(blobName); |
| 70 | + |
| 71 | +// Upload blob with transfer options |
| 72 | +await blockBlobClient.uploadFile(localFilePath, uploadOptions); |
| 73 | +``` |
| 74 | + |
| 75 | +In this example, we set the maximum number of parallel transfer workers to 2 using the `concurrency` property. We also set `maxSingleShotSize` 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, which we define in the `blockSize` property. |
| 76 | + |
| 77 | +### Performance considerations for uploads |
| 78 | + |
| 79 | +During an upload, the Storage client libraries split a given upload stream into multiple subuploads based on the configuration options defined by `BlockBlobParallelUploadOptions`. Each subupload has its own dedicated call to the REST operation. In this example, the operation is [Put Block](/rest/api/storageservices/put-block). The Storage client library manages these REST operations in parallel (depending on transfer options) to complete the full upload. |
| 80 | + |
| 81 | +> [!NOTE] |
| 82 | +> Block blobs have a maximum block count of 50,000 blocks. The maximum size of your block blob, then, is 50,000 times `block_size`. |
| 83 | +
|
| 84 | +#### Buffering during uploads |
| 85 | + |
| 86 | +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 for `blockSize`, even when uploading in sequence. Decreasing the value of `blockSize` 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 of `blockSize` reduces the buffering time, and might result in better performance. |
| 87 | + |
| 88 | +## Performance tuning for downloads |
| 89 | + |
| 90 | +Tuning data transfer options for downloads is available only when using the [downloadToBuffer](/javascript/api/@azure/storage-blob/blobclient#@azure-storage-blob-blobclient-downloadtobuffer) method. This method downloads a blob in parallel to a buffer based on the values defined in [BlobDownloadToBufferOptions](/javascript/api/@azure/storage-blob/blobdownloadtobufferoptions). Other download methods don't support tuning data transfer options. |
| 91 | + |
| 92 | +### Set transfer options for downloads |
| 93 | + |
| 94 | +The following values can be tuned for downloads when using the `downloadToBuffer` method: |
| 95 | + |
| 96 | +- [blockSize](/javascript/api/@azure/storage-blob/blobdownloadtobufferoptions#@azure-storage-blob-blobdownloadtobufferoptions-blocksize): The maximum block size to transfer for each request. |
| 97 | +- [concurrency](/javascript/api/@azure/storage-blob/blobdownloadtobufferoptions#@azure-storage-blob-blobdownloadtobufferoptions-concurrency): The maximum number of parallel requests issued at any given time as a part of a single parallel transfer. |
| 98 | + |
| 99 | +### Performance considerations for downloads |
| 100 | + |
| 101 | +During a download using `downloadToBuffer`, the Storage client libraries split a given download request into multiple subdownloads based on the configuration options defined by `BlobDownloadToBufferOptions`. 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. |
| 102 | + |
| 103 | +#### Code example |
| 104 | + |
| 105 | +The following code example shows how to set values for [BlobDownloadToBufferOptions](/javascript/api/@azure/storage-blob/blobdownloadtobufferoptions) and include the options as part of a [downloadToBuffer](/javascript/api/@azure/storage-blob/blobclient#@azure-storage-blob-blobclient-downloadtobuffer) method call. The values provided in the samples aren't intended to be a recommendation. To properly tune these values, you need to consider the specific needs of your app. |
| 106 | + |
| 107 | +```javascript |
| 108 | +// Specify data transfer options |
| 109 | + const downloadToBufferOptions = { |
| 110 | + blockSize: 4 * 1024 * 1024, // 4 MiB max block size |
| 111 | + concurrency: 2, // maximum number of parallel transfer workers |
| 112 | + } |
| 113 | + |
| 114 | + // Download data to buffer |
| 115 | + const result = await client.downloadToBuffer(offset, count, downloadToBufferOptions); |
| 116 | +``` |
| 117 | + |
| 118 | +## Related content |
| 119 | + |
| 120 | +- To understand more about factors that can influence performance for Azure Storage operations, see [Latency in Blob storage](storage-blobs-latency.md). |
| 121 | +- To see a list of design considerations to optimize performance for apps using Blob storage, see [Performance and scalability checklist for Blob storage](storage-performance-checklist.md). |
0 commit comments