Quick revision
The short answer
JobInstanceAlreadyCompleteException means a JobInstance with exactly those identifying parameters already completed, and Spring Batch never runs a completed instance again; to run again we change an identifying parameter or use an incrementer such as RunIdIncrementer. The restart that read the file from the beginning happened because the reader did not save its position: it was not registered as an ItemStream, had saveState set to false, or is not a restartable reader. A restartable reader saves its position in the step ExecutionContext at each commit and restores it in open(), and steps with allowStartIfComplete(true) re-run even when complete, which can also create duplicates.
What to remember
In this answer, we explained both incidents with the same model. JobInstanceAlreadyCompleteException means the instance with those identifying parameters already completed, and running again needs a new identifying parameter or an incrementer. A restart that reads from the beginning means the reader did not save its position, because it was not registered as an ItemStream, had saveState off, or was not restartable; a working reader saves its position in the step ExecutionContext at each commit and restores it in open(). We also saw that allowStartIfComplete(true) re-runs completed steps and can duplicate data.
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.
JobInstanceAlreadyCompleteException means a JobInstance with exactly those identifying parameters already completed, and Spring Batch never runs a completed instance again; to run again we change an identifying parameter or use an incrementer such as RunIdIncrementer. The restart that read the file from the beginning happened because the reader did not save its position: it was not registered as an ItemStream, had saveState set to false, or is not a restartable reader. A restartable reader saves its position in the step ExecutionContext at each commit and restores it in open(), and steps with allowStartIfComplete(true) re-run even when complete, which can also create duplicates.
Why this question is asked
Restart is the main reason teams choose Spring Batch, so when it does something unexpected at 3 a.m., the on-call engineer needs to know whether the framework refused a run on purpose or silently lost its place. These two behaviours look unrelated, but both come from the same machinery: job instances identified by parameters, and reader state saved with each commit. Interviewers ask this to see whether we can debug from the job repository rather than guessing. In this answer, we will recap instances and restarts, explain the exception and how to run again on purpose, then reproduce a reader that starts from the beginning, see how saved state should work, and look at allowStartIfComplete.
1A Quick Recap of Instances, Executions and Restart
A JobInstance is one logical run of a job, identified by the job name plus its identifying parameters, such as date=2026-11-03. A JobExecution is one attempt at that instance. When an execution fails, launching the job again with the same identifying parameters is a restart: Spring Batch creates a new JobExecution of the same instance, skips steps that already completed, and resumes the failed step from the state saved at its last commit. All of this is recorded in the job repository tables.
2The First Behaviour: JobInstanceAlreadyCompleteException
The exception tells us that the instance we asked for is already COMPLETED. Most likely, the job did not actually fail in the way the operator thought, or an earlier rerun had already completed it. Spring Batch refuses deliberately: running the same instance again would process the same day's data twice, which in a financial job means double postings. A completed instance can never be restarted.
In this example, the 3 a.m. execution 501 failed, someone restarted it as 502, which completed, and the operator then tried again. Querying the repository first would have shown it. If we really need to run the job again for the same date, for example after reversing a bad load, we make it a new instance by changing an identifying parameter. RunIdIncrementer adds a run.id parameter that increases on each launch, and Spring Boot applies a job's incrementer when it launches the job. The safer habit is an explicit parameter such as rerun=1, because it records why the second instance exists.
3The Second Behaviour: a Restart That Starts From Line One
The other incident is the opposite: the restart was accepted, but the step read the file from the start, and rows already committed were inserted again. That means the reader had no saved position to restore. Spring Batch only saves the state of components that implement ItemStream, an interface with open, update and close methods, and that the step knows about. A reader passed directly to .reader() is registered automatically. A reader hidden inside another bean is not.
Two other causes give the same symptom. A reader built with saveState(false) deliberately does not save its position, which is correct in a multi-threaded step but wrong here. And some readers are not restartable at all, for example a custom reader that reads from a queue or an API without saving any position. In each case the step on restart begins with an empty state and reads from the beginning.
4How Saved State Should Work
When a registered reader is working properly, the cycle is simple. At each chunk commit, Spring Batch calls update(executionContext) on every registered stream, and FlatFileItemReader writes how many lines it has read, under a key built from its name, such as importReader.read.count. The ExecutionContext is saved in the same transaction as the chunk's data. On restart, the step loads the saved ExecutionContext and calls open(executionContext), and the reader skips forward to that line.
- Chunk commits
update():importReader.read.count= 4000 saved with the data - Next chunk fails at line 4,600rollback, saved count stays 4000
- Operator restarts the same instancenew
JobExecution, sameJobInstance - open() with saved contextreader skips to line 4,001
- Was the reader registered with saveState on?YesResume at line 4,001, no duplicatesNoStart at line 1, duplicates
5 steps in order. Play them, or step through them one at a time.
Notice that the reader's name matters here, because it forms the key of the saved state. Two readers with the same name in one step overwrite each other's state, and renaming a reader between a failure and its restart makes the restart start from scratch.
5AllowStartIfComplete Can Also Re-Run Work
A step that completed in an earlier execution is normally skipped on restart. Setting allowStartIfComplete(true) on a step changes that: the step runs again on every restart. That is useful for setup steps, such as cleaning a staging table before a load, but dangerous on a step that writes data, because a restart then reprocesses everything it did before. So when we see duplicates after a restart, we check the step configuration too, and the BATCH_STEP_EXECUTION table will show the step running in both executions.
6Follow-up questions
How can we make a restart from the beginning harmless?
Make the writer idempotent, for example with an upsert keyed on the business id, or delete the instance's rows at the start of the step. Then even if the reader loses its place, the result is the same. Restartable readers are still important for speed, but idempotent writes are what protect the data.
What is the difference between a restart and a re-run?
A restart continues the same JobInstance after a failure or stop, with a new execution that resumes from saved state. A re-run is a new JobInstance with different identifying parameters, which starts fresh. Restart is the normal recovery; a re-run is a deliberate decision to do the work again.
Why would an operator get the exception after a real failure?
Sometimes a job is launched with an incrementer by the scheduler but restarted by hand without it, or the other way round, so the parameters do not match and a different instance is targeted. Comparing the parameters in BATCH_JOB_EXECUTION_PARAMS for each execution usually explains it.
7Conclusion
In this answer, we explained both incidents with the same model. JobInstanceAlreadyCompleteException means the instance with those identifying parameters already completed, and running again needs a new identifying parameter or an incrementer. A restart that reads from the beginning means the reader did not save its position, because it was not registered as an ItemStream, had saveState off, or was not restartable; a working reader saves its position in the step ExecutionContext at each commit and restores it in open(). We also saw that allowStartIfComplete(true) re-runs completed steps and can duplicate data.
Open with
The first behaviour is Spring Batch protecting us. A JobInstance is identified by the job name and its identifying parameters, and once an instance has completed it can never be run again, so the exception means that the job for those parameters had already completed, probably through an earlier restart that the operator did not see.
Then, if they want more
I would check BATCH_JOB_EXECUTION for the instance before doing anything. If we truly need to run it again, we create a new instance by changing an identifying parameter, for example with RunIdIncrementer or an explicit rerun parameter. The second behaviour means the reader had no saved position.
Readers save state through the ItemStream interface: at every commit the step calls update and stores the position in the step ExecutionContext, in the same transaction as the data, and on restart it calls open, which skips to that position. That only works if the reader is registered with the step.
If it is wrapped inside another bean, it has to be registered with stream(), or the wrapper has to implement ItemStream itself. Other causes are saveState set to false, or a custom reader that is not restartable. I would also check for steps marked allowStartIfComplete true, because those run again on every restart.
And I would make the writer idempotent, so a restart from the beginning cannot create duplicates.
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 "A Spring Batch job failed at 3 a.m. and the operator re-ran it with the same parameters...": https://scrivio-interview-library.netlify.app/spring-boot/spring-batch/dbe98c68e0/