Quick revision
The short answer
With a 20-second job and a 30-second lockAtMostFor, there is very little margin. The likely causes are clock skew between pods, which lets a pod that fires a few seconds late find the lock already released or apparently expired, especially with no lockAtLeastFor; occasional runs longer than 30 seconds, which let the lock expire mid-run; application clocks used instead of database time; different lock names on old and new versions during a rolling deployment; and a second trigger, such as a manual call or another scheduler.
What to remember
In this answer, we saw that a ShedLock job can run twice only if a second pod finds the lock expired or uses a different lock. With a 20-second job, a 30-second lockAtMostFor and no lockAtLeastFor, clock skew between pods, application time instead of database time and occasional runs longer than 30 seconds all make that possible, while renamed locks during rolling deployments and extra triggers bypass the lock altogether. The fix is a generous lockAtMostFor, a lockAtLeastFor just under the interval, database time, stable lock names, LockAssert, and an idempotent job.
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.
With a 20-second job and a 30-second lockAtMostFor, there is very little margin. The likely causes are clock skew between pods, which lets a pod that fires a few seconds late find the lock already released or apparently expired, especially with no lockAtLeastFor; occasional runs longer than 30 seconds, which let the lock expire mid-run; application clocks used instead of database time; different lock names on old and new versions during a rolling deployment; and a second trigger, such as a manual call or another scheduler.
Why this question is asked
ShedLock usually works quietly, so when a protected job does run twice, teams often conclude the library is broken. In almost every case it is doing exactly what its settings say, and the settings do not match reality. Interviewers ask this question because diagnosing it requires a precise model of how the lock is taken and released, and a calm look at each way two executions can slip through. In this answer, we will recap the lock's timing, go through each possible cause with the evidence that confirms it, and finish with settings and checks that make the job safe.
1A Quick Recap of How the Lock Times Out
When a pod takes a ShedLock lock, it writes lock_until = now + lockAtMostFor into the lock row, here now + 30 seconds. Other pods can take the lock only when lock_until is in the past. When the job finishes, the pod writes lock_until = max(now, locked_at + lockAtLeastFor). With the default lockAtLeastFor of zero, that means the lock is free the moment the job ends.
So a second execution within a minute can only happen in two ways: the second pod found lock_until already in the past, or it never looked at the same lock row at all. Every cause below is one of these two.
2Cause 1: Clock Skew and a Lock Released Too Early
Each pod's scheduler fires by its own clock. If pod B's clock is 25 seconds behind pod A's, B fires its '12:00:00' run when A's clock reads 12:00:25. A started at its 12:00:00, finished after 20 seconds and released the lock immediately because lockAtLeastFor is zero. B finds the lock free and runs the same execution again, five seconds later by real time.
Skew of many seconds sounds unlikely, but it happens on nodes where time synchronisation has failed, after a VM is paused and resumed, or in containers on a host with a drifting clock. The comparison itself can also be affected: if ShedLock compares lock_until against each pod's own clock rather than the database's, a pod whose clock runs fast sees an unexpired lock as expired.
6 messages in order. Play them, or step through them one at a time.
With lockAtLeastFor = 45 s, the lock would still be held at 12:00:25 and pod-b would skip.
The fix has two parts. Setting lockAtLeastFor to, say, 45 seconds keeps the lock taken well past the moment any skewed pod could fire for the same execution, while staying below the one-minute interval. And using the provider's database-time option makes all lock comparisons use one clock, the database's, so pod clocks no longer decide whether a lock has expired. Fixing the time synchronisation on the node is the third, independent fix.
3Cause 2: A Run Occasionally Takes Longer Than 30 Seconds
The job normally takes 20 seconds, but 'normally' hides the tail. A long garbage collection pause, a slow database query on the first of the month or a retry inside the job can push one run to 35 seconds. At 30 seconds, the lock expires while the run is still going, and any pod that fires after that, for example because of slight skew or a different schedule offset, can take the lock and start a second copy.
The evidence is in the job's own logs and metrics: compare the duration of the runs that preceded each double run with 30 seconds. The fix is to set lockAtMostFor well above the longest run ever observed, several minutes rather than 30 seconds for a 20-second job, because its only job is to release the lock if the holder dies, and a few extra minutes of waiting in that rare case costs nothing.
4Cause 3: Different Lock Names During a Rolling Deployment
The lock is identified only by its name. If a recent change renamed the lock, for example from invoiceExport to invoice-export-v2, or the name is built from something that varies, such as a version or a host name, then during a rolling deployment old pods and new pods take different locks. Each group believes it holds the only lock, and both run.
This cause has a clear signature: the double runs cluster around deployments, and the shedlock table contains two rows for what should be one job. The fix is to treat lock names as stable identifiers, written as constants, and never derived from versions, and to rename a lock only with a deployment in which no old pods are running at the time the job fires.
5Cause 4: Something Else Triggers the Job
Finally, the second run may not come from the scheduler at all. An admin endpoint or a test hook that calls the job's method from inside the same class bypasses the proxy that applies the lock. A second scheduler can trigger the same code, such as an old Kubernetes CronJob nobody deleted, a second @Scheduled method that calls the same service, or a second application context in the same JVM with scheduling enabled.
The logs show this immediately if the job logs its trigger and the pod name at start. Calling LockAssert.assertLocked() at the top of the method turns an unlocked call into an error instead of a silent duplicate.
| Cause | Evidence | Fix |
|---|---|---|
| clock skew, lock freed at once | pods' date differs; second run seconds after the first | lockAtLeastFor 45 s, database time, fix NTP |
| run longer than 30 s | run durations above 30 s before doubles | lockAtMostFor of several minutes |
| app time vs database time | provider not set to use database time | enable the database-time option |
| different lock names | doubles during deploys, two lock rows | stable constant names |
| second trigger | start log shows a non-scheduler caller | LockAssert, remove the extra trigger |
6Follow-up questions
What settings would you use for a 20-second job that runs every minute?
lockAtMostFor of about 5 minutes, so a healthy but slow run never loses its lock, and lockAtLeastFor of about 45 seconds, long enough to cover clock skew and fast repeat attempts, but shorter than the one-minute interval so the next legitimate run is never skipped. Plus database time in the lock provider.
Is ShedLock the right tool if duplicates must never happen?
Not on its own. ShedLock reduces duplicates to rare edge cases, but it is a lease-based lock and leases can expire. If a duplicate run would cause real harm, the job itself must be idempotent, for example with a unique constraint per business key and period, so that even a duplicate execution cannot apply the same change twice.
How would you verify the fix?
Log each run's start with the pod name and the lock's locked_at, alert on two runs of the same job inside one interval, and watch that alert through a few deployments. A deliberate test with one pod's clock shifted in a test environment also shows whether lockAtLeastFor is enough.
7Conclusion
In this answer, we saw that a ShedLock job can run twice only if a second pod finds the lock expired or uses a different lock. With a 20-second job, a 30-second lockAtMostFor and no lockAtLeastFor, clock skew between pods, application time instead of database time and occasional runs longer than 30 seconds all make that possible, while renamed locks during rolling deployments and extra triggers bypass the lock altogether. The fix is a generous lockAtMostFor, a lockAtLeastFor just under the interval, database time, stable lock names, LockAssert, and an idempotent job.
Open with
I'd start from how the lock times out. When a pod takes the lock it sets lock_until to now plus 30 seconds, and when it finishes it sets lock_until to the later of now and locked_at plus lockAtLeastFor, which by default is zero.
Then, if they want more
So the lock is free the moment a 20-second run ends. If another pod's clock is behind, it fires its own 12:00 run a few seconds later, finds the lock free and runs again. That's the most likely cause, and setting lockAtLeastFor to around 45 seconds, below the one-minute interval, fixes it.
The second cause is the tail: if a run sometimes takes more than 30 seconds because of a GC pause or a slow query, the lock expires mid-run; lockAtMostFor should be minutes, not 30 seconds. Third, if the provider compares against each pod's clock, a fast clock sees a valid lock as expired, so I'd switch to database time.
Fourth, if the lock name changed between versions, old and new pods take different locks during a rolling deployment; two rows in the shedlock table would show that. And finally, something else may call the job, a manual endpoint or a second scheduler, which logging the trigger and LockAssert would reveal. The job should also be idempotent.
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 ShedLock-protected job occasionally runs twice within a minute on different pods. The...": https://scrivio-interview-library.netlify.app/spring-boot/scheduling/fe36020d92/