Spring Batch Job and Step Interview Questions and Answers
Master Spring Batch Jobs and Steps with interview questions covering Job lifecycle, Step lifecycle, JobInstance, JobExecution, StepExecution, Flow, Split, Decision, ExecutionContext, and production best practices.
Introduction
A Job and Step are the two most fundamental building blocks of Spring Batch.
- A Job represents the entire batch process.
- A Step represents an individual unit of work within a Job.
Understanding how Jobs and Steps interact is one of the most frequently tested topics in Spring Batch interviews, especially for senior Java developers working with ETL pipelines, financial systems, and large-scale data processing.
Spring Batch Job and Step Architecture
flowchart LR
Scheduler --> JobLauncher
JobLauncher --> Job
Job --> Step1
Step1 --> Reader
Reader --> Processor
Processor --> Writer
Job --> Step2
Step2 --> GenerateReport
Job --> JobRepository
JobRepository --> BatchMetadata
Q1. What is a Job in Spring Batch?
Answer
A Job represents an entire batch process.
Examples
- Import Customer File
- Process Payroll
- Generate Daily Reports
- Import Bank Transactions
Example
@Bean
Job importCustomerJob(
JobRepository repository,
Step customerStep){
return new JobBuilder(
"customerJob",
repository)
.start(customerStep)
.build();
}
A Job can contain one or multiple Steps executed sequentially or in parallel.
Q2. What is a Step?
A Step is an independent phase of a Job.
Examples
- Read CSV
- Validate Data
- Transform Records
- Save Database
- Generate Audit Report
Architecture
flowchart LR
Job --> Step1
Step1 --> ReadCSV
Job --> Step2
Step2 --> Validate
Job --> Step3
Step3 --> SaveDatabase
Each Step has its own transaction boundary and execution statistics.
Q3. What is the Job Lifecycle?
Job lifecycle
stateDiagram-v2
[*] --> STARTING
STARTING --> STARTED
STARTED --> COMPLETED
STARTED --> FAILED
STARTED --> STOPPED
FAILED --> RESTARTED
RESTARTED --> COMPLETED
Possible Job statuses
- STARTING
- STARTED
- COMPLETED
- FAILED
- STOPPED
- ABANDONED
Q4. What is the Step Lifecycle?
Each Step executes independently.
stateDiagram-v2
[*] --> STARTING
STARTING --> READING
READING --> PROCESSING
PROCESSING --> WRITING
WRITING --> COMMIT
COMMIT --> COMPLETED
PROCESSING --> FAILED
Every Step records
- Read Count
- Write Count
- Skip Count
- Commit Count
- Rollback Count
Q5. What is the difference between JobInstance and JobExecution?
This is one of the most common interview questions.
| JobInstance | JobExecution |
|---|---|
| Logical batch job | Actual execution |
| Created once | Created every run |
| Uses JobParameters | Linked to JobInstance |
| Represents business execution | Represents runtime execution |
Example
CustomerImportJob
│
├── Execution #1 → FAILED
│
└── Execution #2 → COMPLETED
Diagram
flowchart TD
JobInstance --> Execution1
Execution1 --> Failed
JobInstance --> Execution2
Execution2 --> Completed
Q6. What is StepExecution?
StepExecution stores execution details for one Step.
Information stored
- Read Count
- Write Count
- Skip Count
- Commit Count
- Rollback Count
- Exit Status
Example
Step Name : customerImport
Read Count : 100000
Write Count : 99850
Skip Count : 150
Commit Count : 100
Q7. What is ExecutionContext?
ExecutionContext stores state information for restartability.
Example
executionContext.putLong(
"lastProcessedRow",
250000L
);
After restart
Long row =
executionContext.getLong(
"lastProcessedRow"
);
Restart Flow
flowchart LR
Process100000Rows --> SaveCheckpoint
SaveCheckpoint --> ExecutionContext
Failure --> Restart
Restart --> ContinueFromCheckpoint
Q8. What are Job Flow and Split?
A Job can execute Steps sequentially or in parallel.
Sequential Flow
flowchart LR
Job --> Step1
Step1 --> Step2
Step2 --> Step3
Parallel Split
flowchart TD
Job --> Split
Split --> StepA
Split --> StepB
Split --> StepC
StepA --> End
StepB --> End
StepC --> End
Parallel execution improves throughput for independent processing.
Q9. What is Job Flow Decision?
Spring Batch supports conditional execution.
Example
flowchart TD
Start --> Validate
Validate --> Valid
Validate --> Invalid
Valid --> Process
Invalid --> Reject
Process --> End
Reject --> End
Configuration
.next(decider())
.from(decider())
.on("FAILED")
.to(errorStep)
This allows dynamic execution paths.
Q10. Job and Step Best Practices
Keep Jobs Small
One business process per Job.
Keep Steps Focused
Each Step should perform a single responsibility.
Make Jobs Restartable
Always configure JobRepository correctly.
Use ExecutionContext
Store checkpoints instead of temporary files.
Design Independent Steps
Independent Steps simplify retries and parallel execution.
Banking Example
flowchart TD
TransactionCSV --> ImportStep
ImportStep --> ValidationStep
ValidationStep --> FraudCheckStep
FraudCheckStep --> SettlementStep
SettlementStep --> AuditStep
AuditStep --> Completed
Each Step performs a single business responsibility.
Common Interview Questions
- What is a Job?
- What is a Step?
- JobInstance vs JobExecution?
- What is StepExecution?
- Explain ExecutionContext.
- What is Job Flow?
- What is Split?
- What is Job Decider?
- How does restartability work?
- Job and Step best practices?
Quick Revision
| Topic | Summary |
|---|---|
| Job | Complete batch process |
| Step | Single unit of work |
| JobInstance | Logical batch job |
| JobExecution | Runtime execution |
| StepExecution | Step runtime details |
| ExecutionContext | Restart checkpoint storage |
| Flow | Sequential execution |
| Split | Parallel execution |
| Decider | Conditional routing |
| JobRepository | Stores batch metadata |
Job Execution Lifecycle
sequenceDiagram
Scheduler->>JobLauncher: Start Job
JobLauncher->>Job: Create JobExecution
Job->>Step1: Execute
Step1->>ExecutionContext: Save Checkpoint
Step1-->>Job: Completed
Job->>Step2: Execute
Step2-->>Job: Completed
Job->>JobRepository: Store Metadata
JobRepository-->>Scheduler: Job Completed
Production Example – 1 Million Record Import
A banking system receives a 1 million transaction CSV every night.
The batch consists of five Steps:
- Import Step – Reads the CSV.
- Validation Step – Validates account numbers and transaction amounts.
- Fraud Detection Step – Checks suspicious transactions.
- Settlement Step – Updates account balances.
- Audit Step – Writes audit logs.
If the job fails during the Fraud Detection Step after processing 650,000 records, Spring Batch retrieves the last checkpoint from the ExecutionContext and restarts from that point instead of processing the entire file again.
flowchart LR
CSVFile --> Import
Import --> Validate
Validate --> FraudCheck
FraudCheck --> Settlement
Settlement --> Audit
Audit --> Completed
FraudCheck --> Failure
Failure --> Restart
Restart --> ExecutionContext
ExecutionContext --> FraudCheck
Key Takeaways
- A Job represents the complete batch process, while a Step represents an individual unit of work.
- Every Job execution creates a JobExecution, and every Step execution creates a StepExecution.
- A JobInstance represents the logical batch job, whereas JobExecution represents a specific runtime execution.
- ExecutionContext stores checkpoint information, enabling restartability after failures.
- Jobs can execute Steps sequentially, conditionally, or in parallel using Flows, Splits, and Deciders.
- Each Step maintains execution metrics such as read count, write count, skip count, commit count, and rollback count.
- Design Jobs around business processes and Steps around single responsibilities.
- Proper Job and Step design improves scalability, fault tolerance, maintainability, and production reliability in enterprise batch applications.