Batch Processing

MapReduce · ETL jobs

Written by Darjus Kazakevic · reviewed

One job over a fixed dataset: read it all, compute, replace the output.

A batch job processes a dataset that has an end: yesterday's logs, last month's orders, a full table export. It reads all of it, computes, writes the result and stops. Nightly ETL, report builds, search-index rebuilds and machine-learning training sets work this way. Spark, Hadoop and SQL engines over a data lake, such as Trino or BigQuery, are common tools.

Batch trades latency for simplicity. Results are hours old, but the input does not change while the job runs, so there is no late data and no state to carry between runs. A run that fails, or that had a bug, is fixed by running it again on the same input. Protect that property in every job you write.

In essence — A batch job reads a bounded input and replaces a whole output partition; because the input is fixed and the output is overwritten, any run can be repeated safely.

When to reach for it

Reach for it when

  • Results can be hours old: reports, billing runs, nightly exports
  • The work needs the whole dataset at once: training sets, full reindexes, deduplication
  • History must be reprocessed after a bug fix or for a new metric

Reconsider when

  • Users or systems must react within minutes: use stream processing
  • Each record needs its own immediate action, such as a welcome email: use a queue
  • The data is small enough for one query on the live database

How it works

Split, shuffle, merge (Map · Shuffle · Reduce)
The input is split into chunks and each worker processes one chunk on its own (map). Records are then regrouped by key across the network, so every value for one key reaches one worker (shuffle). That worker combines them (reduce).
Partitioning
Inputs and outputs are stored in partitions, usually by date and sometimes also by a key. A job reads only the partitions it needs and writes only its own.
Idempotent, re-runnable jobs
A job is a function of its input: the same input partition always produces the same output partition. It writes to a staging location and then swaps the result in, replacing whatever was there.
Backfills
Running a job again for past dates, after a bug fix or to fill a new column. When jobs are idempotent and take the date as a parameter, a backfill is just the normal job run for many dates.
Scheduling and dependencies
Jobs form a graph: the report runs after the cleaned orders, which run after the raw export lands. A scheduler such as Airflow or Dagster starts each job when its inputs exist, and retries it when it fails.

Design rules

Make the run date a parameter
A job computes the partition for a date it is given. It never asks for today's date. Then a retry, a rerun and a backfill are all the same operation as the daily run.
Replace whole partitions
Write to a staging table or path, check it, then swap it in atomically. A job that appends rows doubles them on every rerun.
Keep the raw input immutable
Store what arrived exactly as it arrived, and derive everything else from it. A bug in a derived table is then fixed by fixing the code and rerunning, with nothing lost.
Check the output before you publish it
Compare row counts and key totals with the input and with the previous run. A job that succeeds and writes zero rows is the most common silent failure.

Common pitfalls

  • A job that appends rows, so a retry after a timeout doubles yesterday's revenue.
  • Using now() or "today" inside the job, so a backfill for March computes November.
  • Starting at 02:00 on the clock while the export lands at 02:20, and publishing half a day.
  • One customer with millions of orders, so one task runs for hours while the others sit idle.
  • Writing thousands of tiny files that make every later read slow.