@Scheduled(fixedRate = ...) runs a Spring method repeatedly, starting a new run every given number of milliseconds measured from the start of the previous run. Unlike fixedDelay, the interval does not include the time the method takes to finish. If the method runs longer than the interval, the next run starts as soon as the previous one ends, because the default scheduler uses a single thread.
You need @EnableScheduling on a configuration class, and the method must be on a Spring bean, return void and take no parameters.
With fixedRate = 5000, runs are planned for t=0, t=5s, t=10s and so on, regardless of how long each takes.
The start times stay close to a 5 second grid. That makes fixedRate a good fit for polling, metrics sampling and heartbeats.
| Attribute | Purpose |
|---|---|
fixedRate | Interval in milliseconds (a long) |
fixedRateString | Same, as a string, allows ${...} placeholders or an ISO-8601 duration |
initialDelay | Delay before the first run, also initialDelayString |
timeUnit | Unit for the numeric attributes, for example TimeUnit.SECONDS (Spring 5.3.10 and later) |
Externalising the rate lets you change it per environment without a rebuild.
| Scenario | fixedRate = 5000, task takes 2 s | fixedDelay = 5000, task takes 2 s |
|---|---|---|
| Start times | 0 s, 5 s, 10 s | 0 s, 7 s, 14 s |
| Task takes 8 s | Starts at 0 s, then at 8 s, immediately when done | Starts at 0 s, then at 13 s |
Choose fixedDelay when a gap between runs matters, for example to avoid hammering a database. Choose fixedRate when the schedule should stay steady.
On the default single-thread scheduler, a long run delays the next one, so runs never overlap but they bunch up and run back to back until the schedule catches up. Overlapping can happen when the method is also marked @Async or when you configure a multi-thread scheduler and another task triggers the same job manually.
If overlap would be harmful, either use fixedDelay, or guard the method:
The default scheduler has one thread, so slow tasks block other scheduled tasks. In Spring Boot raise the pool size:
Also remember that every application instance runs its own timer. With multiple replicas a fixedRate job runs once per replica. Use a distributed lock such as ShedLock, or Quartz in clustered mode, to restrict it to one instance.
Avoid sleeping in tests. Keep the work in a separate method that you can call directly, and use an integration test with Awaitility only to prove the schedule is wired up. Setting a short rate such as jobs.sync.rate=PT0.1S in the test properties, together with fixedRateString, keeps the test fast without changing production code.
On the default single-thread scheduler, yes: the next run cannot start until the current one ends, so it starts late. It does not skip runs. With a multi-thread scheduler and @Async, runs can overlap.
Milliseconds, unless you set timeUnit.
Not with the annotation. Use SchedulingConfigurer with a Trigger or a TaskScheduler bean to compute the schedule dynamically.
fixedRate repeats at a steady interval from application start, while cron runs at calendar times such as 02:30 every day.
@Scheduled(fixedRate = ...) starts a task at a steady interval measured between starts. Combine it with initialDelay and timeUnit, externalise the interval with fixedRateString, expect late starts rather than overlaps on the default scheduler, and add a pool or a distributed lock when your workload or deployment needs it.