← Building blocks

Process large data offline

Data processing & searchfundamental

Daily reports, model training and backfills over terabytes.

Say it like this

The daily report and the model retrain can be hours old, so I run them as scheduled batch jobs over the event archive in object storage and keep the request path out of it entirely.

Basic solution

Run distributed batch jobs over data in object storage or a warehouse on a schedule.

SparkMapReduceAirflowdbtBigQuery

Partitioned input, parallel tasks, atomic publish

Events are archived into date partitions; the scheduler waits for a partition to be complete, fans the work out across tasks, and swaps the finished output in as one step so readers never see a half-built day.

How it works

  1. Raw events are written to object storage as immutable files, grouped into folders by time (date=2026-10-21/hour=13/part-0001.parquet). A stream sink writes them as events arrive, or a nightly export dumps them in one go. Once written, a file is never edited.
  2. A scheduler such as Airflow triggers the job at a fixed time, for example 01:00 for the previous day, but only after checking that the input is complete: every hour's folder has its _SUCCESS marker, meaning the writer finished.
  3. The job's driver lists just the folders it needs (yesterday's 24 hours), turns them into tasks (one per file or file range), and hands the tasks to a pool of workers that run in parallel.
  4. When a stage has to group or join by a key (clicks by alias, views by video), the workers shuffle: each sends its rows to the worker responsible for that key, so all rows for one alias end up together. That worker then aggregates them and writes its output to a new file.
  5. Output files are written to a staging folder first. When every task has finished, the job swaps the staging folder in as the day's partition in one metadata operation, so a dashboard never reads a half-finished day.
  6. A task that fails is retried on its own. The whole run is safe to repeat because it always rebuilds its output partition from the inputs rather than adding to it, so running it twice gives the same result as once.

Why batch at all

  • Throughput per core is far higher than streaming: there is no per-event overhead, the files are columnar so only needed columns are read, and the scans are big and sequential.
  • Reprocessing is trivial: change the code, rerun the date range, and the outputs are rebuilt.

Worked example

Daily click analytics for the URL shortener

  • 4,000 redirects per second average is 345M click events per day; at 200 bytes each that is about 70 GB of raw JSON, around 10 GB as compressed Parquet in 24 hourly partitions.
  • Job: group by (alias, day, referrer) and count, plus unique visitors per alias from a hashed visitor id. Output is about 20M rows, a few hundred MB.
  • Cost: reading 10 GB and shuffling 345M rows takes a 20-core Spark cluster roughly 4 minutes; the same count as a live query over the raw table would scan the full 70 GB on every dashboard load.
  • Schedule: hourly partitions are closed 15 minutes after the hour; the daily job runs at 01:15 UTC and finishes by 01:20, so the dashboard shows yesterday by 01:30.
  • Backfill: a bug in the referrer parser means rerunning 90 days. Ninety runs of 4 minutes on a bigger cluster finish in an afternoon, and each run overwrites exactly its own day partition.
  • Freshness gap: the batch answers are up to 25 hours stale; the live per-minute counters come from the stream job, and the nightly batch corrects any late or duplicated events (the lambda shape).

Variants and when each wins

Distributed batch engine (Spark, Flink batch)

  • A cluster that runs your code over files in object storage, with joins, aggregation and machine learning. It wins for large transforms the warehouse cannot express; the cost is a cluster to run and a scheduler to drive it.

SQL in the warehouse (BigQuery, Snowflake, dbt)

  • Transforms written as SQL models that dbt runs on a schedule inside the warehouse. It wins when the work is relational and the data already lives there, because there is no cluster to operate and lineage comes for free.

MapReduce

  • The original shape: a map step over input, a shuffle by key, a reduce step, with every stage writing to disk. It has been superseded by in-memory engines, but it is still the vocabulary interviewers use for the same ideas.

Lambda and kappa

  • Lambda runs a batch job for correctness and a stream job for freshness, two code paths that must agree. Kappa keeps one stream job and recomputes by replaying the log from the start. Kappa wins when the log retains enough history and the logic is simple; lambda when exactness needs a separate, auditable batch.

Recommendation: dbt-style SQL in the warehouse for reporting, Spark for anything the warehouse cannot express, and a stream job alongside only when seconds of freshness are a product requirement.

Failure modes

Job reads an incomplete partition

  • Yesterday's totals come out 5 percent low and never correct themselves. It happens because the job started before the last files for the day had landed, so it counted an incomplete input and then marked the day as done. Make the scheduler wait on a completeness marker (a _SUCCESS file or a row count written by the producer), and rerun a trailing window of recent days to pick up late data.

Partial output visible

  • Every night the dashboard shows half a day for about ten minutes. It happens because the job writes output files straight into the live partition while it is still running, so readers see whatever has landed so far. Write to a staging location and publish the finished partition in a single swap or metadata operation.

Skewed keys

  • 199 tasks finish in a minute and one runs for an hour. It happens because one viral alias holds 30 percent of all rows, and the shuffle sent every one of them to a single reducer. Salt the hot key by appending a random suffix so its rows spread across several reducers, then combine the partial results, or pre-aggregate on each worker before the shuffle.

Silent schema drift

  • A column is suddenly null across the whole output and nothing failed. It happens because an upstream producer renamed a field, and the job read the old name as missing rather than erroring. Validate the schema at ingestion, agree a contract for the event shape with producers, and alert when a column's null rate changes.

Runaway cost

  • The monthly bill triples with no change in data volume. It happens because the job read every partition in the archive instead of yesterday's, since a date filter was not pushed down to the file listing. Prune partitions by date explicitly, pass explicit date ranges to the job, and set an alert on bytes scanned per run.

Interviewer follow-ups

Answer out loud before you open each one.

How do you make a batch job safe to rerun?

Make it rebuild its output partition from the inputs every time and write to a staging path that replaces the old partition atomically. No incremental updates to the target and no appends, so running it twice gives the same result as once.

Why not just query the raw events live?

A live count over 345M rows per day scans tens of gigabytes per dashboard load and competes with production traffic. Precomputing once per day turns that into a lookup over 20M summary rows, and the cost is paid once rather than per viewer.

Where does batch processing fit next to the stream job?

The stream job gives per-minute counts within seconds but can double count on retries and misses very late events. The nightly batch recomputes the day from the archive with full dedup and overwrites the summary, so the stream is for freshness and the batch is the source of truth.

How do you handle a backfill of six months without disturbing the daily run?

Run the backfill as a separate job on its own cluster, one day per task so each day is an independent, idempotent unit, with concurrency capped so the warehouse writes do not starve the nightly run. Process oldest first and watch the output row counts against the originals.

Connections

Built on
HyperLogLog · Bitmap index · DAG of tasks
Pairs with
Compute over events as they arrive · Move data between systems · Analytics without hurting production · Keep large blobs out of the database · Run work later or on a schedule
Pulls against
Measure p95 / p99, not the average
Used in
URL Shortener (TinyURL / bit.ly) · Video Streaming Platform (YouTube / Netflix)

The same parts on other clouds

The diagrams above name AWS services as one concrete example. Every part has a close equivalent elsewhere. In an interview, name the role first, such as “a managed Redis cache”, then the product you would pick.

AWSUsed here asGoogle CloudMicrosoft AzureOpen source and others
Amazon S3Event archive (S3), Staging outputCloud StorageBlob StorageCloudflare R2, MinIO, Ceph
AWS Step FunctionsSchedulerWorkflowsLogic Apps, Durable FunctionsTemporal, Apache Airflow
Amazon EMRJob driver, Worker poolDataprocAzure Databricks, HDInsightApache Spark, Databricks
Amazon RedshiftWarehouse partitionBigQuerySynapse Analytics, Microsoft FabricClickHouse, Snowflake, Apache Druid