Quick revision
The short answer
Implement ItemStreamReader so the reader takes part in Spring Batch's state saving: in update() store the page token, or cursor, of the current page and the index within it in the ExecutionContext, and in open() read them back and fetch that page again. Because the ExecutionContext is saved with each chunk commit, the saved position always matches the committed data. If the partner API is not stable between runs, copy its data into a staging table first and process from there, and if the reader is wrapped by another component, register it as a stream so its state is saved at all.
What to remember
In this answer, we made a custom REST reader restartable by implementing ItemStreamReader: update() saves the current page token and the index within the page in the ExecutionContext, and open() restores them and refetches that page. Because the context is saved with every chunk commit, the position always matches committed data. When page tokens are not stable between runs, we stage the API data in a table first and process from there, and we register a wrapped reader as a stream so its state is saved at all.
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.
Implement ItemStreamReader so the reader takes part in Spring Batch's state saving: in update() store the page token, or cursor, of the current page and the index within it in the ExecutionContext, and in open() read them back and fetch that page again. Because the ExecutionContext is saved with each chunk commit, the saved position always matches the committed data. If the partner API is not stable between runs, copy its data into a staging table first and process from there, and if the reader is wrapped by another component, register it as a stream so its state is saved at all.
Why this question is asked
Many batch jobs now read from REST APIs rather than files: a partner exposes orders or claims through an endpoint that returns one page at a time with a token for the next page. Spring Batch has no built-in reader for an arbitrary paginated API, so we write one, and the hard part is not fetching pages but surviving failure. A job that dies after 300,000 items should not fetch and process them again. Interviewers ask this because it tests whether we understand how Spring Batch saves reader state. In this answer, we will recap the ItemStream contract, write a reader that saves its page position, see why it matches committed data, deal with unstable APIs, and look at the registration trap.
1A Quick Recap of the ItemStream Contract
Spring Batch saves a reader's position only if the reader implements ItemStream, an interface with three methods. open(ExecutionContext) is called when the step starts and receives the saved state, which is empty on a first run. update(ExecutionContext) is called just before each chunk commits, and the reader writes its current position into the context. close() is called at the end. ItemStreamReader combines ItemReader and ItemStream in one interface.
The ExecutionContext is a small map of keys and values that belongs to the step execution and is stored in the job repository. For a REST API, the position is usually the token of the page we are on, plus how many items of that page we have already handed out.
2A Reader That Saves Its Page Position
Here is a reader for an API that returns up to 500 orders per call and a nextToken for the following page. It keeps the current page in memory and an index into it. Its state is two values: the token used to fetch the current page and the index of the next item to return.
On a restart, open() receives the saved token and index, fetches that same page again, and skips the 230 items already processed. Notice that we do not save nextToken. Saving the token of the page we are reading from is what allows us to refetch it; the next token comes back with that page. Another option is to extend AbstractItemCountingItemStreamItemReader, which saves an item count for us, but on restart it reaches the position by reading and discarding items, which for an API means fetching every earlier page again.
3Why the Saved Position Matches Committed Data
The order of events inside a chunk is what makes this correct. Spring Batch calls read() until the chunk is full, processes and writes the items, then calls update() on every registered stream, saves the ExecutionContext in the job repository, and commits, all in one transaction. So the saved token and index describe exactly the position after the last item that was committed. If the chunk fails before the commit, the saved state from the previous commit remains, and a restart begins just after the previous chunk.
8 messages in order. Play them, or step through them one at a time.
4When the API Is Not Stable Between Runs
This design assumes something the partner may not guarantee: that the page token p7 still returns the same items an hour later. Many APIs have tokens that expire after minutes, or pages computed from a live query, so new orders shift items between pages. Restarting from token p7 with index 230 could then skip some orders or repeat others.
When we cannot trust the API to be stable, the safer design uses two steps. The first step copies everything from the API into a staging table, keyed by the partner's order id, with an upsert so a repeated copy is harmless. The second step processes from the staging table with a normal restartable database reader. The API is then read once, quickly, and all the expensive processing restarts against data we control.
- Do page tokens stay valid and stable between runs?YesRestartable reader saving token and indexNoStage first
- Step 1: copy API pages into a staging tableupsert by partner order id
- Step 2: process from the staging table
JdbcPagingItemReader, restartable by key
3 steps in order. Play them, or step through them one at a time.
5The Registration Trap
Spring Batch only calls open() and update() on readers it knows about. A reader passed directly to .reader() is registered automatically. But if we wrap our reader, for example in a class that adds logging or rate limiting, the step sees only the wrapper. Unless the wrapper itself implements ItemStream and forwards the calls, the inner reader's state is never saved, and every restart begins at the first page. The fix is to register the inner reader explicitly with .stream(partnerOrderReader) on the step builder, or to make the wrapper an ItemStreamReader that delegates open, update and close.
We should also give the state keys a unique prefix, as in partnerOrders.pageToken, so two readers in the same step do not overwrite each other's values.
6Follow-up questions
Should the reader save state in a multi-threaded step?
No. In a multi-threaded step, chunks finish out of order, so no single saved position is correct. Such readers usually set saveState to false and accept that a restart begins from scratch, or the job uses partitioning instead, where each partition has its own reader and its own saved state.
How do I test that the reader really resumes?
Write an integration test that runs the step against a fake API, makes the writer throw on a chosen item in a later chunk, then restarts the same job instance and checks that the API was asked for the expected page and that no item was written twice. Spring Batch's test utilities make it easy to launch a single step and inspect its ExecutionContext.
What about rate limits and retries on the API calls?
Fetching a page should retry on temporary errors with backoff, and respect the partner's rate limit. Because fetching happens in read(), inside the chunk transaction, long waits there keep the transaction open, which is another reason the staging approach is attractive for slow or unreliable APIs.
7Conclusion
In this answer, we made a custom REST reader restartable by implementing ItemStreamReader: update() saves the current page token and the index within the page in the ExecutionContext, and open() restores them and refetches that page. Because the context is saved with every chunk commit, the position always matches committed data. When page tokens are not stable between runs, we stage the API data in a table first and process from there, and we register a wrapped reader as a stream so its state is saved at all.
Open with
I would implement ItemStreamReader so Spring Batch includes the reader in its state saving. The reader keeps the current page in memory, the token used to fetch that page, and the index of the next item.
Then, if they want more
In update, which the step calls just before each chunk commit, I put the token and the index into the ExecutionContext under keys with a unique prefix. In open, I read them back, fetch that page again with the saved token, and set the index, so the reader continues right after the last committed item.
This is correct because the ExecutionContext is saved in the same transaction as the chunk's writes, so the saved position always matches what was committed; if a chunk fails, the previous position is still there. I would not extend the counting reader base class here, because it resumes by reading and discarding items, which would refetch every earlier page.
Then I would check whether the API is stable between runs. If tokens expire or pages shift as new data arrives, resuming from a token is unsafe, so I would split the job: one step copies the API into a staging table with upserts, and a second processes from that table with a restartable database reader.
Finally, if the reader is wrapped by another component, I would register it with stream on the step builder, otherwise its state is never saved.
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 "You are writing a custom ItemReader that reads from a paginated partner REST API in a...": https://scrivio-interview-library.netlify.app/spring-boot/spring-batch/dafb28f812/