Quick revision
The short answer
ShedLock provides mutual exclusion, so by design only one pod processes invoices at a time, and adding pods adds nothing. To scale, we let every pod claim its own batch of pending rows with SELECT ... FOR UPDATE SKIP LOCKED and process the batches in parallel, or we publish pending invoices to a queue and let consumers scale with the backlog. In both cases processing must be idempotent so a crash mid-batch is safe to retry, and we track backlog size and age as metrics.
What to remember
In this answer, we saw that ShedLock caps the invoice job at one pod because it is a mutual exclusion tool, not a throughput tool. We redesigned it so every pod claims its own batch with SELECT ... FOR UPDATE SKIP LOCKED, either processing inside the transaction or claiming with a short lease for slow work, made billing idempotent so crashes are safe to retry, compared a queue as the most scalable alternative, and added backlog size and age metrics so we know whether we keep up.
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.
ShedLock provides mutual exclusion, so by design only one pod processes invoices at a time, and adding pods adds nothing. To scale, we let every pod claim its own batch of pending rows with SELECT ... FOR UPDATE SKIP LOCKED and process the batches in parallel, or we publish pending invoices to a queue and let consumers scale with the backlog. In both cases processing must be idempotent so a crash mid-batch is safe to retry, and we track backlog size and age as metrics.
Why this question is asked
A growing backlog is a capacity problem disguised as a scheduling problem. The team added ShedLock to stop pods processing the same invoices twice, which was correct, but it also turned a cluster of pods into a single worker with spare capacity sitting idle. Interviewers ask this scenario to see whether we can tell coordination tools apart from throughput tools, and whether we know the standard database pattern for many workers sharing one table. In this answer, we will see why the lock caps throughput, redesign the job with SKIP LOCKED claiming, deal with crashes through leases and idempotency, look at the queue alternative, and decide what to measure.
1Why the Backlog Keeps Growing
ShedLock answers one question: is anyone else already running this job? Exactly one pod gets the lock each minute, and every other pod skips. That is the right tool when the work must happen once, such as a daily report, but here the work is a stream of independent invoices. Throughput is therefore capped at what one pod can do in a minute, no matter how many pods we run.
A little arithmetic shows the trap. Suppose 600 new invoices arrive per minute and one pod can process 400 per minute. The backlog grows by 200 every minute, 12,000 an hour, and doubling the pods changes nothing. What we need instead is for each pod to take a different share of the pending invoices, so the work is divided rather than guarded.
2Every Pod Claims Its Own Batch With SKIP LOCKED
PostgreSQL and MySQL 8 both support SELECT ... FOR UPDATE SKIP LOCKED. FOR UPDATE locks the rows a query returns; SKIP LOCKED tells the query to silently pass over rows that another transaction has already locked instead of waiting for them. So when three pods run the same query at the same moment, each gets a different set of rows, with no waiting and no overlap. Here every pod runs the job every few seconds, without ShedLock.
8 messages in order. Play them, or step through them one at a time.
No pod waits for another, and no row is processed twice while it is locked.
Each batch is one transaction: the rows stay locked while they are processed and the status update commits with the release of the lock. If the pod crashes, the transaction rolls back, the locks disappear and the rows are PENDING again for another pod to pick up. Throughput now grows with the number of pods, until the database or the billing system becomes the limit. The index that makes the claim cheap is one on created_at limited to pending rows, such as CREATE INDEX ON invoice (created_at) WHERE status = 'PENDING'.
3Short Transactions With a Claim and a Lease
Holding a transaction open while we call an external billing system has a cost: if a charge takes 2 seconds, a batch of 100 holds its locks and a database connection for over 3 minutes. For slow processing, a better variant claims rows in one short transaction by marking them, then processes them outside any long transaction.
The claim is now a lease: the pod owns the rows for 10 minutes. If it crashes, the rows stay in PROCESSING until the lease runs out, and then any pod can reclaim them. The lease must be longer than a normal batch takes, or a slow but healthy pod would have its rows taken away.
4Idempotency: Crashes Mean Repeats
In both variants, a crash after charging an invoice but before marking it DONE means the invoice will be processed again. So the processing itself must be idempotent: doing it twice must have the same effect as doing it once. For billing, the usual tools are an idempotency key sent to the payment provider, such as the invoice id, which the provider uses to ignore a repeated charge, and a unique constraint in our own database, such as one payment row per invoice.
This is not a new requirement introduced by parallel processing. The single-pod ShedLock design had the same crash window; it was just easier to overlook.
5The Alternative: A Queue
Instead of polling the table, we can publish each pending invoice as a message to a queue or a Kafka topic, ideally written through an outbox table in the same transaction that creates the invoice. Consumers in every pod take messages and process them. The broker handles distribution, retries and dead-letter queues, and autoscaling can add consumers based on queue depth. With Kafka, parallelism is limited by the number of partitions of the topic, so it must be planned.
The cost is another piece of infrastructure and the outbox pattern to keep the database and the queue consistent. If the system already runs a broker, a queue is the most scalable answer. If not, SKIP LOCKED on the existing table gets us most of the way with no new components, and it comfortably handles thousands of rows per second on a well-indexed table.
| Design | Parallelism | Crash handling | New infrastructure |
|---|---|---|---|
| ShedLock, one pod | 1 pod | rows stay pending | none |
| SKIP LOCKED in a transaction | all pods | rollback frees the rows | none |
| SKIP LOCKED claim with lease | all pods | stale claims reclaimed | none |
| Queue or Kafka topic | consumers or partitions | redelivery, dead letters | broker and outbox |
6Measuring the Backlog
The backlog grew unnoticed because nobody was measuring it. We should publish two gauges: the number of pending invoices, and the age of the oldest pending invoice, which is the delay a customer actually experiences. Both are a cheap query, such as SELECT count(*), max(now() - created_at) FROM invoice WHERE status = 'PENDING', run every minute.
Age is the better alert, because it says directly whether we meet the business expectation, for example 'every invoice is processed within 15 minutes'. Count tells us about capacity: if it rises steadily, arrivals exceed processing. With these two numbers we can also scale automatically and see at once whether a change, like adding pods, worked.
7Follow-up questions
Why not keep ShedLock and just make the single pod faster?
That helps for a while, but it keeps a hard ceiling of one pod and turns that pod into a single point of failure for invoicing. Dividing the work removes the ceiling and lets any pod pick up the slack when another dies.
Does SKIP LOCKED change the processing order?
Slightly. Each pod takes the oldest unlocked rows, so overall the work proceeds roughly oldest first, but two invoices can finish in a different order than they were created. If invoices of one customer must be processed strictly in order, we claim by customer instead, or use a queue partitioned by customer id.
What isolation level does SKIP LOCKED need?
The default READ COMMITTED is the usual choice in PostgreSQL. SKIP LOCKED is meant for queue-like tables, and it deliberately gives an inconsistent view of the table, because rows locked by others are invisible to the query, which is exactly what we want here.
8Conclusion
In this answer, we saw that ShedLock caps the invoice job at one pod because it is a mutual exclusion tool, not a throughput tool. We redesigned it so every pod claims its own batch with SELECT ... FOR UPDATE SKIP LOCKED, either processing inside the transaction or claiming with a short lease for slow work, made billing idempotent so crashes are safe to retry, compared a queue as the most scalable alternative, and added backlog size and age metrics so we know whether we keep up.
Open with
ShedLock is the wrong tool here. It guarantees that only one pod runs the job, so throughput is capped at one pod no matter how many we add; if 600 invoices arrive a minute and one pod does 400, the backlog grows by 200 a minute.
Then, if they want more
I'd remove the lock and let every pod claim its own share. Each pod runs select from invoice where status is pending, order by created_at, limit 100, for update skip locked. Skip locked means rows another pod has locked are simply skipped, so the pods get different batches with no waiting.
For quick processing, I process and mark them done in that same transaction, and a crash rolls back and frees the rows. If processing is slow, like calling a billing provider, I instead claim the rows in a short transaction by setting status processing with a claimed_at time, and reclaim claims older than a lease.
Either way a crash means a repeat, so the charge must be idempotent, with an idempotency key per invoice and a unique constraint. The alternative is publishing invoices to a queue through an outbox and scaling consumers on queue depth. And I'd add metrics for pending count and the age of the oldest pending invoice, alerting on age.
About this answer
- Last updated
- 12 November 2026
- Versions it names
- MySQL 8
- 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 team uses ShedLock to run a job that processes all pending invoices every minute, but...": https://scrivio-interview-library.netlify.app/spring-boot/scheduling/addc75e3e2/