Quick revision
The short answer
I would separate the two needs: the live job repository is operational metadata that only needs a recent window, while audit needs a long-lived record of what ran. Completed executions are archived to a cheaper store with the fields audit needs, the live tables keep only a recent window, for example 90 days, and older rows are purged by a supported, tested cleanup process. The retention policy is agreed with compliance in writing and recorded in an ADR, the repository tables are indexed and maintained, possibly with one repository schema per domain, and the archive is funded as part of the batch platform rather than by each team.
What to remember
In this answer, we resolved the conflict between seven-year retention and slow launches by separating operational metadata from audit records. A daily job archives completed executions to a cheaper audit store with the fields audit needs, verifies them, and purges old rows from the live repository in foreign key order, leaving a recent window. The policy is agreed with compliance in writing and recorded in an ADR, the live tables are indexed and maintained, possibly with one schema per domain, and the archive is funded as part of the batch platform.
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.
I would separate the two needs: the live job repository is operational metadata that only needs a recent window, while audit needs a long-lived record of what ran. Completed executions are archived to a cheaper store with the fields audit needs, the live tables keep only a recent window, for example 90 days, and older rows are purged by a supported, tested cleanup process. The retention policy is agreed with compliance in writing and recorded in an ADR, the repository tables are indexed and maintained, possibly with one repository schema per domain, and the archive is funded as part of the batch platform rather than by each team.
Background to this question
This is a typical conflict between two reasonable groups. Compliance has a real obligation to show seven years of batch history, and operations has a real problem: launches slow down because Spring Batch queries tables with hundreds of millions of rows every time a job starts or restarts. If each side argues for its own need, the result is either a slow platform or a compliance breach. A senior technical leader's job is to show that the two needs are about different data and can both be met. In this answer, we will see why large repository tables slow launches, separate operational from audit data, design the archive and purge, agree the policy formally, look at indexing and schema layout, and decide who pays.
1Why Big Repository Tables Slow Down Launches
Every launch asks the job repository questions: does an instance with these parameters exist, what is its last execution, is any execution still running, and on restart, what was the last execution of each step. These queries run against BATCH_JOB_INSTANCE, BATCH_JOB_EXECUTION, BATCH_JOB_EXECUTION_PARAMS and BATCH_STEP_EXECUTION. With jobs running every few minutes for years, these tables reach hundreds of millions of rows. If the foreign key columns are not indexed, which the standard schema scripts do not always do for every database, each lookup scans far more than it should, and launches that took 50 milliseconds now take seconds or time out.
Nothing in Spring Batch needs that old data to work. Restart only needs the recent executions of instances that have not completed, and duplicate protection only needs the instances that might be launched again. Seven-year-old rows are dead weight for operations.
2Separating Operational Metadata From Audit Records
The key move is to recognise that the job repository and the audit record are different things that happen to start as the same rows. The repository is a working database for the framework. The audit record is a business record: what job ran, for which business date, when, with what outcome, how many records it processed, and who launched or re-ran it. Audit rarely needs the serialised ExecutionContexts, which make up much of the storage.
- Live job repositorylast 90 days, all tables, used by Spring Batch
- Archive jobcopies completed executions daily, then purges old rows
- Audit store7 years, append-only, fields audit needs
- Audit queries and reportsby job, business date, outcome, operator
4 layers, from the top down. Step through them one at a time.
So we define an audit record format with compliance: job name, identifying parameters including the business date, job and step execution ids, start and end times, status and exit codes, counts, the launching user or system, and any input checksums the job recorded. That record goes to a store designed for long retention and cheap storage, such as an append-only audit database partitioned by month or files in object storage with a retention lock.
3Archiving and Purging Safely
A scheduled archive job, itself a Spring Batch job on the platform, runs every day. It selects job executions that ended more than 90 days ago and whose instance is in a final state, writes their audit records to the audit store, verifies the write, for example by comparing counts, and only then deletes the rows from the live repository. Deletion must follow the foreign keys, from the context tables and step executions to the execution parameters, the job executions, and finally instances with no executions left.
- Select executions ended over 90 days agofinal status only
- Write audit recordsappend-only audit store
- Do counts match?YesDelete rows in foreign key orderNoStop and alert, delete nothing
- Record the runhow many archived and purged
4 steps in order. Play them, or step through them one at a time.
There is one important subtlety. Deleting a completed instance removes Spring Batch's protection against running it again. For jobs keyed by business date, that is usually fine after 90 days, because no scheduler will launch a three-month-old date. But if re-running an old date by mistake would be harmful, the launch path should check the audit store too. Newer Spring Batch versions provide delete operations on the JobRepository that can replace hand-written SQL; check your version, and either way test the purge on a copy of production first.
4Agreeing the Policy in Writing
The technical design only resolves the conflict if compliance agrees that the archive meets the obligation. So we write the retention policy together: what is kept for seven years, in what form, how it can be retrieved and how quickly, who can access it, and that live operational data is kept for 90 days. Compliance signs it, and we record the decision in an architecture decision record, an ADR, which captures the context, the options considered and the reasons. The ADR is what a future auditor or engineer will read when they ask why the live tables only hold 90 days.
5Indexing, Maintenance and Schema Layout
Even with a 90-day window, the live tables need care. We add indexes on the columns Spring Batch looks up, such as the job instance id in the execution table and the job execution id in the step execution and parameter tables; the Spring Batch reference documentation includes recommendations for indexing the metadata tables. We run regular maintenance, such as vacuum and statistics updates in PostgreSQL, after large purges.
We can also reduce the blast radius by giving each business domain its own repository schema rather than one huge shared one. A payments domain with frequent jobs then cannot slow launches for the reporting domain, and each schema's retention can follow its own rules if compliance requires different periods.
6Funding It as Part of the Platform
Finally, the archive is built and run by the batch platform team, funded as part of the platform, not left to each team. If every team had to build its own archive, we would get twenty slightly different archives, some missing fields, some never tested, and the compliance risk would come back. A single archive with one format also makes audits cheaper: one query answers a question about any job. The cost, storage plus a small amount of engineering, is easy to justify against both the operational incidents from slow launches and the regulatory risk.
7Follow-up questions
Why not just keep everything and add indexes?
Indexes help a lot and should be added anyway, but tables that grow forever keep getting slower to maintain, back up and restore, and the serialised contexts make them large. Keeping seven years of framework data in a hot operational database is expensive and unnecessary when audit needs only a fraction of the columns.
What window should the live repository keep?
Long enough to cover any realistic restart or investigation, plus a margin. If the longest gap between a failure and its fix is a few weeks, 90 days is comfortable. Jobs that run monthly may need a longer window, such as 13 months, so last year's month-end is still visible.
How do we prove to auditors that the archive is complete?
The archive job records how many executions it copied and deleted each day, and a periodic reconciliation compares counts of executions per job and month between the audit store and what the platform has run. Those reports, kept alongside the archive, are the evidence.
8Conclusion
In this answer, we resolved the conflict between seven-year retention and slow launches by separating operational metadata from audit records. A daily job archives completed executions to a cheaper audit store with the fields audit needs, verifies them, and purges old rows from the live repository in foreign key order, leaving a recent window. The policy is agreed with compliance in writing and recorded in an ADR, the live tables are indexed and maintained, possibly with one schema per domain, and the archive is funded as part of the batch platform.
Open with
I would start by showing that the two sides want different data. Spring Batch only needs recent executions to launch and restart jobs; audit needs a long-lived record of what ran, for which business date, with what outcome and by whom.
Then, if they want more
So I would separate them.
A daily archive job on the platform copies completed executions older than, say, 90 days into an append-only audit store with the fields compliance needs, verifies the counts, and then purges those rows from the live repository in foreign key order, in small batches, using the repository's delete support if our version has it or a tested SQL script.
The live tables then stay small, and launches are fast again. I would agree the retention policy with compliance in writing, what is kept, in what form and how it is retrieved, and record the decision in an ADR.
I would also index the repository tables on the columns Spring Batch looks up, run maintenance after purges, and consider one repository schema per domain so one busy domain does not slow everyone. One subtlety: purging completed instances removes duplicate-run protection for old dates, so for sensitive jobs the launch path can check the audit store.
Finally, the archive is funded and run by the platform team, not by each team, so there is one format and one place auditors can query.
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 "Audit asks you to keep batch execution history for seven years, but the job repository...": https://scrivio-interview-library.netlify.app/spring-boot/spring-batch/56cdadc874/