Skip to content

Batch & stream processing · 3 of 3

The batch job that outgrew its night

Step 1 · The pattern

A nightly job that fits its night

After the shop goes quiet, a nightly job rebuilds the sales summary by reading every order ever placed. When the shop was young it finished in an hour, long before the morning rush (amber). (Time is compressed: the night is six seconds.)

  • a shopper
  • a job query
  • answered

In short

The batch job that outgrew its night: how it works and how it breaks

Nightly batch jobs are planned around a window: start after the shop goes quiet, finish before the morning. A job that recomputes everything, all orders ever, all customers, takes time in proportion to the whole history, which grows every day. For months it fits. Then one night it is still running when the morning rush begins, and its heavy queries take the database capacity that shoppers need. If it fails and must be rerun, the rerun collides with the next night's run too. The fix is to make the job's work follow a day's changes, not the size of the history: process only what changed since the last run, using a timestamp, a change log or partitions by date, and merge the result into the totals. Track every job's run time against its window, and alert well before they meet.

What this simulation simplifies

  • The night is six seconds here and the job's growth is shown as three separate nights; real jobs creep by minutes a month.
  • The job and the shoppers share one small database with no limits on the job's share, which makes the collision sharper than a well-limited system would show.

How this comes up in interviews

"A nightly batch job keeps getting slower and has started running into business hours. What would you do?"

Then they ask

  1. How would you make the job incremental, and what could it miss?
  2. How would you see this coming next time?
  3. How do you keep the job from hurting live traffic?

A strong answer covers

  • A job that reprocesses all history grows with the history and will outgrow any window.
  • Process only what changed since the last run: timestamps, change data capture, or date partitions.
  • Track run time against the window and alert when the gap shrinks, not when it fails.
  • Make reruns safe (overwrite a period's output) and leave room for one retry.
  • Run heavy jobs on a replica or warehouse, or cap their share of the live database.

Points to cover, not a script. Say them in your own words and tie them to a system you have worked on.

On one page: the track cheat sheet.

Last updated: Official documentation

Diagrams are our own simulations: the timings, rates and counts in them are illustrative, not measurements. Captions are original wording.