Topic 123 / Advanced
Batch processing and ETL
Chapter 7.5 · Jobs, Workflows, Batch and Streams
What and why
Some work is naturally bounded (yesterday's orders, this month's invoices) rather than an endless stream, and processing it one record at a time as it arrives wastes the efficiency of operating on the whole set at once. Batch processing runs a job over a bounded dataset on a schedule or on demand; ETL (extract, transform, load) is the specific batch shape of pulling data from a source, reshaping it, and loading it into a destination such as a warehouse (→ 213) — distinct from stream processing (→ 124), which processes unbounded data continuously as it arrives. Builds on: → 121 (background job processing — a batch job is a scheduled job whose scope is "the whole available dataset," not a single unit of work).
Diagram
| Batch | Stream | |
|---|---|---|
| Input | Bounded (yesterday's data) | Unbounded, continuous |
| Latency | Minutes to hours | Seconds or sub-second |
| Typical use | Nightly reports, backfills | Live dashboards, alerting |
| Re-run cost | Safe if idempotent | Needs windowing/replay logic |
How it works
- A batch job runs on a schedule or on demand, over a defined, bounded slice of data (e.g. "all orders placed yesterday").
- Extract: read the source data — database tables, files, an upstream export — for that slice.
- Transform: reshape, clean, aggregate, or join the data into the destination's required shape.
- Load: write the result to its destination — a warehouse table, a report, a downstream system.
- Design every batch job to be safely re-runnable (idempotent, → 116): a job that fails halfway and is re-run from the start, or re-run for the same date range, should produce the same final result — not double-counted or partially-overlapping data. This is what makes a backfill (re-running a past date range after a bug fix) safe.
Practical example
(illustrative). A nightly ETL job extracts the prior day's 2
million orders, transforms them into daily revenue-by-region aggregates, and loads the
result into a warehouse table keyed by (date, region) using an upsert. Re-running the
same date overwrites that date's rows rather than appending duplicates, so a retried or
backfilled run for a date that failed yesterday produces exactly the same totals as a clean
first run.
When to use it
- Use when the data is naturally bounded, some processing latency is acceptable, and bulk reads and writes outweigh the need for immediacy.
- Avoid when the business needs sub-minute freshness or alerting on individual events as they happen — that's stream processing's case (→ 124).
- Failure scenarios: a non-idempotent load step (appending rather than upserting/overwriting) turns every retry or backfill into duplicated data; a batch window boundary that doesn't match the source system's notion of "a day" (time zones, late-arriving data written after the batch already ran) silently drops or double-counts records at the edges.
- Simpler alternative: for small enough datasets, a single ad hoc script run manually when needed is simpler than a scheduled pipeline — formalize into ETL once it's recurring and a missed manual run has already caused a problem.
Practice
(process; 20–30 min, lab Postgres). Write a batch job that aggregates a
day's worth of seeded order rows into a daily_revenue(date, region, total) table using an
upsert keyed by (date, region); run it twice for the same date and confirm the totals
don't double. Delete a row to simulate a late-arriving order, re-run for that date, and
confirm the backfill corrects the total without affecting other dates. Done when re-running
the same date is a no-op on correct data, and a correct fix on corrected data.
Key takeaway
Batch and ETL process bounded data on a schedule, trading immediacy for bulk efficiency, and must be built idempotent so retries and backfills are safe rather than risky. Done when you can: design a batch job's boundary and re-run behavior so a retry never corrupts the result. Used next in → 124 (stream processing is the same transform-and-load problem on unbounded data); Project 18, warehouse context → 213.
References
None — ETL mechanics here are vendor-neutral; the warehouse destination (→ 213) is out of scope for this topic.
Source directory