← Back to Case StudiesPipeline work sample

Synthetic data. I rebuilt these patterns from scratch so the code can be public. Nothing here comes from Amazon systems.

Recovering a failed AWS batch without losing track of a record

An event-driven AWS pipeline built around what happens when things go wrong: failed batches, late data, duplicates, broken feeds, and reruns.

Why I built this

At Amazon I built and ran pipelines like this one. Sites reported operational data every few minutes, and the BI teams depended on every event showing up exactly once, in the right day, even when a batch failed or showed up late. Most of that work was about the failure cases, not the happy path. I wanted a public version I could point to, so I rebuilt the same patterns with made-up data, wrote the infrastructure in Terraform, tested every failure case, and ran it for real on AWS.

The data

A generator creates events from 250 sites, each reporting every 1 to 3 minutes. It adds the problems real feeds have, on purpose:

  • Duplicates from producer retries (same event_id twice)
  • Late events that arrive hours after they happened
  • Mixed timezone offsets instead of clean UTC
  • Broken lines: truncated JSON, missing fields, a value of "n/a", a timestamp of "yesterday-ish"

How it works

1. Normal path

A producer drops a JSONL file in the raw bucket. S3 notifies SQS, and SQS triggers the ingest Lambda one file at a time. Ingest normalizes every timestamp to UTC, removes duplicates, runs the quality gate, and writes Parquet partitioned by the day each event happened.

Loading diagram…

2. Failure and recovery

If a file fails ingest three times, SQS moves it to a dead-letter queue and a CloudWatch alarm watches the queue. The replay Lambda copies the failed file to a replay prefix first and deletes the DLQ message second, so a crash in the middle loses nothing. The copy goes back through the normal path.

Loading diagram…

3. Backfill, check, compact

A Step Functions workflow reprocesses every raw file for a date range, four at a time. Then a check confirms every raw file has a manifest and every manifest balances. Only if nothing was lost does it start the Glue (PySpark) job that compacts each day into large files.

Loading diagram…

What it handles

  • Late events land in the day they happened, not the day they arrived, because partitions come from event time after UTC conversion.
  • Duplicates collapse by event_id inside a batch. A deduplicated Athena view and the compaction job remove duplicates across batches.
  • Bad lines are kept in a quarantine bucket with a reason. Nothing is silently dropped.
  • A broken feed is quarantined whole if more than 5% of its lines fail. Partially loading a broken feed would hide the producer failure.
  • A batch that fails every retry goes to the dead-letter queue and comes back through the normal path after replay.
  • Reruns and backfills are safe. Each batch writes one file per day it touches, and a rerun overwrites exactly those files.
  • Checking nothing was lost: every batch writes a manifest (received, written, rejected, duplicates), and a check confirms every raw file has one and every manifest balances.
  • Small files from per-batch writes are compacted by a Glue PySpark job, so Athena scans and lists less.

Live run on AWS

I deployed it with terraform apply, ran every scenario with a script, recorded the results, and tore it down. The run on November 24, 2026 is archived with its queries and outputs in docs/evidence.md (opens in a new tab).

Three results from that run:

  1. A file that failed every retry was replayed. The destination check listed it as unprocessed before replay, and listed nothing after.
  2. Athena read 36,919 written rows, and the compacted table matched the deduplicated view exactly.
  3. A full-day backfill left the totals identical before and after: 38,541 received, 36,919 written, 539 rejected, 1,083 duplicates.

Everything the run recorded:

  • 38,541 events received: 36,919 written, 539 rejected, 1,083 duplicates removed, and 0 lost in this run
  • Broken feed: quarantined whole, curated data untouched
  • Failed batch: hit the dead-letter queue after 3 attempts, was replayed, and loaded through the normal path
  • Full-day backfill: totals identical before and after
  • Compaction: 10 small Parquet files rewritten into 2 in 79 seconds, with row counts matching the deduplicated view exactly
  • Athena: all 250 sites reporting, and 535 late events landed in the earlier day

What this shows: count agreement and manifest coverage were observed in this one archived run. It does not establish exactly-once behavior for every possible failure. The optional Kinesis streaming input is mock-tested only and was not part of the live run.

Tools and where they're used

  • Python: parsing, cleaning, quality rules, Lambda handlers, and the data generator
  • AWS Lambda, S3, SQS: event-driven ingest, dead-letter queue, and replay
  • AWS Step Functions: backfill orchestration with retries, a parallel map, and a check before compaction
  • AWS Glue and PySpark: the compaction job, plus Glue Data Catalog tables with partition projection
  • Amazon Athena and SQL: a deduplicated view and the reporting queries in the evidence
  • Amazon Kinesis (optional): streaming input batched into raw files
  • Terraform: every resource, including the optional stream behind one variable
  • IAM and KMS: one least-privilege role per function, KMS encryption with key rotation, public access blocked, TLS only
  • Amazon CloudWatch: logs with retention and an alarm on the dead-letter queue
  • Parquet and partitioning: curated data by event date, compacted to one file per day
  • pytest, moto, GitHub Actions: 20 tests against mocked AWS, plus terraform fmt and validate, on every push

Trade-offs I made

  • One file per day per batch instead of appending to one file per day. Reruns stay safe and two batches never fight over the same file. The cost is small files, which compaction fixes.
  • One file per Lambda invocation, so one bad file can never hold up a good one. At higher volume I would batch and use partial batch responses.
  • Copy first, delete second in replay, so a crash between the two steps can only cause a harmless repeat, never a loss.
  • Partition projection instead of a crawler, so late data is queryable as soon as it lands.

Run it yourself

The code, Terraform and tests are on GitHub: timothypike/event-pipeline-recovery (opens in a new tab). The live-run evidence is in docs/evidence.md (opens in a new tab). The tests need no AWS account: run pytest. To deploy it, run terraform apply, then python scripts/demo.py, then terraform destroy. A full run costs a few cents.

Have a failing ingestion job, recurring backfills, or a cutover coming up?

Ask about a pipeline reliability review →