Scheduling, Async & Caching
The three declarative features built on proxies: @Async for off-thread execution, @Scheduled for periodic work, and the cache abstraction. Interviewers like this area because all three share the same proxy caveats and the same production failure modes.
part ofSpring Frameworkoverview, primer and where to startread it →on this pageshowhide
explore
- Async Method Execution (@Async)6 questions
- Cache Abstraction5 questions
- Task Scheduling (@Scheduled)5 questions
- Async Exception Handling5 questions
- Programmatic & Dynamic Scheduling5 questions
- TaskDecorator & Context Propagation5 questions
questions
page 2 of 2You're designing async exception handling for a platform with many fire-and-forget @Async tasks (emails, audit writes, webhooks). How do you make failures observable and recoverable without falling into the 'silently swallowed' trap?
basics
~20 sDon't rely on the default log-only handler. Register a custom AsyncUncaughtExceptionHandler (via AsyncConfigurer) that logs with context, emits metrics, and alerts or dead-letters. For work needing results or retries, return CompletableFuture and handle failures explicitly, and propagate trace/MDC context onto async threads.
Describe the Cache and CacheManager SPI contracts. How does the annotation layer sit on top of them, and why does this abstraction matter?
basics
~20 sCacheManager is a factory that returns named Cache instances (getCache(name), getCacheNames()). Cache is the store interface: get, put, evict, clear, and get(key, Callable) for atomic loading. The @Cacheable/@CacheEvict annotations are just declarative sugar over these two interfaces, so any provider that implements them plugs in unchanged.
What design considerations and pitfalls arise around the TaskScheduler and thread pool when using programmatic/dynamic scheduling?
basics
~20 sThe default TaskScheduler is single-threaded, so tasks serialize and a slow one delays others. Supply your own ThreadPoolTaskScheduler with adequate size, handle graceful shutdown, make tasks idempotent, and add cluster coordination since Spring scheduling isn't cluster-aware.
What are the risks of propagating RequestContextHolder / request-scoped beans across the async thread boundary, and how would you do it safely?
basics
~20 sThe HttpServletRequest and request-scoped beans live only for the request; once it completes the request/response may be recycled and its attributes cleared. Copying RequestContextHolder to a pool thread risks touching a dead request. Safer: extract the plain values you need before submitting, and pass them explicitly.
What happens when a @Scheduled fixedDelay/fixedRate method throws an unchecked exception versus a cron method? How should you make scheduled jobs resilient?
basics
~20 sAn uncaught exception from a fixedDelay or fixedRate task is logged but the schedule keeps running — future executions still fire. Only if you use a one-shot future would it stop. Make jobs resilient by catching and handling exceptions inside the method rather than letting them propagate.
Compare @EnableAsync's PROXY mode with mode=ASPECTJ. When would you choose ASPECTJ, and what's the cost?
basics
~20 sPROXY (default) wraps beans in a proxy, so only external calls to public methods become async. ASPECTJ weaves the async advice into the bytecode, so self-invocation and non-public methods also work. ASPECTJ needs the AspectJ weaver and compile/load-time weaving setup, which is more complex.
showing 31–36 of 36