Scrivio Interview library
ScenarioIntermediate6 min read · updated 12 November 2026

An @Async method that sends notifications causes memory to grow during traffic spikes until the service runs out of heap. Boot's default task executor is in use. What is happening and how do you fix it?

Boot's auto-configured ThreadPoolTaskExecutor has an unbounded queue by default, so during a spike every notification task waits in memory, holding its message and recipient, until the heap runs out. Because a thread pool only grows past its core size when the queue is full, the max-size setting never takes effect either, and only 8 threads ever work. The fix is to bound the queue and size the pool (through spring.task.execution.pool.* or a dedicated executor), pick a rejection policy such as CallerRunsPolicy for back-pressure, monitor queue size, and move work that must not be lost to a durable message queue.

Why this question is asked

Out-of-memory errors that appear only under load are some of the hardest incidents to read, because the heap dump shows millions of ordinary objects and nothing obviously wrong. This one is a good interview scenario because the cause is a default most people never read, and the fix touches capacity planning, back-pressure and reliability. In this answer, we will recap how a ThreadPoolExecutor decides what to do with a new task, see Boot's defaults, reproduce the memory growth, fix it with a bounded pool and a rejection policy, add metrics, and discuss when in-memory tasks are the wrong tool altogether.

1A Quick Recap of How a Thread Pool Accepts Tasks

Spring's ThreadPoolTaskExecutor wraps Java's ThreadPoolExecutor, and that class follows a fixed order for each submitted task. If fewer than core-size threads exist, it starts a new thread. Otherwise it tries to put the task in the queue. Only if the queue refuses, because it is full, does it start extra threads up to max-size. If even that is impossible, it calls the rejection handler.

Notice the surprising part: the pool prefers queueing over growing. Extra threads are a last resort before rejection, not the first response to load.

Step throughWhat the pool does with a new task
  1. Fewer than core-size threads?core 8 by default
    YesStart a threadNoTry the queue
  2. Queue has space?unbounded by default: always yes
    YesQueue the taskNoTry to add a thread
  3. Fewer than max-size threads?only reached when the queue is full
    YesStart a threadNoCall the rejection handler

3 steps in order. Play them, or step through them one at a time.

2Boot's Defaults and Why They Bite

Boot auto-configures one executor, named applicationTaskExecutor, and @Async uses it unless told otherwise. Its pool has 8 core threads, and both max-size and queue-capacity default to Integer.MAX_VALUE. So the queue is effectively unbounded, and the second step in the diagram always answers yes.

Properties
spring.task.execution.pool.core-size=8 # default
spring.task.execution.pool.max-size=2147483647 # default, never reached
spring.task.execution.pool.queue-capacity=2147483647 # default: unbounded

During a spike, say 2,000 sign-ups per second, each task captures a User, an email body and a template model, perhaps 20 KB. If SMTP sends 100 emails per second with 8 threads, the queue grows by 1,900 tasks per second, about 38 MB per second. After a few minutes of the spike the heap is gone, and the heap dump shows a LinkedBlockingQueue holding millions of lambdas.

Raising max-size to 50 changes nothing, because the queue is never full, so the pool never reaches step 3. That is the detail interviewers want to hear.

3Fix: Bound the Queue and Size the Pool

The fix starts with numbers. The queue should hold only as many tasks as we can tolerate losing on a crash and finishing within an acceptable delay; the pool should be sized for the downstream, here SMTP, not for our CPU count.

Properties
spring.task.execution.pool.core-size=16
spring.task.execution.pool.max-size=32
spring.task.execution.pool.queue-capacity=500 # bounded: about 10 MB at 20 KB per task
spring.task.execution.thread-name-prefix=notify-

For notification work it is cleaner to give it its own executor so that one slow downstream cannot starve other @Async work. Then we can also set a rejection policy, which decides what happens when both the queue and the pool are full.

4A Dedicated Executor With Back-Pressure

With CallerRunsPolicy, a rejected task runs on the thread that submitted it. The request thread then spends time sending the email itself, which slows down incoming requests and naturally throttles the producer. That is back-pressure: the system slows the source instead of buffering without limit.

Java
@Bean
ThreadPoolTaskExecutor notificationExecutor() {
ThreadPoolTaskExecutor ex = new ThreadPoolTaskExecutor();
ex.setCorePoolSize(16);
ex.setMaxPoolSize(32);
ex.setQueueCapacity(500);
ex.setThreadNamePrefix("notify-");
ex.setRejectedExecutionHandler(new ThreadPoolExecutor.CallerRunsPolicy()); // slow the caller
return ex;
}
@Async("notificationExecutor")
public void sendWelcome(User u) { mailer.send(u); }

The alternatives are AbortPolicy, the default, which throws TaskRejectedException so the caller can record the failure, and the discard policies, which drop tasks silently and are only acceptable for work nobody will miss. Note that defining any Executor bean of our own can make Boot's auto-configured one back off, depending on the version, so name our bean clearly and check which executor @Async without a name now uses.

5Watching the Queue

A bounded queue makes the problem visible instead of fatal, but only if we look. Boot binds Micrometer metrics for ThreadPoolTaskExecutor beans, including executor.active, executor.queued, executor.queue.remaining and executor.pool.size, tagged with the executor name. Rejections are not counted for us, so a small wrapper around the rejection handler increments a counter.

We alert when executor.queue.remaining stays near zero, because that means we are throttling callers, and on any rejection count for executors where losing work matters.

6When In-Memory Tasks Are the Wrong Tool

Even a perfectly sized executor loses its queue on every deployment or crash, because the tasks exist only in the heap. For a welcome email that may be acceptable. For a password reset or an order confirmation it is not. Durable work belongs in a message broker such as Kafka or RabbitMQ, or in an outbox table that a worker drains. The broker absorbs spikes on disk instead of in heap, survives restarts and allows retries, and consumers pull at the rate SMTP can handle.

7Follow-up questions

How does enabling virtual threads change the auto-configured executor?

With spring.threads.virtual.enabled=true on Java 21 or later, Boot auto-configures a SimpleAsyncTaskExecutor that starts a new virtual thread for every task, and the pool and queue properties no longer apply. There is no queue, so tasks do not pile up waiting, but there is also no limit: 2,000 tasks per second become 2,000 concurrent senders hammering SMTP, and each still holds its data in memory while it waits. We bound it with spring.task.execution.simple.concurrency-limit, which makes extra submitters wait, or with a semaphore around the SMTP call.

Why not just make the queue very large, like 100,000?

A large queue only delays the failure and hides it. At 20 KB per task that is 2 GB of heap, and the last task in the queue waits many minutes, which may be worse than rejecting it. The queue size should come from acceptable delay and acceptable loss, not from how much memory we have.

Is CallerRunsPolicy always safe?

No. If the caller is a Kafka consumer or a scheduler thread, running the task there can block other important work. It is a good default for request threads in a web app where slowing requests is the intended back-pressure, and a bad one where the caller must never block.

8Conclusion

In this answer, we saw that Boot's default executor queues without limit, so a spike turns into heap growth and max-size never applies. We bounded the queue, sized the pool for the downstream, gave notifications their own executor with CallerRunsPolicy, watched executor.queued and rejections, and moved durable work to a broker.

What to say in the interviewAbout 90 seconds aloud

Open with

What's happening is that Boot's auto-configured ThreadPoolTaskExecutor has 8 core threads and a queue capacity of Integer.MAX_VALUE, so it's effectively unbounded. A ThreadPoolExecutor only adds threads beyond the core size when the queue is full, so during a spike the queue just keeps growing, max-size never takes effect, and each queued task holds its user, email body and template data in the heap until we run out.

Then, if they want more

I'd fix it in layers. First, bound the queue and size the pool for the downstream, for example core 16, max 32 and a queue of 500, using spring.task.execution.pool properties or, better, a dedicated notificationExecutor bean so notifications can't starve other async work. Second, choose a rejection policy.

CallerRunsPolicy gives back-pressure on request threads by making the caller send the email itself, while AbortPolicy lets us record the failure. Third, monitor it: Boot exposes executor.queued, executor.active and queue.remaining through Micrometer, and I'd add a counter for rejections and alert on them.

Finally, these in-memory tasks are lost on every restart, so for emails that must be delivered, like order confirmations, I'd publish to a durable queue or an outbox table and let a consumer send at the rate SMTP can handle.

If we switched on virtual threads, the queue disappears but so does the limit, so I'd set the simple executor's concurrency limit.

About this answer

Last updated
12 November 2026
Versions it names
Java 21
Checked
Its structure and format are checked automatically. Use it as a carefully prepared study note: where a detail matters, confirm it against the documentation below. What the checks cover
Where the question comes from
Written by Scrivio for practice. It is not taken from an interview.
Report an issue with this answer

Copy the line below and send it to the author with what you found. It carries the address of this page.

Issue with "An @Async method that sends notifications causes memory to grow during traffic spikes...": https://scrivio-interview-library.netlify.app/spring-boot/spring-utilities/10d124a967/