Quick revision
The short answer
In a normal chunk, Spring Batch opens a transaction, reads items one by one until it has 100, passes each through the processor, gives the writer all 100 in one call, then saves the step execution and its ExecutionContext and commits. If the writer throws a skippable exception, the whole chunk transaction rolls back, and Spring Batch then scans the chunk: it writes the items one at a time, each in its own transaction, so items 1 to 56 and 58 to 100 are written and item 57 is skipped. Processing may be repeated during the scan, so processors should be idempotent.
What to remember
In this answer, we followed one chunk of 100 items: inside one transaction the reader collects 100 items, the processor handles each, the writer receives the list, and the step execution and ExecutionContext are saved before the commit. When the writer throws a skippable exception on item 57 in a fault-tolerant step, the chunk rolls back and Spring Batch scans it, writing items one at a time in separate transactions to find and skip item 57. Because processing may run again during the scan, processors must be idempotent, and large commit intervals make scans expensive.
Your turn
Answer it aloud as you would in the interview, or type your answer below. Then show one part at a time and compare.
In a normal chunk, Spring Batch opens a transaction, reads items one by one until it has 100, passes each through the processor, gives the writer all 100 in one call, then saves the step execution and its ExecutionContext and commits. If the writer throws a skippable exception, the whole chunk transaction rolls back, and Spring Batch then scans the chunk: it writes the items one at a time, each in its own transaction, so items 1 to 56 and 58 to 100 are written and item 57 is skipped. Processing may be repeated during the scan, so processors should be idempotent.
Why this question is asked
The chunk is the heart of Spring Batch, and almost every surprising behaviour, from duplicate side effects to restarts that resume in the middle of a file, comes from how a chunk is read, written and committed. The writer failure case is where many people's mental model breaks: they expect Spring Batch to know which item failed, but the writer received a list and threw for the whole list. Interviewers ask this to see if we understand the transaction boundaries and the scan. In this answer, we will recap what a chunk-oriented step is, follow one chunk of 100 items, see what changes in a fault-tolerant step when item 57 fails in the writer, and draw the practical lessons for processors and commit intervals.
1A Quick Recap of Chunk-Oriented Steps
A chunk-oriented step processes data in groups. It has an ItemReader that returns one item per call and null when the input is finished, an optional ItemProcessor that transforms or filters one item, and an ItemWriter that receives a whole group, called a chunk, and writes it. The commit interval, set with chunk(100, transactionManager), is the chunk size: how many items go into each transaction.
A fault-tolerant step is one configured with .faultTolerant() and rules for skip or retry. Skip means that an item that throws a certain exception is left out and counted, and processing continues, up to a skip limit. Retry means the operation is tried again for exceptions that may be temporary. Without these, any exception fails the step.
2Inside One Chunk of 100
Here is what happens for one chunk on the normal path. The step's tasklet starts a transaction through the transaction manager. Inside it, the reader is called 100 times, and the items are kept in a list. Then each item is passed to the processor. Then the writer receives the list of processed items in one call. If all goes well, Spring Batch updates the StepExecution counts, asks every registered ItemStream, such as the reader, to put its current state into the ExecutionContext, saves both in the job repository, and commits.
7 messages in order. Play them, or step through them one at a time.
Notice that saving the ExecutionContext happens in the same transaction as the business write when the repository and the business tables share a database and transaction manager. That is the reason a restart is precise: the saved reader position, say line 4,300, always matches what was committed. If the commit fails, both roll back together.
3When the Writer Throws on Item 57
Now suppose item 57 has a value that breaks a database constraint, and the step is configured to skip that exception. The writer is called with all 100 items and the database rejects the batch. The writer throws. At this point Spring Batch does not know which item caused it: the exception came from writing a list. So the chunk transaction rolls back, and nothing from this chunk is in the database.
The read items are not lost, though. Spring Batch keeps the chunk's input items in memory, because the reader is not transactional and cannot simply re-read them. It now enters what is called scanning: it processes and writes the items again, one at a time, each in its own small transaction.
So the failing item is found by elimination. Each single-item write either commits or fails on its own, and the one that fails is recorded as a skip, with the SkipListener's onSkipInWrite called for it, so we can log it or store it in an error table. If the skip limit is exceeded, the step fails instead.
4Why Processing Can Run Again
During the scan, Spring Batch may call the processor again for each item, because the processed results from the failed attempt were part of a transaction that rolled back. By default the processor is treated as transactional, meaning its results are not trusted after a rollback, so it is re-run. There is a setting, processorNonTransactional(), that tells Spring Batch to cache the processed results and reuse them, which is useful when processing is expensive and has no side effects.
This is why processors must be idempotent: calling them twice with the same item must give the same result and cause no extra effect. A processor that sends an email, increments a counter in another system, or assigns the next number from a sequence would do it twice for most items in the chunk. Spring Batch 6 reworked parts of the chunk processing implementation, including this behaviour, so check your version's documentation for the exact caching rules.
5Why the Commit Interval Matters for Skips
The scan replaces one transaction with one transaction per item. With a commit interval of 100, one bad item costs about 100 extra small transactions, which is acceptable. With a commit interval of 5,000, one bad item costs 5,000 single-item transactions plus reprocessing 5,000 items, which can take minutes. If bad items are frequent, say one every few chunks, a large interval turns a fast job into a very slow one.
So choosing the interval is a trade-off between throughput on clean data, where bigger chunks mean fewer commits, and the cost of failures, where smaller chunks mean less rework. A common approach is to validate items in the processor, where a problem affects only that item, so that writer failures become rare.
6Follow-up questions
Why can a large commit interval make skip scanning slow?
Because scanning writes every item of the failed chunk in its own transaction and may process each again. The cost is proportional to the chunk size, not to the number of bad items. A 10,000-item chunk with one bad row means 10,000 commits and possibly 10,000 processor calls just to find that row. If the bad rows are spread out, nearly every chunk is scanned and the job slows dramatically. Smaller chunks, or validation before the writer, keep scans cheap.
What happens when the reader or processor throws instead of the writer?
A skippable exception from the reader skips that one item straight away, because Spring Batch knows exactly which read failed. A skippable exception from the processor also identifies the item, so the chunk is rolled back and retried without that item, with no need to scan. Only writer failures need the scan, because the writer works on a list.
Is the reader called again during the scan?
No. The items already read are kept with the chunk and reused. The reader has moved on, and its saved position is only committed with a successful chunk, so on a restart after a failure the reader goes back to the last committed position.
7Conclusion
In this answer, we followed one chunk of 100 items: inside one transaction the reader collects 100 items, the processor handles each, the writer receives the list, and the step execution and ExecutionContext are saved before the commit. When the writer throws a skippable exception on item 57 in a fault-tolerant step, the chunk rolls back and Spring Batch scans it, writing items one at a time in separate transactions to find and skip item 57. Because processing may run again during the scan, processors must be idempotent, and large commit intervals make scans expensive.
Open with
For a chunk with a commit interval of 100, Spring Batch starts a transaction, calls the reader until it has 100 items or the input ends, runs each item through the processor, and then calls the writer once with the list.
Then, if they want more
If the write succeeds, it updates the StepExecution counts, collects the reader's state into the ExecutionContext and saves both to the job repository, and then commits. Because that happens in the same transaction, the saved position always matches the committed data, which is what makes restart accurate.
If the writer throws on item 57 and the exception is skippable, Spring Batch cannot tell which item caused it, since the writer got a list. So it rolls back the whole chunk, keeps the read items in memory, and scans: it writes the items one at a time, each in its own transaction.
Items 1 to 56 and 58 to 100 commit individually, item 57 fails again and is recorded as a write skip, and the skip listener is called. During this scan the processor is usually called again for each item, unless it is marked non-transactional, so processors must be idempotent and should not have external side effects.
The scan cost grows with the chunk size, which is one reason not to use huge commit intervals when bad data is common.
About this answer
- Last updated
- 12 November 2026
- Checked
- Its structure and format are checked automatically. Use it as a carefully prepared study note: where a detail matters, confirm it against the documentation below. What the checks cover
- Where the question comes from
- Written by Scrivio for practice. It is not taken from an interview.
- Official documentation
- Spring Batch reference
Report an issue with this answer
Copy the line below and send it to the author with what you found. It carries the address of this page.
Issue with "Describe what happens inside one chunk of a Spring Batch chunk-oriented step with a...": https://scrivio-interview-library.netlify.app/spring-boot/spring-batch/4b461d9b34/