デッドレターキューと再試行戦略
バックオフ付きの再試行、再配信回数の制限、デッドレターキューを使って処理に失敗したメッセージを扱い、非同期パイプラインの信頼性を保つ方法を学びます。
「デッドレターキューと再試行戦略」はCoddyKit上の無料API Rate Limiting & Scalability Patternsレッスンです。 これはレッスン4/4です。 下記で完全なレッスンを無料で読むことができます。その後、ブラウザ内の組み込みコードエディタと24時間対応のAIチューターでハンズオン演習できます。 これはAPI Rate Limiting & Scalability Patterns学習パスの一部であり、ウェブとCoddyKitアプリ全体で進捗が同期されます。 API Rate Limiting & Scalability Patternsコースには全4レッスンが含まれています。
このレッスンの一部はまだ翻訳されておらず、英語で表示されています。
When Messages Fail
In an async pipeline, a consumer can fail to process a message — a bug, a bad payload, or a downstream outage. Without a plan, that message can block the queue or be lost.
This lesson covers retries and the dead letter queue.
Acknowledgements
A consumer acks a message to confirm successful processing. If it nacks (or never acks), the broker can redeliver it.
Acking before doing the work risks loss; ack only after success.
msg = queue.receive()
process(msg)
msg.ack() # only after successNaive Retries Are Dangerous
Immediately re-processing a failed message can create a tight loop that hammers a struggling downstream service — a self-inflicted outage.
You need delay and a limit.
Exponential Backoff
Wait longer after each failed attempt: 1s, 2s, 4s, 8s. This gives a transient problem time to recover instead of pounding it.
def delay(attempt):
return min(2 ** attempt, 60)Jitter
If many consumers back off on the same schedule, they retry in sync — a thundering herd. Add random jitter to spread retries out.
import random
def delay(attempt):
base = min(2 ** attempt, 60)
return base / 2 + random.uniform(0, base / 2)Max Retry Limit
Some failures never succeed — a malformed message is poison. After a fixed number of attempts, stop retrying and move the message aside.
The Dead Letter Queue
A dead letter queue (DLQ) is a separate queue where messages go after exhausting retries. The main pipeline keeps flowing while failures are quarantined for inspection.
if msg.attempts >= MAX_RETRIES:
dlq.send(msg)
else:
requeue(msg, delay(msg.attempts))Inspecting the DLQ
The DLQ is your debugging surface. Engineers review failed messages, find the root cause, fix code or data, and then replay them back into the main queue.
Idempotent Consumers
Retries mean a message may be processed more than once. Make handlers idempotent — processing the same message twice yields the same result, for example by tracking processed message IDs.
if seen.contains(msg.id):
msg.ack() # already handled
else:
process(msg)
seen.add(msg.id)Alerting on the DLQ
A growing DLQ is a signal something is broken. Alert when its depth crosses a threshold so failures get human attention before they pile up.
Poison Message Patterns
Some failures repeat no matter how many times you retry — a malformed payload, a missing referenced record. Detect these early by inspecting the error type: route deterministic, non-transient failures straight to the DLQ instead of wasting retry attempts.
if is_permanent(error):
dlq.send(msg) # no point retrying
else:
requeue(msg, delay(msg.attempts))Quick Check
Test your understanding of failure handling.
Recap
You learned to handle message failures:
- Ack after success, nack to redeliver
- Exponential backoff with jitter spaces retries
- A retry limit protects against poison messages
- A DLQ quarantines failures for inspection and replay
- Make consumers idempotent
よくある質問
「デッドレターキューと再試行戦略」レッスンは無料ですか?
はい。「デッドレターキューと再試行戦略」の完全なテキストはこのウェブで無料で読めます。インタラクティブに演習し(組み込みコードエディタと24時間対応のAIチューター)、API Rate Limiting & Scalability Patternsコースの残りをアンロックするには、CoddyKit PROにアップグレードしてください。 API Rate Limiting & Scalability Patternsコースには全4レッスンが含まれています。
「デッドレターキューと再試行戦略」で何を学びますか?
バックオフ付きの再試行、再配信回数の制限、デッドレターキューを使って処理に失敗したメッセージを扱い、非同期パイプラインの信頼性を保つ方法を学びます。 ブラウザで直接実行するハンズオンコードでAPI Rate Limiting & Scalability Patternsを演習し、24時間対応のAIチューターがレッスンを進める中での質問に答えます。
API Rate Limiting & Scalability Patternsを始めるのに経験は必要ですか?
事前経験は必要ありません。CoddyKitのAPI Rate Limiting & Scalability Patternsは初級者から上級者向けに構成されているため、ここから始めるか最初から始めて、自分のペースで進むことができます。 これはレッスン4/4です。
「デッドレターキューと再試行戦略」レッスンにはどのくらい時間がかかりますか?
ほとんどのCoddyKitレッスンは約5~10分かかります。各レッスンはコンパクトでインタラクティブなので、着実に進歩し、ウェブとアプリ全体で正確に前回の場所から再開できます。
このAPI Rate Limiting & Scalability Patternsレッスンでコードを書いて実行できますか?
はい。すべてのAPI Rate Limiting & Scalability Patternsレッスンに組み込みコードエディタが含まれているため、ブラウザでリアルコードを書いて実行し、即座のAIフィードバックを取得できます。ローカル設定は不要です。
このコースのすべてのレッスン
- 非同期API入門
- メッセージキューの基礎
- バックグラウンドタスクの実装
- デッドレターキューと再試行戦略