Quick revision
The short answer
@Scheduled runs inside each instance of the application, and the six replicas know nothing about each other, so each one fires the job at 08:00 and every customer now gets the reminder six times. We can let only one replica run each execution with a distributed lock such as ShedLock, move the trigger out of the service into a Kubernetes CronJob or clustered Quartz, and in every case make the job idempotent by recording which reminders were sent.
What to remember
In this answer, we saw that @Scheduled lives inside each replica, so scaling from one to six replicas makes the reminder job run six times. We compared three ways to run it once: a ShedLock distributed lock with the least change, a Kubernetes CronJob with one external trigger, and clustered Quartz with persistent schedules and misfire handling. Whichever we choose, we make the job idempotent with a sent-reminder record keyed by user and day, because only the database can finally guarantee one email per user.
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.
@Scheduled runs inside each instance of the application, and the six replicas know nothing about each other, so each one fires the job at 08:00 and every customer now gets the reminder six times. We can let only one replica run each execution with a distributed lock such as ShedLock, move the trigger out of the service into a Kubernetes CronJob or clustered Quartz, and in every case make the job idempotent by recording which reminders were sent.
Why this question is asked
Scaling out is supposed to be a configuration change: set replicas to 6 and the service handles six times the traffic. Scheduled jobs are where that assumption breaks, and the first sign is often a flood of customer complaints about duplicate emails. Interviewers ask this scenario because it checks whether we understand where a scheduler lives and what 'exactly once' really takes in a distributed system. In this answer, we will see why the job now runs six times, then go through the options, a distributed lock, a Kubernetes CronJob and clustered Quartz, compare their costs, and finish with idempotency, which we need whichever option we choose.
1Why Every Replica Runs the Job
@Scheduled is handled by a scheduler that lives inside the Spring application context. Each replica of the service is a separate JVM with its own context and its own scheduler, built from the same code and the same cron expression. Nothing in @Scheduled talks to the other replicas, and there is no shared record of 'this run already happened'.
So at 08:00 every replica's scheduler wakes up independently and calls sendReminders(). With one replica that was exactly right. With six, the same work is done six times, each replica reads the same list of users due a reminder, and each sends the same emails.
6 messages in order. Play them, or step through them one at a time.
Replicas 3 to 5 do the same. User 42 gets six identical emails.
2Option 1: A Distributed Lock With ShedLock
The smallest change is to keep @Scheduled but let only one replica do the work. ShedLock is a small library that does this with a lock stored in a shared place, such as a table in the service's database. When the schedule fires, each replica tries to take the lock named reminders; the first one succeeds and runs the job, the others see the lock is taken and skip that execution.
This is quick to adopt and needs no new infrastructure. Its limits are that it is only mutual exclusion, so one replica does all the work, and that it keeps no history and does not make up a run that every replica missed, for example during a deployment at 08:00. For a daily reminder job, those limits are usually acceptable.
3Option 2: A Kubernetes CronJob
Another approach is to take the schedule out of the service entirely. A Kubernetes CronJob is a resource that starts a new pod on a cron schedule, runs it to completion and stops it. The pod can run the same code with a profile that sends the reminders once and exits, or it can simply call an internal endpoint of the service.
Now there is exactly one trigger, owned by the cluster, and the service replicas no longer schedule anything. We get logs and status for each run, a concurrencyPolicy: Forbid setting to stop runs overlapping, and the heavy work no longer competes with requests inside the serving pods. The cost is that it only works on Kubernetes, starting a pod adds some seconds, and Kubernetes itself documents that a CronJob can occasionally create two jobs or none for one schedule time, so the job must still tolerate a repeat.
4Option 3: Quartz in Clustered Mode
Quartz is a full scheduling library that Spring Boot supports with spring-boot-starter-quartz. With a JDBC job store, the triggers live in database tables shared by all replicas, and in clustered mode the replicas coordinate through row locks so that each firing is taken by exactly one of them.
Quartz gives us more than ShedLock: persistent schedules, misfire handling to decide what to do about runs missed during downtime, and load sharing across replicas for many different jobs. The price is a set of QRTZ_ tables, more configuration and more to understand. It is worth it when the service has many jobs or needs misfire rules; for one daily email it is usually more than we need.
| Option | What it needs | Good at | Watch out for |
|---|---|---|---|
| ShedLock | a lock table, two annotations | smallest change | no history, no catch-up of missed runs |
Kubernetes CronJob | a CronJob manifest | isolated run, run status | Kubernetes only, rare double or missed run |
| Clustered Quartz | QRTZ tables, configuration | persistence, misfire rules | more moving parts |
| Idempotent job | a sent-reminders table | safe repeats | needed with every option |
5Make the Job Idempotent Anyway
None of these options gives a true guarantee that the work happens exactly once. A lock can expire while a slow run is still going, a CronJob can occasionally start twice, and a replica can crash after sending half the emails and be retried. The reliable answer is to make the job idempotent: running it twice has the same effect as running it once.
For reminders, that means recording each reminder as it is sent, with a unique key per user and day, and only sending when the insert succeeds.
Notice the order: claim first, then send. If the process dies between the insert and the send, that user misses one reminder, which is usually better than sending duplicates; if the business prefers the opposite, we can mark the row as sent only after the mail service accepts the message and retry unconfirmed rows. Either way, the database's unique key, not the scheduler, is what finally guarantees one reminder per user per day.
6Follow-up questions
Could we just run the scheduler on one replica using a property?
We could enable scheduling only on a pod with a special flag, but Kubernetes treats replicas of a Deployment as interchangeable, so that pod can be rescheduled, scaled away or duplicated during a rolling update. It turns a duplicate problem into a missed-run problem. A lock or an external trigger is more reliable.
Which option would you choose here?
For a daily reminder in a service that already has a database, ShedLock plus the sent_reminder table is the smallest safe change. If the job grew heavy or the team already ran many CronJobs, I would move it to a CronJob. I would choose Quartz if the service had many jobs that needed persistent schedules and misfire rules.
Does ShedLock alone prevent duplicates completely?
Not completely. It prevents the normal case of six replicas running at once, but if a run takes longer than lockAtMostFor, the lock expires and another replica can start, and clock differences between nodes can matter for very short jobs. That is why the idempotency record is the real safety net.
7Conclusion
In this answer, we saw that @Scheduled lives inside each replica, so scaling from one to six replicas makes the reminder job run six times. We compared three ways to run it once: a ShedLock distributed lock with the least change, a Kubernetes CronJob with one external trigger, and clustered Quartz with persistent schedules and misfire handling. Whichever we choose, we make the job idempotent with a sent-reminder record keyed by user and day, because only the database can finally guarantee one email per user.
Open with
Each replica has its own Spring context and its own scheduler, and they don't coordinate, so at the scheduled time all six fire the job and every user gets six reminders.
Then, if they want more
There are a few ways to fix the trigger. The smallest is a distributed lock with ShedLock: the method keeps @Scheduled and gets @SchedulerLock with a name, the lock lives in a table in our database, and only the replica that gets the lock runs that execution while the others skip.
Another option is to remove the schedule from the service and use a Kubernetes CronJob that starts one pod, or calls an endpoint, once a day, with concurrencyPolicy Forbid. If the service had many jobs needing persistent schedules and misfire handling, I would use Quartz in clustered mode with a JDBC job store.
But I would make the job idempotent regardless, because locks can expire and CronJobs can occasionally run twice. I would insert a row per user and date into a sent_reminder table with a primary key on both, using on conflict do nothing, and only send the email if the insert succeeded. Then even a duplicate trigger cannot send a duplicate email.
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 team uses @Scheduled to send daily reminder emails from a Spring Boot service, and it...": https://scrivio-interview-library.netlify.app/spring-boot/scheduling/3d177f2d68/