Chapter 6.1☕ 16 min read

Spring Batch Intro

When a for loop is not enough. Enterprise-grade batch processing.

01The Concept: Batch Processing

The Hyderabad Mega Kitchen Analogy:

Think of the massive catering kitchens in Hyderabad that cook for thousands of people during weddings. If they cooked one plate at a time, it would take weeks. Instead, they use an assembly line: one team washes 100kg of rice (Reader), one team cooks it in massive vessels (Processor), and one team serves it into batches of 100 plates (Writer).

Spring Batch works exactly like this. It doesn’t process data one by one. It reads data in “chunks” (e.g., 100 records at a time), processes them, and writes them to the database in bulk. If a failure happens at record 5000, it knows exactly where it stopped and can restart from there.

02Technical Explanation
  1. Job: The entire batch process. It represents the end-to-end task (e.g., “User Data Migration Job”).
  2. Step: An independent phase of the job. A job usually has multiple steps.
  3. JobRepository: The database where Spring Batch stores metadata (which jobs ran, which failed, at which record they stopped). This is what makes Spring Batch restartable.
03Full Working Code: A Simple Tasklet Job

In Spring Boot 3.x, we don’t need @EnableBatchProcessing anymore; it is auto-configured. Let’s create a simple Job with one Step that prints a message.

<!-- pom.xml Dependencies -->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-batch</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-data-jpa</artifactId>
</dependency>
<dependency>
<groupId>com.h2database</groupId>
<artifactId>h2</artifactId>
<scope>runtime</scope>
</dependency>
package com.devinhyderabad;

import org.springframework.batch.core.Job;
import org.springframework.batch.core.Step;
import org.springframework.batch.core.job.builder.JobBuilder;
import org.springframework.batch.core.repository.JobRepository;
import org.springframework.batch.core.step.builder.StepBuilder;
import org.springframework.batch.core.step.tasklet.Tasklet;
import org.springframework.batch.repeat.RepeatStatus;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.transaction.PlatformTransactionManager;

@Configuration
public class BatchConfig {

// 1. Define the Tasklet (the actual work to be done)
@Bean
public Tasklet printMessageTasklet() {
return (contribution, chunkContext) -> {
System.out.println("Batch processing started in Hyderabad Mega Kitchen!");
return RepeatStatus.FINISHED;
};
}

// 2. Define the Step
@Bean
public Step step1(JobRepository jobRepository, PlatformTransactionManager transactionManager) {
return new StepBuilder("step1", jobRepository)
.tasklet(printMessageTasklet(), transactionManager)
.build();
}

// 3. Define the Job
@Bean
public Job processDataJob(JobRepository jobRepository, Step step1) {
return new JobBuilder("processDataJob", jobRepository)
.start(step1)
.build();
}
}

When you start your app, Spring Boot will automatically run this Job once at startup, and you will see the message in your console!

04Code Walkthrough

The code above demonstrates three essential Spring Batch beans:

  • Tasklet: The actual unit of work. It returns RepeatStatus.FINISHED to signal the step is complete.
  • Step: Wraps the Tasklet. Notice the new Spring Batch 5 API using StepBuilder with explicit JobRepository.
  • Job: Wraps steps. Here we call .start(step1) — you can chain multiple steps later.
05Why It Matters / Interview Note

Interview Question: “Why use Spring Batch instead of a standard @Scheduled loop or writing a simple for loop?”

Answer: A simple for loop processes everything in memory, causing OutOfMemoryError for large datasets. It also has no restartability — if the server crashes at record 50,000, you have to start from 0. Spring Batch processes data in chunks (managing memory) and stores metadata in a database, allowing it to restart exactly where it left off.

Enterprise Note: In Spring Boot 3.x (Spring Batch 5), the old JobBuilderFactory and StepBuilderFactory were removed. You must use the new JobBuilder and StepBuilder, passing the JobRepository explicitly to them.

Key Takeaways

  • ✅ Spring Batch is designed for processing large volumes of data reliably
  • ✅ Job = end-to-end task, Step = independent phase, JobRepository = restart metadata
  • ✅ Chunk processing reads/processes/writes data in batches to manage memory
  • ✅ Spring Batch is restartable — can resume from where it failed
  • ✅ Spring Batch 5 uses new JobBuilder/StepBuilder API with explicit JobRepository