When NOT to Retry

Concepts covered: Retry & Failure Handling

A retry is the wrong tool for records that can't be processed, credentials that are rejected, and bugs in the pipeline's own code, which together account for nearly all the cases where retrying hurts. A retry on any of them produces the same error again, only later, with the alert delayed by the full length of the backoff schedule. Each category has its own destination.

CategoryExampleWhy a retry cannot helpRoute to
Bad recordA row with `shipped_at = 'yesterday'` in a timestamp columnThe row is the same on every attemptQuarantine storage, then continue
Rejected credential401 after the partner rotated the API keyThe key stays wrong until a person replaces itStop, alert, rotate the credential
Code bug`TypeError` because a function received a list instead of a dictThe code is the same on every attemptFail the task, alert the owner
Missing resource404 for an endpoint removed in the partner's v3 APIThe endpoint will not come backFail the task, alert the owner
Rate limit429 with a `Retry-After: 120` headerIt will help, after the pause the service namesRetry after exactly that pause

Bad records go to quarantine

A nightly file holds 2 million orders, and 12 of them have a text string in the amount column. Failing the whole task over 12 rows blocks 1,999,988 good rows from reaching the warehouse, and retrying the task re-reads the same 12 bad rows. Instead, catch the parse error per row, write the bad row to a quarantine table alongside the error message, the source file name and the time, and keep going. Good rows load as usual. The quarantine table is a storage location with a schema, so it can be queried, and the task raises an alert when the quarantine count crosses a threshold, say 100 rows.

Rows that parse take the top path into orders. Rows that fail to parse take the bottom path, and an alert fires when the quarantine count crosses its threshold.
  • order_files (source, daily CSV drop)
  • parse_rows (transform, Python task)
  • orders (storage, warehouse table)
  • orders_quarantine (storage, warehouse table)
  • quarantine_alert (consumer, on-call channel)
  • order_files -> parse_rows
  • parse_rows -> orders
  • parse_rows -> orders_quarantine
  • orders_quarantine -> quarantine_alert
from datetime import datetime


def load_file(path):
    good, bad = [], []
    for raw in read_csv(path):
        try:
            good.append(parse_order(raw))
        except ValueError as err:
            bad.append(
                {"raw": raw, "error": str(err), "file": path, "seen_at": datetime.utcnow()}
            )
    write_orders(good)
    write_quarantine(bad)
    if len(bad) > 100:
        alert(f"{len(bad)} rows quarantined from {path}")

The except here catches ValueError and nothing wider. A KeyError from a renamed column is a bug in the mapping rather than a bad row, and it should stop the task so the mapping gets fixed, instead of quarantining 2 million rows one at a time. The error field in each quarantine row is what lets someone fix the source or the parser tomorrow.

Rejected credentials go to a person

A 401 or 403 means the service understood the request and refused it. The key has expired, been revoked, or lost a permission. Retrying has 2 costs beyond wasted time. Many services count failed authentications and lock the account after a handful, so a loop of 5 attempts with backoff can turn a 2 minute fix into a support ticket. And the backoff schedule delays the alert: with the cap at 60 seconds, a person hears about the problem minutes after the first 401 instead of immediately. The task should fail on the first 401 and raise an alert that names the credential, so the owner can rotate it. Rotation means issuing a new secret at the service, storing it where the task reads secrets, and rerunning the task.

Bugs fail fast

A TypeError, KeyError, AttributeError or ZeroDivisionError from the pipeline's own code is the same on the hundredth attempt as on the first. The right response is the one Python gives by default: let it propagate, let the task fail, let the orchestrator alert the owner with the traceback. The bounded loop from earlier does this already, because is_transient returns False for anything outside its list. A loop that catches Exception turns a 1 line bug into a task that sleeps for 2 minutes before reporting the same traceback it had at second 0.

A task that pulls invoices from a partner API starts receiving 403 Forbidden on every call at 02:00. Its retry loop uses backoff with a 60 s cap and 8 attempts. What should the task do differently?

About this lesson

This section is part of the Failure Handling: Beginner lesson on DataDriven, the home of data engineering. Lessons cover SQL, Python, Spark, data modeling and pipeline architecture, and each section pairs its explanation with worked examples and practice you run as you go.