Sub topic 3
Fixed rate, fixed delay and initial delay
This chapter assumes you can write a Spring Boot application, define beans with @Component, and have already enabled scheduling with @EnableScheduling and declared a method with @Scheduled. Here you learn how the three timing attributes differ and how to choose between them.
The three attributes
A @Scheduled method can run on a fixed interval. Spring offers two ways to measure that interval, plus an optional delay before the first run.
fixedRateis the time between the start of one run and the start of the next.fixedDelayis the time between the end of one run and the start of the next.initialDelayis how long to wait before the first run. It works with both attributes.
All values are in milliseconds unless you use a String variant or set the timeUnit attribute. The annotated method must return void and take no arguments.
@Component
public class ReportJobs {
private static final Logger log = LoggerFactory.getLogger(ReportJobs.class);
@Scheduled(fixedRate = 5000)
public void pollEveryFiveSeconds() {
log.info("fixed rate: starts every 5s, measured from the previous start");
}
@Scheduled(fixedDelay = 5000, initialDelay = 10000)
public void pauseBetweenRuns() {
log.info("fixed delay: waits 5s after the previous run finishes");
}
}Without initialDelay, the first run starts as soon as the application context has finished setting up its scheduled tasks. In this example, pauseBetweenRuns waits 10 seconds first, then runs, then waits 5 seconds after each finish.
How the two intervals differ
Suppose a task takes 2 seconds and the interval is 5 seconds.
| Attribute | Run 1 starts | Run 2 starts | Gap between runs |
|---|---|---|---|
| `fixedRate = 5000` | 0 s | 5 s | 3 s of idle time |
| `fixedDelay = 5000` | 0 s | 7 s | 5 s of idle time |
With fixedRate, the schedule is anchored to the clock. With fixedDelay, each run pushes the following one back.
In the diagram, one day stands for one second.
What happens when a run is too slow
If a fixedRate task takes longer than its interval, Spring does not start a second copy at the same time. With the default single-threaded scheduler, the next run starts late, right after the current one ends. Missed runs are not made up in a burst. The task simply falls behind its intended rhythm.
fixedDelay cannot overlap with itself and cannot fall behind, because the wait always starts after the run completes. The cost is that the number of runs per hour depends on how long each run takes.
Choosing between them
- Use
fixedDelaywhen a run must finish before the next begins, or when you want a rest period between runs. Polling a queue or an external API is the usual case. - Use
fixedRatewhen the start times matter and the work is short, such as sending a heartbeat or sampling a metric. - Use
cronwhen you need wall-clock times like "every day at 02:00". Interval attributes count from application start, so they cannot express that.
Common mistakes
- Assuming
fixedRateruns tasks in parallel. With one scheduler thread, it does not. - Forgetting that
initialDelayis also counted in milliseconds.initialDelay = 10is 10 ms, not 10 seconds. - Choosing
fixedRatefor work that must not overlap with itself, then adding@Asyncand losing that protection. - Setting a very short
fixedRateon a task with unpredictable duration. Runs queue up late and the task can keep other scheduled tasks waiting on the same thread.
Exercise
A task takes 3 seconds to run. It is annotated with @Scheduled(fixedDelay = 10000). The first run starts at second 0. At what second does the third run start?
Show answer
The first run ends at 3 s, so the second run starts at 13 s. It ends at 16 s, so the third run starts at 26 s. With fixedRate = 10000 it would have started at 20 s.
Exercise
You poll a remote API and each call can take anywhere from 1 to 30 seconds. You never want two calls in progress at once. Should you use fixedRate or fixedDelay, and why?
Show answer
Use fixedDelay. The wait starts after the previous call finishes, so calls never overlap and a slow call simply postpones the next one.