Spring Batch Basics Interview Questions and Answers
Master Spring Batch fundamentals with interview questions covering architecture, Job, Step, JobRepository, JobLauncher, ExecutionContext, JobExecution, StepExecution, chunk processing, and enterprise batch processing.
Introduction
Spring Batch is the industry-standard framework for building large-scale batch processing applications in Java. It is designed to process millions of records efficiently while providing transaction management, restartability, fault tolerance, scalability, monitoring, and parallel processing.
Spring Batch is widely used in:
- Banking
- Insurance
- Healthcare
- Retail
- Telecommunications
- Government
- Financial Reporting
Typical batch jobs include:
- CSV Processing
- Excel Processing
- Database Migration
- ETL Pipelines
- Payment Processing
- End-of-Day Settlement
- Report Generation
Spring Batch Architecture
flowchart LR
Scheduler --> JobLauncher
JobLauncher --> Job
Job --> Step1
Step1 --> Reader
Reader --> Processor
Processor --> Writer
Writer --> Database
Job --> JobRepository
JobRepository --> BatchMetadataDB
Q1. What is Spring Batch?
Answer
Spring Batch is an open-source framework for processing large volumes of data in a reliable and scalable manner.
It provides:
- Chunk Processing
- Transaction Management
- Retry
- Skip
- Restartability
- Parallel Processing
- Monitoring
- Scheduling Integration
Example
@EnableBatchProcessing
@Configuration
public class BatchConfiguration {
}
Benefits
- High Performance
- Fault Tolerance
- Enterprise Ready
- Restartable Jobs
- Scalable Processing
Q2. What is the Spring Batch Architecture?
Spring Batch consists of several core components.
flowchart TD
Application --> JobLauncher
JobLauncher --> Job
Job --> Step
Step --> ItemReader
ItemReader --> ItemProcessor
ItemProcessor --> ItemWriter
ItemWriter --> Database
Job --> JobRepository
Each component has a specific responsibility.
Q3. What is a Job?
A Job represents the complete batch process.
Examples
- Import Customers
- Generate Reports
- Process Payroll
- Load Transactions
Example
@Bean
Job importJob(JobRepository repository,
Step customerStep){
return new JobBuilder(
"customerJob",
repository)
.start(customerStep)
.build();
}
A job may contain one or many steps.
Q4. What is a Step?
A Step is a single unit of work inside a Job.
Examples
- Read CSV
- Validate Records
- Transform Data
- Save Database
- Generate Report
flowchart LR
Job --> Step1
Step1 --> ReadCSV
Job --> Step2
Step2 --> Validate
Job --> Step3
Step3 --> SaveDatabase
Q5. What is Chunk Processing?
Chunk Processing is the most common processing model.
Instead of writing every record immediately,
Spring Batch groups records into chunks.
Example
Read 100 Records
↓
Process 100 Records
↓
Write 100 Records
↓
Commit Transaction
Configuration
.chunk(100, transactionManager)
Chunk Flow
flowchart LR
Reader --> Chunk100
Chunk100 --> Processor
Processor --> Writer
Writer --> Commit
Advantages
- Better Performance
- Reduced Database Calls
- Efficient Transactions
Q6. What are ItemReader, ItemProcessor, and ItemWriter?
These are the core interfaces of Spring Batch.
| Component | Responsibility |
|---|---|
| ItemReader | Reads data |
| ItemProcessor | Processes data |
| ItemWriter | Writes data |
Pipeline
flowchart LR
CSV --> Reader
Reader --> Processor
Processor --> Writer
Writer --> Database
Example
reader.read();
processor.process(item);
writer.write(items);
Q7. What is JobRepository?
JobRepository stores metadata about every batch execution.
It stores
- JobExecution
- StepExecution
- ExecutionContext
- Job Parameters
- Status
- Start Time
- End Time
Metadata Architecture
flowchart TD
Job --> JobRepository
JobRepository --> BATCH_JOB_INSTANCE
JobRepository --> BATCH_JOB_EXECUTION
JobRepository --> BATCH_STEP_EXECUTION
JobRepository --> ExecutionContext
This enables restartability and monitoring.
Q8. What is JobLauncher?
JobLauncher starts batch jobs.
Example
jobLauncher.run(
job,
new JobParameters()
);
Execution Flow
sequenceDiagram
Scheduler->>JobLauncher: Start Job
JobLauncher->>Job: Execute
Job->>Step: Run
Step-->>Job: Complete
Job-->>Scheduler: Status
Q9. What are JobExecution and StepExecution?
JobExecution
Represents one execution of a job.
Stores
- Status
- Start Time
- End Time
- Exit Status
StepExecution
Represents execution details of each step.
Stores
- Read Count
- Write Count
- Skip Count
- Commit Count
- Rollback Count
Q10. Spring Batch Best Practices
Keep Jobs Restartable
Use JobRepository properly.
Prefer Chunk Processing
Better performance than processing one record at a time.
Keep Steps Small
Each step should perform one responsibility.
Monitor Metadata Tables
Track execution history.
Banking Example
flowchart TD
CsvFile["CSV File"] --> ItemReader
ItemReader --> ItemProcessor
ItemProcessor --> ItemWriter
ItemWriter --> CustomerTable["Customer Table"]
ItemWriter --> AuditTable["Audit Table"]
JobRepository --> BatchMetadata["Batch Metadata"]
Common Interview Questions
- What is Spring Batch?
- Explain Spring Batch architecture.
- What is a Job?
- What is a Step?
- Explain Chunk Processing.
- What is JobRepository?
- What is JobLauncher?
- Explain JobExecution and StepExecution.
- What are ItemReader, ItemProcessor, and ItemWriter?
- Spring Batch best practices?
Quick Revision
| Topic | Summary |
|---|---|
| Job | Complete batch process |
| Step | Unit of work |
| Chunk | Group of records processed together |
| ItemReader | Reads input |
| ItemProcessor | Processes records |
| ItemWriter | Writes output |
| JobRepository | Stores execution metadata |
| JobLauncher | Starts jobs |
| JobExecution | Job execution details |
| StepExecution | Step execution statistics |
Spring Batch Execution Lifecycle
sequenceDiagram
Scheduler->>JobLauncher: Launch Job
JobLauncher->>Job: Start
Job->>Step: Execute
Step->>ItemReader: Read
ItemReader-->>Step: Item
Step->>ItemProcessor: Process
ItemProcessor-->>Step: Processed Item
Step->>ItemWriter: Write Chunk
ItemWriter-->>Step: Success
Step->>JobRepository: Save Metadata
JobRepository-->>Job: Execution Status
Job-->>Scheduler: Completed
Production Example – Processing 1 Million CSV Records
A banking application receives a 1-million-row CSV file containing daily transactions.
The batch job executes as follows:
- ItemReader reads 1,000 records at a time.
- ItemProcessor validates transactions, removes duplicates, and enriches customer information.
- ItemWriter performs batch inserts into PostgreSQL.
- A transaction is committed after every 1,000 records.
- If processing fails after 650,000 records, Spring Batch uses the JobRepository and ExecutionContext to restart from the last successful checkpoint instead of starting from the beginning.
flowchart LR
CSVFile --> Reader
Reader --> Chunk1000
Chunk1000 --> Processor
Processor --> Writer
Writer --> PostgreSQL
Writer --> Commit
Commit --> JobRepository
This restart capability is one of the biggest advantages of Spring Batch over custom file-processing solutions.
Key Takeaways
- Spring Batch is the standard Java framework for processing large-scale batch workloads.
- A Job represents the entire batch process, while a Step represents an individual unit of work.
- Chunk Processing improves performance by processing and committing records in batches.
- ItemReader, ItemProcessor, and ItemWriter form the core processing pipeline.
- JobRepository stores execution metadata that enables monitoring and restartability.
- JobLauncher is responsible for starting batch jobs.
- JobExecution tracks overall job execution, while StepExecution tracks metrics for individual steps.
- Spring Batch provides built-in support for transactions, retries, skips, parallel processing, and job restart.
- It is widely used for ETL pipelines, financial settlements, report generation, and processing millions of database or file records in enterprise systems.