Quick revision
The short answer
The REST call is not part of the chunk's database transaction, so when the chunk later rolls back, the database rows disappear but the partner's records stay, and on restart Spring Batch processes the failed chunk again and repeats the calls. The fix is to make each external call idempotent with a deterministic key per item, and preferably to split the work into two steps: the first writes the data and an outbox row in the same transaction, and the second sends outbox rows to the API and marks them as sent.
What to remember
In this answer, we saw that the duplicates come from a REST call that is outside the chunk's database transaction: a rollback undoes the database rows but not the partner's records, and the restart repeats the calls. We fixed it by making each call idempotent with a deterministic key per item, and by splitting the job into a load step that writes the data and an outbox row in one transaction and a send step that calls the API and marks rows as sent, so a restart sends only what is still pending.
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.
The REST call is not part of the chunk's database transaction, so when the chunk later rolls back, the database rows disappear but the partner's records stay, and on restart Spring Batch processes the failed chunk again and repeats the calls. The fix is to make each external call idempotent with a deterministic key per item, and preferably to split the work into two steps: the first writes the data and an outbox row in the same transaction, and the second sends outbox rows to the API and marks them as sent.
Why this question is asked
Batch jobs often need to tell another system about the records they create: post invoices to a billing provider, create accounts at a partner, push orders to a warehouse. Putting the call in the writer looks natural, because that is where the data is written. But a database and a REST API cannot share a transaction, and Spring Batch's restart model assumes that everything a chunk did can be undone. Interviewers ask this debugging scenario because the duplicates follow directly from how chunks commit, and the fix is a design pattern every senior engineer should know. In this answer, we will recap the chunk transaction, reproduce the duplicate, then fix it with idempotency keys and with an outbox step.
1A Quick Recap of the Chunk Transaction
In a chunk-oriented step, each chunk of items is written inside one database transaction managed by the step's transaction manager. If anything fails before the commit, the transaction rolls back and every database change from that chunk is undone. The reader's saved position is stored in the same transaction, so after a failure, a restart begins at the start of the chunk that did not commit.
That model works perfectly for changes inside the database. A call to an external REST API is different: once the partner has received a POST and created a record, our rollback cannot reach it. The partner's system has its own database and its own transactions.
2Reproducing the Duplicates
Here is the writer that causes the problem. For each item it inserts a row and calls the partner's API to create the same record there.
8 messages in order. Play them, or step through them one at a time.
The failure does not need to be in the writer. A timeout on the commit, the pod being killed after the calls but before the commit, or a skip scan in a fault-tolerant step that writes items one by one, all re-run calls that already succeeded. Notice that even a retry of the REST call itself can duplicate: if the partner created the invoice but our client timed out waiting for the response, retrying creates a second one.
3Fix 1: Idempotent Calls With a Deterministic Key
An idempotent operation gives the same result whether it is done once or several times. We make the REST call idempotent by sending a key that identifies the item, and is the same every time the same item is sent. Many APIs accept an Idempotency-Key header and return the original result for a repeated key. If the partner does not, we can use our own id as the partner's external reference and call an upsert-style endpoint, or check for the reference before creating.
The key must be deterministic, derived from the item's identity, and never a random UUID generated at call time, which would be different on every retry and defeat the purpose. This fix alone stops duplicates, but it relies on the partner honouring the key, and the writer is still slow because each chunk waits for 100 HTTP calls inside an open database transaction.
4Fix 2: Write an Outbox, Then Send in a Separate Step
The more robust redesign separates the two kinds of work. The first step does only database work: it writes the invoices and, in the same transaction, an outbox row for each invoice with status PENDING. The outbox is simply a table of messages waiting to be sent. Because both inserts commit or roll back together, an outbox row exists exactly when its invoice does.
- loadStep: insert invoice and outbox rowone transaction per chunk, no HTTP calls
- sendStep: read PENDING outbox rows
- POST with Idempotency-Key invoice-<id>partner returns the same result for a repeated key
- Call succeeded?YesMark row SENTNoLeave PENDING, retry or skip
- Restart reads only PENDING rows
5 steps in order. Play them, or step through them one at a time.
The second step reads PENDING rows, calls the partner API for each with the idempotency key, and marks the row SENT. If it fails partway, a restart reads only the rows still PENDING, so most items are not called again. The small remaining window, a call that succeeded just before the job died and before SENT was committed, is covered by the idempotency key. That is why the two fixes work best together.
5Why the Status Belongs in the Same Transaction
The general principle behind the redesign is to record what we did in the same transaction as the change that depends on it, whenever possible. In step 1, the outbox row and the invoice commit together, so we never have an invoice without a pending message or the reverse. In step 2, the SENT status is the record of the external effect, and the idempotency key protects the gap between the call and that status, which no transaction can close. This also gives operators a clear view: a count of PENDING rows after the job shows exactly what still needs to be sent.
6Follow-up questions
Could I use a JTA or XA transaction to include the REST call?
No. Distributed transactions work across resources that support a two-phase commit protocol, such as some databases and message brokers. A REST API does not take part in our transaction, so there is no way to roll back a POST. The only general tools are idempotency and compensation, such as calling a cancel endpoint.
What if the partner API has no idempotency support?
We store our id as the partner's external reference and, before creating, check whether a record with that reference already exists. That check-then-create has a small race, but within a single sending step that processes each item once it is reliable. Asking the partner to add idempotency is still worth doing.
Does the outbox make the job slower?
The loading step becomes faster, because it no longer waits on HTTP calls inside its transactions. The sending step can then be tuned separately, for example with a rate limit that matches the partner's quota or a few parallel partitions.
7Conclusion
In this answer, we saw that the duplicates come from a REST call that is outside the chunk's database transaction: a rollback undoes the database rows but not the partner's records, and the restart repeats the calls. We fixed it by making each call idempotent with a deterministic key per item, and by splitting the job into a load step that writes the data and an outbox row in one transaction and a send step that calls the API and marks rows as sent, so a restart sends only what is still pending.
Open with
The cause is that the REST call is not part of the chunk transaction. The writer inserts rows and calls the partner API, then something fails before the commit, maybe a constraint on a later item or the pod being killed.
Then, if they want more
The database rolls back, but the partner has already created those records, and nothing can undo a POST. On restart Spring Batch resumes from the last committed chunk, which is the start of the failed one, so it processes those items again and repeats the calls. A skip scan in a fault-tolerant step can cause the same thing.
I would fix it in two ways. First, make the call idempotent: send a deterministic key per item, like invoice plus the invoice id, as an Idempotency-Key header or an external reference, so a repeated call returns the existing record instead of creating a new one. Second, redesign into two steps.
The load step writes the invoices and an outbox row with status pending in the same transaction, with no HTTP calls. The send step reads pending rows, calls the API with the idempotency key and marks each row sent.
A restart then only sends rows that are still pending, and the idempotency key covers the small gap between a successful call and the status update. Recording the status in the same transaction as the database write is the general principle.
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 writes to a database and also calls an external REST API in the...": https://scrivio-interview-library.netlify.app/spring-boot/spring-batch/b63967e79a/