Spring Boot Scheduling Standards
Priority: P0
Configure Scheduled Tasks
- ThreadPool: ALWAYS configure a dedicated
TaskScheduler(default is 1 thread). Enable with@EnableSchedulingannotation. - Async: Keep
@Scheduledmethods light; offload to@Async/Queues. Wrap logic in try/catch; log errors and use@Retryablefor retry on transient failures.
Lock Tasks with ShedLock
- Problem:
@Scheduledruns on ALL pods in K8s. - Solution: Use ShedLock to guarantee single execution.
- Config: Set
lockAtMostFor(deadlock safety) andlockAtLeastFor(debounce).
See implementation examples for ShedLock distributed task configuration and scheduler pool setup.
Anti-Patterns
- No Default Pool: Configure dedicated TaskScheduler (default is 1 thread).
- No duplicates: Use ShedLock for distributed cron in multi-pod deployments.
- No task state: Design tasks to be idempotent; assume pod can restart.