@Scheduled — Cron Jobs and Fixed-Rate Tasks in Spring Boot
Fast recall
Quick revision
Core answer
@Scheduled enables method-level task scheduling. `fixedRate` runs at a constant interval regardless of execution time; `fixedDelay` waits for the previous execution to finish before starting the timer; `cron` uses a Spring-specific 6-field expression (seconds first, unlike Unix cron). The critical prerequisite is `@EnableScheduling` — without it, @Scheduled annotations are silently ignored. By default, all scheduled tasks share a single thread, meaning one slow task blocks all others. Configure a ThreadPoolTaskScheduler with an ErrorHandler for production workloads.Whether the candidate understands the difference between `fixedRate` and `fixedDelay` and knows the single-thread default
Not realizing all `@Scheduled` methods run on a single thread by default, so one slow task delays all others
Mention configuring a custom `TaskScheduler` with a thread pool, the 6-field Spring cron format vs Unix 5-field, and ShedLock for clustered single-execution coordination
Interview-ready guide
Interview answer
@Scheduled is Spring's built-in mechanism for recurring tasks — cron jobs, periodic cache refreshes, health pings. The defaults hide a critical limitation: every scheduled task runs on the same single thread.
The single-thread problem
- Spring creates one scheduler thread by default — if a task takes 5 seconds and is scheduled every 3 seconds, subsequent firings queue up behind the running one
- Configure a custom
ThreadPoolTaskSchedulerwith a pool size matching the number of concurrent tasks you expect - Always set an
ErrorHandler— otherwise failures are silently logged to stderr
`fixedRate` vs `fixedDelay`
fixedRate = 5000— fires every 5 seconds from the previous start time; if the task takes 6 seconds, the next fires immediately after completion (no overlap by default)fixedDelay = 5000— fires 5 seconds after the previous execution completes; total cycle time = task duration + delayfixedRateis for metrics and health checks where timing regularity matters;fixedDelayis for sequential processing where overlap is unsafe
Cron expressions
- Spring uses a 6-field cron: seconds, minutes, hours, day-of-month, month, day-of-week
@Scheduled(cron = "0 0 8 * * MON-FRI")= 8:00 AM every weekday; note the leading0for seconds — Unix cron has no seconds field, a common copy-paste mistake
Clustered deployments
- Every instance runs every task — no built-in coordination
- ShedLock or Quartz clustered scheduler for at-most-once cluster-wide execution
Add @EnableScheduling to a @Configuration class — without it, every @Scheduled annotation is silently ignored. In an interview, the strong answer is: `@EnableScheduling` + thread pool + error handler + `/actuator/scheduledtasks` for verification, with ShedLock for clustered coordination.
@Scheduled — Cron Jobs and Fixed-Rate Tasks in Spring Boot
Direct Answer
@Scheduled enables method-level task scheduling. fixedRate runs at a constant interval regardless of execution time; fixedDelay waits for the previous execution to finish before starting the timer; cron uses a Spring-specific 6-field expression (seconds first, unlike Unix cron). The critical prerequisite is @EnableScheduling — without it, @Scheduled annotations are silently ignored. By default, all scheduled tasks share a single thread, meaning one slow task blocks all others. Configure a ThreadPoolTaskScheduler with an ErrorHandler for production workloads.
Common Mistakes & Pitfalls
Not realizing all @Scheduled methods run on a single thread by default, so one slow task delays all others
Check your understanding
Answer these without looking back at the article.
- How would you ensure a `@Scheduled` task runs exactly once across a cluster of pods?
- What is the difference between `fixedRate` and `fixedDelay` when the task takes longer than the interval?
- How do you handle exceptions thrown by a `@Scheduled` method?
- Why does `@Scheduled` require `@EnableScheduling` separately from `@SpringBootApplication`?
Related interview prep categories
1Looking for a structured roadmap?
Every module on this page is also part of the full Java backend interview track.