tencent cloud

ドキュメントKey Management Service

Implementing Exponential Backoff to Deal with Service Frequency

Download
フォーカスモード
フォントサイズ
最終更新日: 2026-07-30 17:00:44
AI翻訳

Policy Recommendations for Exception Handling

When an application's request is sent to the KMS remote server (that is, when the KMS API is called), if an exception or error occurs, you can adopt the following policies to handle it.
Cancel: You should terminate/cancel the program call and report an exception when the returned error indicates a non-transient fault, or when the operation still fails after a retry.
Retry: You can retry immediately when the returned error is uncommon or rare, such as when a network packet is corrupted during transmission but is still sent.
Delayed Retry: When the returned error is due to common connection or busyness issues, the service may require a brief recovery period to clear backlogged work. For such problems, retry after waiting for an appropriate amount of time.
This document explains the delayed retry policy. The wait time (that is, the delay) mentioned above can be implemented using a gradually increasing approach or a timing policy (such as exponential backoff). Because the KMS API service limits the request frequency, you can adopt the delayed retry method to avoid issues caused by rate limiting when your call concurrency is too high.

Exponential Backoff

Pseudocode

Use a gradually increasing approach to delay retrying an operation.
InitDelayValue = 100
For(Retries = 0; Retries < MAX_RETRIES; Retries = Retries+1)
wait for (2^Retries * InitDelayValue) milliseconds
Status = KmsApiRequest()
IF Status == SUCCESS
BREAK // Succeeded, stop calling the API again.
ELSE IF Status = THROTTLED || Status == SERVER_NOT_READY
CONTINUE // Failed due to throttling or server busy, try again.
ELSE
BREAK // another error occurs, stop calling the API again.
END IF

Policy Application

Python Example: How to Handle Rate Limiting Errors When Calling the KMS Encrypt API Using the Exponential Backoff Method.
# -*- coding: utf-8 -*-
import base64
import math
import time
from tencentcloud.common import credential
from tencentcloud.common.exception.tencent_cloud_sdk_exception import TencentCloudSDKException
from tencentcloud.common.profile.client_profile import ClientProfile
from tencentcloud.common.profile.http_profile import HttpProfile
from tencentcloud.kms.v20190118 import kms_client, models


def KmsInit(region="ap-guangzhou", secretId="", secretKey=""):
try:
credProfile = credential.Credential(secretId, secretKey)
client = kms_client.KmsClient(credProfile, region)
return client
except TencentCloudSDKException as err:
print(err)
return None

def BackoffFunction(RetryCount):
InitDelayValue = 100
DelayTime = math.pow(2, RetryCount) * InitDelayValue
return DelayTime

if __name__ == '__main__':
# User-defined parameters
secretId = "replace-with-real-secretId"
secretKey = "replace-with-real-secretKey"
region = "ap-guangzhou"
keyId = "replace-with-realkeyid"
plaintext = "abcdefg123456789abcdefg123456789abcdefg"
Retries = 0
MaxRetries = 10
client = KmsInit(region, secretId, secretKey)
req = models.EncryptRequest()
req.KeyId = keyId
req.Plaintext = base64.b64encode(plaintext)
while Retries < MaxRetries:
try:
Retries += 1
rsp = client.Encrypt(req) # Call the encryption API.
print 'plaintext: ',plaintext,'CiphertextBlob: ',rsp.CiphertextBlob
break
except TencentCloudSDKException as err:
if err.code == 'InternalError' or err.code == 'RequestLimitExceeded':
if Retries == MaxRetries:
break
time.sleep(BackoffFunction(Retries + 1))
continue
else:
print(err)
break
except Exception as err:
print(err)
break
Attention:
To resolve other specific errors, you can directly modify or adjust the content of the except statement.
Based on your code logic and business policies, plan and formulate a timing policy to set the optimal initial delay value (InitDelayValue) and number of retries (Retries), thereby avoiding thresholds that are set too low or too high, which would impact overall business operations.

ヘルプとサポート

この記事はお役に立ちましたか?

フィードバック