Under The Hood
backend springboot Spring Batch 5.x / Spring Boot 3.x

Spring Batch: JobRepository, Chunk Processing (ItemReader/Processor/Writer)

Last updated
Prerequisites:
Spring Boot basics
Understanding of batch processing concepts (jobs, steps)
  • spring
  • spring-batch
  • chunk
  • job-repository
  • item-reader
  • item-writer
  • batch

Read at your depth

The practical view

Spring Batch processes large volumes with the chunk model: a Step reads items (ItemReader), processes them (ItemProcessor), and writes them (ItemWriter) in chunks of a configured size (commit-interval). A Job is a sequence of Steps; JobRepository persists job/step execution state so jobs can restart from where they failed. Build with @Configuration: @Bean Job job(JobRepository repo, Step step) and @Bean Step step(JobRepository repo, PlatformTransactionManager tm, ItemReader..., ItemProcessor..., ItemWriter...). Run with CommandLineRunner/JobLauncher. Enable with @EnableBatchProcessing. Readers: JdbcCursorItemReader, JpaPagingItemReader, FlatFileItemReader.

The same idea in other frameworks

react equivalent
N/A — server-side Spring Batch concern.

Legacy vs modern

Manual loop + no restart vs Spring Batch chunk step

A hand-written processing loop is not restartable and has no chunked transactions; the chunk model gives atomic batches, restart, and skip/retry.

before → after
Manual processing loop
for (Row r : db.queryAll()) { // ❌ OLD: no chunking, no restart, no transaction per batch
Spring Batch chunk step
@Bean
Step step(JobRepository repo, PlatformTransactionManager tm) {
  return new StepBuilder("import", repo)
    .<Row, Out>chunk(500, tm) // ✅ NEW: 500 rows per transaction
    .reader(reader())
    .processor(processor())
    .writer(writer())
    .build();
}

Interview gotchas

Context

This tests the restart model — interviewers want the commit-boundary and persisted-execution-state mechanics that distinguish Spring Batch from a plain loop.

The mechanical answer

Chunk processing commits every N items as one transaction. The JobRepository persists metadata about every execution: JobInstance (the logical job + parameters), JobExecution (one run), and StepExecution (one step's run with read/write/skip counts, status, and restart data). After a failure, the batch framework identifies the FAILED JobExecution, finds the last successfully committed chunk boundary of the failed StepExecution, and resumes the reader from there — so already-committed chunks are never reprocessed, and the partially-processed chunk is redone atomically. This restartability is what makes batch jobs safe to rerun. The reader must support restart (return a restart token / current position) for precise resumption; otherwise the job restarts from the beginning but still skips nothing that was committed.

Trap

The naive answer is 'Spring Batch just processes in chunks'. The chunk IS the atomic/restart unit, and the JobRepository persistence is what enables resumption. Another trap: assuming a restarted job re-reads from the start and skips duplicates — it resumes at the last committed chunk; if the reader doesn't implement restart properly, it starts over. Candidates who describe the commit boundary and the metadata tables show real Spring Batch understanding.

Under The Hood — a multi-depth technical interview hub.

Third-party licenses Press ⌘ K to search.