Spring Batch Retry and Skip Interview Questions and Answers
Master Spring Batch Retry and Skip with interview questions covering fault tolerance, retry policy, skip policy, rollback, exception handling, retry limits, skip limits, and enterprise production best practices.
Introduction
In enterprise batch processing, failures are inevitable. Files may contain invalid records, databases may experience temporary connectivity issues, APIs may timeout, or external services may become unavailable.
Spring Batch provides Fault Tolerance through two powerful mechanisms:
- Retry – Try processing the same item again.
- Skip – Ignore the problematic item and continue processing.
These features allow long-running batch jobs to complete successfully even when some records or temporary infrastructure failures occur.
Typical enterprise use cases include:
- Banking Transaction Imports
- Insurance Claim Processing
- Payroll Systems
- ETL Pipelines
- Customer Data Migration
- Financial Settlement
Fault Tolerance Architecture
flowchart LR
ItemReader --> ItemProcessor
ItemProcessor --> ItemWriter
ItemWriter --> Success
ItemWriter --> Exception
Exception --> Retry
Retry --> Success
Retry --> Skip
Skip --> Continue
Q1. What is Fault Tolerance in Spring Batch?
Answer
Fault Tolerance allows batch jobs to continue processing even when errors occur.
Spring Batch supports
- Retry
- Skip
- Rollback
- Restart
- Recovery
Configuration
.faultTolerant()
Benefits
- Better reliability
- High availability
- Minimal manual intervention
- Improved production stability
Q2. What is Retry?
Retry attempts to process the same item again after an exception occurs.
Configuration
.faultTolerant()
.retry(SQLException.class)
.retryLimit(3)
Flow
flowchart TD
ProcessItem --> Exception
Exception --> Retry1
Retry1 --> Retry2
Retry2 --> Retry3
Retry3 --> Success
Typical Retry Scenarios
- Database deadlock
- Temporary network failure
- API timeout
- Message broker unavailable
Q3. What is Skip?
Skip ignores an item that cannot be processed and continues with the next item.
Configuration
.faultTolerant()
.skip(ValidationException.class)
.skipLimit(100)
Flow
flowchart TD
ProcessItem --> Exception
Exception --> Skip
Skip --> NextItem
Typical Skip Scenarios
- Invalid email
- Missing mandatory field
- Corrupt CSV row
- Invalid account number
Q4. Retry vs Skip
| Retry | Skip |
|---|---|
| Reprocess item | Ignore item |
| Used for temporary failures | Used for bad data |
| Same record retried | Continue to next record |
| Infrastructure issue | Business data issue |
| May eventually succeed | Record is discarded |
Q5. What is RetryLimit?
RetryLimit specifies how many times Spring Batch retries an item.
Example
.retryLimit(5)
Flow
Attempt 1
↓
Attempt 2
↓
Attempt 3
↓
Attempt 4
↓
Attempt 5
↓
Fail
Choose retry limits carefully to avoid excessive processing delays.
Q6. What is SkipLimit?
SkipLimit specifies how many records may be skipped before the job fails.
Example
.skipLimit(50)
Scenario
Skipped
49 Records
↓
Continue
Skipped
51 Records
↓
Job Failed
Skip limits help detect poor-quality input data.
Q7. How do Retry and Transactions work?
Each chunk executes within a transaction.
If one item fails
- Retry first
- Skip if configured
- Rollback if necessary
Transaction Flow
sequenceDiagram
Chunk->>Processor: Process Item
Processor->>Writer: Write
Writer-->>Processor: Exception
Processor->>RetryPolicy: Retry
RetryPolicy-->>Processor: Success
Processor->>Transaction: Commit
If retries fail and skipping is not configured, the chunk rolls back.
Q8. Which Exceptions should be Retried?
Retry Examples
- SQLException
- DeadlockLoserDataAccessException
- SocketTimeoutException
- ConnectException
- HttpServerErrorException
Do not retry
- ValidationException
- NullPointerException
- IllegalArgumentException
- DataIntegrityViolationException (usually indicates invalid data)
Q9. Which Exceptions should be Skipped?
Skip Examples
- ValidationException
- FlatFileParseException
- BindException
- InvalidRecordException
- MissingFieldException
Skip Flow
flowchart LR
CSVRecord --> Validation
Validation --> Valid
Validation --> Invalid
Valid --> Database
Invalid --> Skip
Skip --> NextRecord
The job continues processing remaining valid records.
Q10. Retry and Skip Best Practices
Retry Only Temporary Failures
Examples
- Database unavailable
- Network timeout
- API temporarily unavailable
Skip Invalid Business Data
Examples
- Invalid email
- Corrupt CSV row
- Missing required field
Configure Reasonable Limits
Avoid infinite retries or unlimited skips.
Log Every Retry and Skip
Capture detailed information for auditing and troubleshooting.
Banking Example
flowchart TD
TransactionReader --> ValidationProcessor
ValidationProcessor --> TransactionWriter
TransactionWriter --> Success
TransactionWriter --> DatabaseFailure
DatabaseFailure --> Retry
Retry --> Success
Retry --> Skip
Skip --> AuditTable
Failed or skipped transactions are typically recorded in an audit table for later review.
Common Interview Questions
- What is Fault Tolerance?
- What is Retry?
- What is Skip?
- Retry vs Skip?
- What is RetryLimit?
- What is SkipLimit?
- Which exceptions should be retried?
- Which exceptions should be skipped?
- How do transactions interact with Retry?
- Retry and Skip best practices?
Quick Revision
| Topic | Summary |
|---|---|
| Fault Tolerance | Continue processing after failures |
| Retry | Reprocess failed item |
| Skip | Ignore failed item |
| RetryLimit | Maximum retry attempts |
| SkipLimit | Maximum skipped records |
| Rollback | Undo current transaction |
| ValidationException | Usually skipped |
| SQLException | Usually retried |
| Chunk | Transaction boundary |
| Audit | Log skipped/retried items |
Retry and Skip Lifecycle
sequenceDiagram
ItemReader->>ItemProcessor: Item
ItemProcessor->>ItemWriter: Write
ItemWriter-->>ItemProcessor: Exception
ItemProcessor->>RetryPolicy: Retry
RetryPolicy-->>ItemProcessor: Retry Failed
ItemProcessor->>SkipPolicy: Skip Item
SkipPolicy-->>ItemProcessor: Continue
ItemProcessor->>ItemReader: Next Item
Production Example – Processing a 1 Million Transaction File
A banking application imports 1 million daily transactions.
Configuration:
- Chunk size = 1000
- Retry SQLExceptions up to 3 times because database deadlocks are temporary.
- Skip ValidationException for invalid customer records.
- Skip limit = 100 invalid records.
- If more than 100 invalid records are encountered, the job fails because the input file quality is unacceptable.
- Every skipped record is written to an error audit table for manual investigation.
flowchart LR
CSVReader --> ItemProcessor
ItemProcessor --> ItemWriter
ItemWriter --> PostgreSQL
ItemWriter --> SQLException
SQLException --> Retry3Times
Retry3Times --> Success
Retry3Times --> Skip
Skip --> ErrorAuditTable
This strategy ensures temporary infrastructure issues are recovered automatically while invalid business data is isolated without interrupting the entire batch job.
Key Takeaways
- Fault Tolerance enables Spring Batch jobs to continue processing despite failures.
- Retry is appropriate for temporary infrastructure failures, while Skip is appropriate for invalid business data.
- Use RetryLimit to control how many times a failed item is retried.
- Use SkipLimit to prevent excessive invalid records from silently degrading data quality.
- Retry occurs before Skip; if retries are exhausted and the exception is skippable, processing continues with the next item.
- Chunk transactions ensure consistency by rolling back failed chunks when necessary.
- Log every retry and skipped record for operational visibility and auditing.
- Proper Retry and Skip configuration improves resilience, reduces manual intervention, and keeps large enterprise batch jobs running reliably.