Scrivio Interview library
ScenarioIntermediate7 min read · updated 12 November 2026

A service uses Executors.newFixedThreadPool(20) to process incoming events. During a downstream slowdown, heap usage climbs until OutOfMemoryError, and event latency was minutes before the crash. Explain and redesign.

Executors.newFixedThreadPool(20) puts tasks into an unbounded LinkedBlockingQueue, so when the downstream slows down and the 20 workers fall behind, tasks pile up without limit: latency grows to minutes first, and memory runs out later. The redesign is a ThreadPoolExecutor with a bounded queue sized from the latency budget using Little's law, a rejection policy that applies backpressure or sheds load with alerts on rejections and queue depth, and timeouts and circuit breaking on the downstream calls.

Why this question is asked

This is one of the most common production failures in Java services, and it is caused by one convenient line of code. The fixed thread pool looks safe because the number of threads is limited, but the queue in front of the threads is not. Interviewers ask about it because the answer shows whether we understand what an executor really is, how queues turn slowness into latency and then into memory, and how to design for overload. In this answer, we will look inside newFixedThreadPool, replay the incident with numbers, apply Little's law to size a bounded queue, choose a rejection policy, and protect the workers from a slow downstream.

1What newFixedThreadPool Really Creates

An executor has two parts: a set of worker threads and a queue of tasks waiting for a worker. newFixedThreadPool(20) is a shortcut for a ThreadPoolExecutor with exactly 20 threads and a LinkedBlockingQueue created without a capacity, which means a capacity of Integer.MAX_VALUE.

Java
// What Executors.newFixedThreadPool(20) does inside
new ThreadPoolExecutor(20, 20, // core and maximum pool size
0L, TimeUnit.MILLISECONDS, // keep-alive (irrelevant here)
new LinkedBlockingQueue<Runnable>()); // capacity 2,147,483,647

So execute and submit never refuse a task and never block the caller. When all 20 threads are busy, every new task simply joins the queue. That is convenient on a normal day and dangerous on a bad one, because the queue has no way to say the service is overloaded.

2Replaying the Incident With Numbers

Say events arrive at 300 per second and each one makes a downstream call that normally takes 50 ms. Twenty threads can then complete 20 / 0.05 = 400 events per second, comfortably above the arrival rate, and the queue stays near empty.

Now the downstream slows to 2 seconds per call. Capacity drops to 20 / 2 = 10 events per second while 300 keep arriving, so the queue grows by 290 tasks every second: 17,400 per minute, over a million in an hour. A new event joins the back of the queue and must wait for everything ahead of it: with 17,400 queued and 10 completed per second, that is about 29 minutes. Latency is in minutes long before memory is the problem, which matches what was observed.

Memory grows at the same time. Each queued task holds the event payload, its lambda and the queue node; at 20 KB per event, 290 tasks per second is about 5.8 MB per second, roughly 350 MB per minute. Within several minutes the heap is full, garbage collection runs constantly, and finally the JVM throws OutOfMemoryError, losing every queued event at once.

Step throughHow a slow downstream becomes OutOfMemoryError
  1. Downstream slows from 50 ms to 2 scapacity falls from 400/s to 10/s
  2. Arrivals continue at 300/snothing refuses or slows them
  3. Unbounded queue grows by 290/s17,400 tasks per minute
  4. Every new event waits behind the queuelatency reaches minutes
  5. Queued payloads fill the heapGC thrashes, then OutOfMemoryError
  6. All queued events are lost at once

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

3Sizing a Bounded Queue With Little's Law

Little's law says that in a stable system, the average number of items in it equals the arrival rate times the average time each item spends there: L = lambda x W. Applied to our queue, the number of queued tasks equals the rate at which tasks leave it times how long they wait. Turned around, if we decide how long a task may wait, the law tells us how long the queue may be.

Suppose the business says an event processed more than 2 seconds after arrival is useless, and on a healthy day the pool completes 400 per second. Then the queue should hold at most about 400 x 2 = 800 tasks. A longer queue does not help: any task beyond position 800 would wait longer than 2 seconds even on a good day, so it only wastes memory and work. The bound is really a latency budget written as a number.

Java
int workers = 20;
int queueCapacity = 800; // ~ 400/s x 2 s acceptable wait
ThreadPoolExecutor pool = new ThreadPoolExecutor(
workers, workers, 0L, TimeUnit.MILLISECONDS,
new ArrayBlockingQueue<>(queueCapacity), // bounded
Thread.ofPlatform().name("events-", 0).factory(),
new ThreadPoolExecutor.CallerRunsPolicy()); // see the next section

During the slowdown, the queue now fills to 800 within about 3 seconds and stays there. From that point, something must happen to each new event, and we get to choose what.

4Choosing a Rejection Policy

When the queue is full and all threads are busy, ThreadPoolExecutor calls its RejectedExecutionHandler. The JDK provides four. AbortPolicy, the default, throws RejectedExecutionException to the caller. CallerRunsPolicy runs the task on the caller's thread, which slows the caller down, a simple form of backpressure. DiscardPolicy drops the new task silently, and DiscardOldestPolicy drops the oldest queued task to make room.

The right choice depends on where events come from. If they come from a Kafka consumer, backpressure is ideal: CallerRunsPolicy, or a blocking submit, makes the consumer thread slow down, so it polls less often and the remaining events wait safely in Kafka, not in our heap. If they come from HTTP requests, we should reject quickly with AbortPolicy and return 503 so clients can retry later, rather than make request threads wait. Silent discarding is acceptable only for data that may be lost, such as metrics samples, and even then we count it.

Whatever we pick, rejections are a signal that we are over capacity, so we count them and alert. We also export queue depth, active threads and task wait time, for example with Micrometer's ExecutorServiceMetrics, and alert when depth stays near the bound.

Rejection policies for a full pool
PolicyWhat happensGood for
AbortPolicy (default)caller gets RejectedExecutionExceptionHTTP: return 503 quickly
CallerRunsPolicycaller's thread runs the taskbackpressure on a consumer loop
DiscardPolicynew task dropped silentlylosable data, with a counter
DiscardOldestPolicyoldest queued task droppedonly newest data matters

5Protecting the Workers From the Downstream

A bounded queue limits the damage, but the workers are still stuck for 2 seconds or more per call, so throughput collapses. Every downstream call needs a timeout shorter than our latency budget, for example 500 ms on the HTTP client, so a hung downstream cannot hold a worker indefinitely.

A circuit breaker, such as Resilience4j's, goes further. When a large share of recent calls fail or time out, it opens and fails calls immediately for a while, then lets a few test calls through. Workers stop waiting on a downstream that is clearly unhealthy, and we can fall back, for example by parking events in a retry topic. A bulkhead, a separate limit on concurrent calls to each downstream, prevents one slow dependency from taking all 20 workers.

6Follow-up questions

Why not just increase the number of threads?

More threads help only if the downstream can handle more concurrent calls. When it is slow because it is overloaded, more threads add load and make it slower. More threads also cost memory, and the queue in front of them is still unbounded, so the failure only arrives a little later.

Does CallerRunsPolicy have a downside?

Yes. The task runs on the caller's thread, so if the caller is an HTTP server thread or an event loop, that thread is blocked for the length of the task and the whole service can stall. It fits callers whose slowing down is exactly what we want, such as a dedicated consumer loop.

How do you choose between AbortPolicy and a blocking submit?

Ask whether the caller can wait safely. A Kafka consumer can, because waiting leaves events in Kafka. An HTTP request thread usually cannot, because clients have timeouts and threads are limited, so failing fast with a clear error is better.

7Conclusion

In this answer, we saw that newFixedThreadPool uses an unbounded LinkedBlockingQueue, so a downstream slowdown turns into an ever-growing queue: minutes of latency first, then OutOfMemoryError and lost events. We replaced it with a ThreadPoolExecutor whose bounded queue is sized with Little's law from the latency budget, chose a rejection policy that applies backpressure or sheds load depending on the source, alerted on rejections and queue depth, and added timeouts, a circuit breaker and bulkheads on the downstream calls.

What to say in the interviewAbout 90 seconds aloud

Open with

newFixedThreadPool limits the threads to 20 but puts an unbounded LinkedBlockingQueue in front of them, so execute never refuses work. When the downstream slowed down, say from 50 milliseconds to 2 seconds per call, the pool's capacity fell from 400 events per second to 10, while events kept arriving at a few hundred per second.

Then, if they want more

Every second hundreds of tasks joined the queue, and each new event had to wait behind all of them, which is why latency reached minutes before the crash. The queued tasks held their payloads, so the heap filled until OutOfMemoryError, and then every queued event was lost.

I would build the ThreadPoolExecutor explicitly with a bounded ArrayBlockingQueue sized from the latency budget using Little's law: if a healthy pool completes 400 per second and an event older than two seconds is useless, the queue should hold about 800 tasks, because anything beyond that would miss the budget anyway. Then I choose a rejection policy deliberately.

If events come from Kafka, CallerRunsPolicy or a blocking submit slows the consumer so events wait safely in Kafka; for HTTP I would use AbortPolicy and return 503 quickly. I alert on rejections and on queue depth staying near the bound.

Finally I protect the workers: timeouts on every downstream call below the latency budget, a circuit breaker so we fail fast when the downstream is unhealthy, and a bulkhead per dependency.

About this answer

Last updated
12 November 2026
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 "A service uses Executors.newFixedThreadPool(20) to process incoming events. During a...": https://scrivio-interview-library.netlify.app/java/queues-and-deques/cf6c5623e2/