Scrivio Interview library
DesignAdvanced6 min read · updated 12 November 2026

Several teams write batch jobs that update the same core customer database during the night and cause lock contention and replication lag. How would you set standards for chunk processing that protect shared databases?

I would publish a short standard for batch jobs that write to shared databases: chunked writes with a commit interval in an agreed range, batch statements instead of per-row updates, heavy jobs only in agreed windows with a concurrency limit per database, throttled writers, and read-heavy work on replicas. The standard is backed by monitoring of replication lag and lock waits during batch windows with alerts to the job owner, and a lightweight design review for any new job that touches a shared database.

Background to this question

When several teams run batch jobs against the same core database at night, each job may be reasonable on its own while the combination is not. Long transactions hold row locks that block other jobs and early-morning traffic, and a flood of writes makes replicas fall behind, so reports and read services serve old data. Nobody owns the combined load, which is exactly why a senior engineer is asked to set standards. In this answer, we will recap where lock contention and replication lag come from, then go through the standards for chunks and writes, scheduling and concurrency, throttling and replicas, monitoring, and design review.

1Where Lock Contention and Replication Lag Come From

When a transaction updates a row, the database locks that row until the transaction commits. Another transaction that wants to update the same row has to wait. In a chunk-oriented batch step, a transaction lasts for one chunk, so the bigger and slower the chunk, the longer its locks are held. Two jobs updating overlapping customer rows, for example one recalculating segments and one applying address corrections, wait for each other, and in bad cases deadlock.

Replication lag is the delay between a write on the primary database and its appearance on a replica. The replica applies changes from the primary's log, and if the primary produces changes faster than the replica can apply them, lag grows. A batch job that updates 20 million rows as fast as possible can create minutes or hours of lag, and every read service and report on the replica shows stale data in the meantime. Very large single transactions make it worse, because a replica may not show any of the change until the whole transaction has been applied.

2Standards for Chunks and Writes

The first standard is about transaction size. We give a recommended commit interval range, for example 200 to 2,000 rows, and more usefully a time target: each chunk transaction should finish in under about one or two seconds. That keeps locks short and replication smooth while still amortising commit overhead. A job that updates a million rows in one transaction is not allowed on a shared database.

The second standard is about how rows are written. Per-row UPDATE statements sent one by one are slow and keep each transaction open longer than needed. Jobs use JDBC batching, such as JdbcBatchItemWriter, or set-based statements where the logic allows it. Updates should touch rows in a consistent order, for example by primary key, which reduces deadlocks between jobs.

SQL
-- not allowed on shared databases: one transaction, 20 million rows
UPDATE customer SET segment = compute_segment(id);
-- allowed: chunked by key range, about 1,000 rows per transaction
UPDATE customer SET segment = compute_segment(id)
WHERE id > :lastId AND id <= :lastId + 1000; -- commit after each range

3Windows and Concurrency Limits

Most contention comes from heavy jobs overlapping. So we agree batch windows per shared database, for example 00:30 to 04:30 for heavy writers, and a concurrency limit, such as at most two heavy write jobs on the customer database at the same time. The orchestrator enforces the limit, for example with a resource pool or a named lock that jobs must acquire before their heavy step, so it does not depend on teams checking a calendar.

Customer database batch window (example)
TimeHeavy write slot 1Heavy write slot 2Read-only on replica
00:30-01:30segment recalculationaddress correctionsmarketing extract
01:30-03:00loyalty pointsfreerisk reporting
03:00-04:30archive and purgefreedata warehouse load
after 04:30no heavy writesno heavy writesallowed
Two heavy writers at most, enforced by the orchestrator.

Notice that read-heavy work, such as extracts for the data warehouse, is moved to a replica. It then does not compete for the primary's I/O or hold locks there. The cost is that it reads slightly old data, which is acceptable for extracts but must be stated in the job's description.

4Throttling Writers

Even within a window, a job that writes as fast as possible can push replication lag beyond what the readers tolerate. Throttling means deliberately limiting the write rate, for example to 5,000 rows per second for a given job. A simple way is a rate limiter in the writer. A better one is adaptive: before each chunk, the writer checks replication lag and pauses when it exceeds a threshold.

Java
public void write(Chunk<? extends Customer> chunk) throws Exception {
while (lagMonitor.replicaLagSeconds() > 10) { // agreed threshold
Thread.sleep(500); // let replicas catch up
}
rateLimiter.acquire(chunk.size()); // e.g. 5,000 rows per second
delegate.write(chunk);
}

This makes the job slower but predictable, and it protects everyone else using the database. The threshold and rate belong in the job's configuration, so the database owner can tighten them on a busy night without a code change.

5Monitoring and Alerts During the Window

Standards need evidence. During batch windows we monitor replication lag on each replica, lock waits and blocked sessions on the primary, deadlocks, and long-running transactions. In PostgreSQL, for example, replication lag is visible in pg_stat_replication and lock waits in pg_stat_activity and pg_locks; MySQL has similar views. Each alert includes which job's sessions are involved, which is possible if every job sets an application name on its connections, and it goes to that job's owning team, not only to the database team.

Over time these measurements also show which jobs are the worst neighbours. A monthly report of lock wait time and lag caused per job gives the database owner a factual basis for asking a team to change its job.

6A Lightweight Design Review

Finally, any new job, or a significant change to an existing one, that writes to a shared database goes through a short design review. It is a one-page form and a 30-minute conversation with the database owners: what tables it touches, how many rows, chunk size, expected duration, window, and whether it can read from a replica. The aim is not to block teams but to catch the one job that would update 50 million rows in a single transaction before it runs in production.

Step throughAdmitting a new batch job to a shared database
  1. Writes to a shared database?
    YesDesign reviewNoNormal team process
  2. Within chunk and write standards?
    YesAssign a window slotNoRevise the design
  3. Test run shows lag and locks within limits?
    YesSchedule in productionNoTune or throttle

3 steps in order. Play them, or step through them one at a time.

7Follow-up questions

Why not just give each team its own database?

Where data truly belongs to one team, that is the long-term fix, and the review should point it out. But a core customer database is shared because many processes need the same customer data. Splitting it is a large programme, so standards are needed in the meantime and remain useful afterwards.

How do you handle a job that cannot fit its window?

We first apply the standards, which often speed it up, since batched writes and keyset reads are faster as well as gentler. If it still does not fit, we look at running it incrementally during the day at a throttled rate, splitting the work across nights, or moving its reads to a replica.

What if two jobs must update the same rows?

We order them in the schedule so they never overlap, or merge their logic into one job. Two jobs updating the same rows concurrently will contend no matter how small their chunks are.

8Conclusion

In this answer, we saw that lock contention comes from long transactions on overlapping rows and replication lag from writing faster than replicas can apply. The standards keep chunk transactions short with batched writes, put heavy jobs in agreed windows with a concurrency limit enforced by the orchestrator, throttle writers and move read-heavy work to replicas, monitor lag and lock waits with alerts to the job owner, and review new jobs that touch shared databases in a lightweight design review.

What to say in the interviewAbout 80 seconds aloud

Open with

I would write a short standard for any batch job that writes to a shared database, and back it with tooling. On chunk processing: a commit interval in an agreed range, roughly a few hundred to two thousand rows or about a second per transaction, and batched JDBC writes or set-based statements instead of per-row updates, touching rows in key order to reduce deadlocks.

Then, if they want more

Huge single transactions are not allowed. On scheduling: agreed batch windows per database and a limit on concurrent heavy writers, say two on the customer database, enforced by the orchestrator through a resource pool rather than a shared calendar.

On load: writers are throttled with a rate limit and, better, pause when replica lag passes a threshold, and read-heavy jobs like extracts run against replicas.

On monitoring: during the window we watch replication lag, lock waits, deadlocks and long transactions, and because every job sets an application name on its connections, alerts go to the owning team of the job causing it.

Finally, a lightweight design review for any new job touching a shared database, a one-page form and a short conversation covering tables, row counts, chunk size and window, with a test run that shows lag and locks within limits before it goes to production.

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 "Several teams write batch jobs that update the same core customer database during the...": https://scrivio-interview-library.netlify.app/spring-boot/spring-batch/5e7092d827/