BackendBench
Coffee
Free tool

ShedLock generator

Stop a @Scheduled job from running on every instance. Pick the lock store and durations and copy the Spring code and table DDL.

Java
SQL for the lock table

One scheduled job, many instances

Spring's @Scheduled annotation is easy and runs inside your application's memory. That becomes a problem the moment you deploy more than one instance: every instance has its own scheduler, so your nightly report runs three times, the invoices are sent in triplicate, and the cleanup job fights itself. ShedLock is the small library that fixes this without introducing a full scheduler. Before the task body runs, the instance tries to take a named lock in a shared store (a database table, Redis or MongoDB); the one that succeeds runs the job, and the others skip it.

This generator writes the configuration and the annotated task. Choose the store, name the lock, set lockAtMostFor and lockAtLeastFor as ISO-8601 durations (PT10M is ten minutes) and it produces the Java and, for JDBC, the table DDL. The cron expression is checked with Spring's six-field rules.

Choosing the durations

ShedLock guarantees that a lock is held by at most one node at a time, but it does not make a job safe if it can run longer than lockAtMostFor. Keep jobs idempotent where you can. The code is generated locally in your browser; review it and test it in a staging environment with at least two instances.

Frequently asked questions

What does ShedLock do?

It makes sure a scheduled task runs on only one node at a time when the same application is deployed several times. It does not schedule anything; Spring's @Scheduled still fires on every node, and ShedLock lets only one of them proceed.

What is lockAtMostFor?

The longest time the lock is held if the node dies or hangs. Set it longer than the job can legitimately run, otherwise a second node starts the job while the first is still running.

What is lockAtLeastFor?

The shortest time the lock is held, even if the job finishes quickly. It stops another node from running the same job again within that window because its clock is slightly ahead.

Does ShedLock replace Quartz?

No. Quartz persists jobs and supports clustering and misfire policies. ShedLock is a small add-on for plain @Scheduled methods when you do not need a full scheduler.

More tools you might need