LoginSignup
17

More than 3 years have passed since last update.

Boto3 に追加されたリトライ処理モードを利用する

Posted at

はじめに

AWS SDK は Exponential Backoff による自動再試行ロジックが実装されていますが、
Boto3 では 3 つのリトライ処理モードを選択できます。

  • legacy (デフォルト)
  • standard
  • adaptive

botocore の GitHub リポジトリを確認すると、2020年2月ごろに standard および adaptive が
追加され、それ以前の実装は legacy という名称になっています。

Add support for new retry modes #1972

互換性のため、デフォルトが legacy となっており、standard/adaptive で追加された機能を
利用するにはリトライ処理モードを明示的に変更する必要があります。

リトライ処理モードの変更方法

botocore の config オブジェクトをインスタンス化し、設定情報をクライアントに
渡すことができます。利用可能なオプションは max_attemptsmode です。
リトライ処理モードによってデフォルトの最大試行回数は異なりますが、
max_attempts によってカスタマイズできます。

import boto3
from botocore.config import Config

config = Config(
   retries = {
      'max_attempts': 10,
      'mode': 'standard'
   }
)

ec2 = boto3.client('ec2', config=config)

各リトライ処理モードの概要

Legacy retry mode

Boto3 クライアントでデフォルトで使用されるモードです。
バックオフ時間の計算に使用される係数は 2 です。
v1 リトライハンドラー が使用されます。
再試行に対応する errors/exceptions の数が限られています。

Standard retry mode

多言語の AWS SDK と一貫性のあるロジックにより、再試行ルールが標準化されています。
legacy モードよりも多くの throttling/limit エラーや例外に対応しています。
バックオフ時間の計算に使用される係数は legacy モード同様 2 ですが、最大値は 20 秒です。
v2 リトライハンドラー が使用されます。

legacy standard
デフォルトの最大試行回数 5 3
一時的な接続エラーへの対応 ConnectionError
ConnectionClosedError
ReadTimeoutError
EndpointConnectionError
RequestTimeout
RequestTimeoutException
PriorRequestNotComplete
ConnectionError
HTTPClientError
throttling/limitエラーおよび例外への対応外 Throttling
ThrottlingException
ThrottledException
RequestThrottledException
ProvisionedThroughputExceededException
Throttling
ThrottlingException
ThrottledException
RequestThrottledException
TooManyRequestsException
ProvisionedThroughputExceededException
TransactionInProgressException
RequestLimitExceeded
BandwidthLimitExceeded
LimitExceededException
RequestThrottled
SlowDown
EC2ThrottledException
ステータスコードによる再試行 429/500/502/503/504/509 など 500/502/503/504
遅延時間の計算式 rand(0, 1) * (2 ^ (attempts - 1)) min(rand(0, 1) * 2 ^ attempt, 20)

Adaptive retry mode

Standard モードの機能に加えて、トークンバケットアルゴリズムによるクライアント側の
自動レート制限機能や、再試行の毎に AWSサービス側のエラー/例外/HTTPステータスコード
に基づいてレート制限変数を変更する機能が追加されています。
クライアント側でエラー内容に応じて柔軟な再実行を行なうことができますが、
実験的なモードであるため、将来的に機能と動作が変わる可能性があります。

参考

Retries - Boto3 Docs
https://boto3.amazonaws.com/v1/documentation/api/latest/guide/retries.html

以上です。
参考になれば幸いです。

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
17