About Us Contact Us Write for Us Advertise
Home > Java > Spring Boot Async & Scheduling (Beginner Guide)
Java

Spring Boot Async & Scheduling (Beginner Guide)

Do background work in Spring Boot: @Async to run slow tasks off the request thread, and @Scheduled with fixedRate and cron to run jobs automatically on a timer.

Shiv Pandey
Shiv Pandey
Oct 07, 2026 | 111 views
Spring Boot Async & Scheduling (Beginner Guide)

Real applications do more than respond to requests. They send emails in the background, clean up old data every night, publish scheduled posts, and generate reports on a timer. Spring Boot has clean, built-in support for both patterns: async (do slow work in the background without making the user wait) and scheduling (run tasks automatically on a timer). This lesson shows you both — and yes, this website uses them.

Async: don't make users wait for slow work

Imagine a user signs up and your app sends a welcome email. Sending email can take a couple of seconds. If you do it during the request, the user stares at a spinner the whole time — a bad experience. The fix: send the email asynchronously (in the background) and respond to the user immediately.

Enabling and using @Async

Two simple pieces: enable async once, then mark background methods with @Async:

@SpringBootApplication
@EnableAsync              // turn on async support (once)
public class MyApp { ... }
@Service
public class EmailService {

    @Async                  // runs on a separate background thread
    public void sendWelcomeEmail(String to) {
        // slow work happens here, off the request thread
        mailSender.send(...);
    }
}

Now when your controller calls sendWelcomeEmail(...), the method returns instantly and the actual sending happens in the background. The user gets their "Welcome!" response right away, while the email goes out moments later. This connects back to the idea of doing expensive work efficiently — here, by moving it off the critical path.

Two gotchas: (1) @Async only works when the method is called from another bean, not from within the same class (a quirk of how Spring adds the behaviour). (2) Since it runs in the background, you can't just return a value normally — for a result, return a CompletableFuture. For fire-and-forget tasks like emails, void is perfect.

Scheduling: run tasks automatically on a timer

The other pattern is work that runs on a schedule, with no user involved at all: nightly cleanups, hourly syncs, publishing a post at a set time. Enable scheduling, then annotate methods with @Scheduled:

@SpringBootApplication
@EnableScheduling         // turn on scheduling (once)
public class MyApp { ... }
@Component
public class ScheduledTasks {

    @Scheduled(fixedRate = 60000)          // every 60 seconds
    public void everyMinute() { ... }

    @Scheduled(cron = "0 0 2 * * *")      // every day at 2:00 AM
    public void nightlyCleanup() { ... }
}

Ways to schedule

Option Meaning
fixedRate run every N milliseconds, from each start
fixedDelay wait N ms after each run finishes
cron flexible schedule (specific times/days)

The cron expression ("0 0 2 * * *" = second, minute, hour, day, month, weekday) looks cryptic at first but is incredibly powerful for "run at exactly this time" needs. There are handy online cron helpers to build them.

A real example you're experiencing right now: this website's blog posts can be scheduled to publish at a future date and time. Behind that feature is a Spring @Scheduled job that wakes up on a timer, checks for posts whose scheduled time has arrived, and publishes them — no human needed. If you're reading this, a scheduled task very likely put it live. That's this exact lesson, running in production.

Async and scheduling were the features that made my apps feel truly alive — doing useful things on their own rather than only reacting to clicks. My advice: reach for @Async whenever a user is waiting on slow work they don't need to see finish (emails, notifications, report generation), and @Scheduled for anything that should happen on a clock (cleanups, digests, timed publishing). This website leans on both — background email sending and timed post publishing are core to how it runs. They're simple to add and dramatically expand what your app can do. Just remember the async gotchas above, and you'll avoid the usual beginner traps.

Key takeaways

  • @Async (with @EnableAsync) runs slow work in the background so users aren't kept waiting.
  • Async methods must be called from another bean; return void or CompletableFuture.
  • @Scheduled (with @EnableScheduling) runs tasks automatically via fixedRate, fixedDelay, or cron.
  • Use them for emails, cleanups, syncs, and timed publishing — background work that makes apps feel alive.

← Previous: Spring Lesson 14 — Caching
Next: Spring Lesson 16 — Dockerizing a Spring Boot App →
↑ Back to the Spring Boot roadmap

Related Articles

Spring vs Spring Boot: The Difference Explained (Beginner Guide)
Java

Spring vs Spring Boot: The Difference Explained (Beginner Guide)

Spring Boot Project Ideas (Beginner to Advanced)
Java

Spring Boot Project Ideas (Beginner to Advanced)

Deploying a Spring Boot App to Production (Beginner Guide)
Java

Deploying a Spring Boot App to Production (Beginner Guide)

Dockerizing a Spring Boot App (Beginner Guide)
Java

Dockerizing a Spring Boot App (Beginner Guide)