Quick revision
The short answer
In-memory queues lose everything they hold on a crash or redeploy, so the guidance is that they fit only work that can be lost or recomputed, and even then they must be bounded and drained on graceful shutdown within a time limit. Work that must survive goes through a durable broker or an outbox table, with idempotent consumers. Both kinds expose queue depth and age metrics, and the choice weighs broker cost, operations and latency against the business cost of losing work.
What to remember
In this answer, we saw that in-memory queues lose their contents on crash or redeploy, so they fit only work that can be lost or recomputed, and even then must be bounded and drained on graceful shutdown within a time limit. Work that must survive goes to a durable broker or an outbox table written in the same transaction, with idempotent consumers because delivery is at least once. Every queue exposes depth and age, and teams weigh broker cost and latency against the business cost of losing work, with durable as the default.
Your turn
Answer it aloud as you would in the interview, or type your answer below. Then show one part at a time and compare.
In-memory queues lose everything they hold on a crash or redeploy, so the guidance is that they fit only work that can be lost or recomputed, and even then they must be bounded and drained on graceful shutdown within a time limit. Work that must survive goes through a durable broker or an outbox table, with idempotent consumers. Both kinds expose queue depth and age metrics, and the choice weighs broker cost, operations and latency against the business cost of losing work.
Why this question is asked
Buffering work in a BlockingQueue between a request handler and a background thread is easy and fast, so teams do it everywhere. The cost only shows during deploys and crashes, when whatever sat in the queue silently disappears: confirmation emails never sent, audit records missing, invoices not generated. After a few such incidents, someone has to set guidance that teams can apply without a meeting each time. Interviewers ask this to see whether we can turn incidents into a clear, proportionate rule. In this answer, we will see exactly how queued work is lost, define which work may live in memory, look at the durable options including the outbox pattern, set the rules for in-memory queues that remain, and add the metrics and the cost comparison.
1How Deploys Lose Queued Work
An in-memory queue exists only inside one JVM process. When the process stops, by crash, by OOM kill, or by a deploy that replaces the instance, the queue and its contents are gone. A typical flow is: the request handler saves an order, puts a send-confirmation task into the queue, and returns 200 to the client. The client believes the work is accepted. If the instance is stopped one second later with 3,000 tasks queued, those 3,000 emails are never sent, and nothing records that they were ever owed.
Graceful shutdown helps only partly. Kubernetes sends SIGTERM and by default waits 30 seconds before killing the pod. If the queue holds more work than can be finished in that window, or the process crashes instead of shutting down, the remainder is lost. So the honest statement is that anything in an in-memory queue may be lost at any time.
6 messages in order. Play them, or step through them one at a time.
The client was told the work was accepted, but the only record of it lived in memory.
2Which Work May Live Only in Memory
The deciding question is: if this work were silently lost, would anyone need to know, and could it be recreated? Some work is genuinely disposable. Cache warm-ups, metrics batching, prefetching, recomputing a derived view that will be rebuilt on the next change, or log shipping where a small gap is acceptable can all be lost without harm, because they are either repeated anyway or recomputable from durable data.
Other work carries an obligation: sending a payment instruction, a customer email, a webhook, an audit record, an inventory update. Losing it creates a wrong state that nobody notices until a customer complains. That work must be written somewhere durable before we tell anyone it has been accepted.
- Can this work be lost or recomputed without harm?YesIn-memory queue allowed, with the rules belowNoIt needs durable storage
- Is it created inside a database transaction?YesOutbox table in the same transactionNoPublish to a durable broker
- Consumers are idempotentdurable delivery means at least once
- Expose depth and age metricsfor every queue, in memory or durable
4 steps in order. Play them, or step through them one at a time.
3Durable Options: Broker or Outbox
For work that must survive, a message broker such as Kafka, RabbitMQ or a cloud queue keeps messages on disk, often replicated, until a consumer acknowledges them. A deploy then only pauses processing; the messages wait. The producer must make sure the publish succeeded before replying to the client.
When the work is created together with a database change, a broker introduces a new problem: we cannot commit the order and publish the message atomically, so a crash between the two either loses the message or sends one for an order that was rolled back. The outbox pattern solves this. We insert the task into an outbox table in the same transaction as the business change, and a separate relay, either a poller or a change data capture tool such as Debezium, reads the outbox and publishes or processes it. For moderate volumes, workers can process the outbox table directly, claiming rows with SELECT ... FOR UPDATE SKIP LOCKED, and no broker is needed at all.
Both durable options deliver at least once: after a crash, a task may be processed again because its completion was not recorded. So consumers must be idempotent, for example by recording a processed task id, or by using an idempotency key that the email or payment provider deduplicates.
4Rules for the In-Memory Queues That Remain
In-memory queues are still the right tool for disposable work, but with three rules. First, they are always bounded, with an explicit rejection behaviour, because an unbounded queue turns a slow consumer into an OutOfMemoryError. Second, they are drained on graceful shutdown within a time limit: stop accepting new work, let workers finish what is queued, and give up after a bound shorter than the platform's grace period, logging how many tasks were dropped. Third, the code says in a comment or in the type name that the queue is lossy, so nobody later puts important work into it.
In Spring, a ThreadPoolTaskExecutor supports this directly with setWaitForTasksToCompleteOnShutdown(true) and setAwaitTerminationSeconds(20), and server.shutdown=graceful with spring.lifecycle.timeout-per-shutdown-phase controls how long the application waits. The numbers must fit inside the platform's termination grace period, or the process is killed in the middle of draining.
5Metrics and Costs
Every queue, in memory or durable, exposes two metrics: depth, the number of waiting items, and age, how long the oldest item has been waiting. Depth shows load; age shows whether the work is late, which is what users feel. For brokers, consumer lag plays the same role. Alerts on age catch stuck consumers early, before a deploy or crash turns a backlog into a loss.
Durability has a price. A broker is infrastructure to run, monitor, secure and upgrade, or a managed service to pay for; it adds a few milliseconds per message, and it requires idempotent consumers. The outbox adds writes to the main database. So the guidance asks each team to compare that price with the business cost of lost work. A lost cache warm-up costs nothing; a lost payment instruction may cost money, trust and a regulatory report. When in doubt, the default is durable, and an in-memory queue requires the team to state why loss is acceptable.
6Follow-up questions
Is a Redis list a durable queue?
Only partly. Redis keeps data in memory and persists it with snapshots or an append-only file, so depending on configuration, recent writes can be lost on a crash. It survives our application's deploys, which is already a big improvement, but for work that must never be lost we should know its persistence settings or use a broker designed for durable delivery.
Why not just make shutdown wait until the queue is empty?
Because crashes and kills do not wait, and an unbounded wait during a deploy can block rollouts for a long time. Draining reduces loss in the normal case; it cannot guarantee zero loss, so it is fine for disposable work and not enough for important work.
How do you roll this guidance out to existing teams?
Start with an inventory of in-process queues and classify each one with its owners using the question about loss. Fix the queues that carry obligations first, starting with the ones involved in incidents, and add the depth and age metrics everywhere. New code follows the guidance through review.
7Conclusion
In this answer, we saw that in-memory queues lose their contents on crash or redeploy, so they fit only work that can be lost or recomputed, and even then must be bounded and drained on graceful shutdown within a time limit. Work that must survive goes to a durable broker or an outbox table written in the same transaction, with idempotent consumers because delivery is at least once. Every queue exposes depth and age, and teams weigh broker cost and latency against the business cost of losing work, with durable as the default.
Open with
My guidance would start from one fact: anything in an in-memory queue may be lost at any time, on a crash, an OOM kill or a deploy, and graceful shutdown only reduces that, because the platform kills the process after a grace period.
Then, if they want more
So the question for each queue is whether the work can be lost or recomputed without harm. Cache warm-ups, metrics batching or recomputable views can stay in memory. Anything that carries an obligation, such as emails, payments, webhooks or audit records, must be stored durably before we tell the caller it was accepted.
For that I would use a durable broker, or, when the work is created together with a database change, an outbox table written in the same transaction, with a relay or workers claiming rows with SKIP LOCKED. Both give at-least-once delivery, so consumers must be idempotent.
The in-memory queues that remain must be bounded with an explicit rejection behaviour, and drained on graceful shutdown with a time limit that fits inside the platform's grace period, logging what was dropped. Every queue, in memory or durable, exposes depth and the age of the oldest item, with alerts on age.
Finally, I would be explicit about the trade-off: a broker costs money, operations and a little latency, so teams weigh that against the business cost of losing the work, but the default is durable and an in-memory queue needs a stated reason.
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.
- Official documentation
- The Collections Framework, overview, java.util, package documentation
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 "Several teams use in-process BlockingQueues to buffer work between request handling and...": https://scrivio-interview-library.netlify.app/java/queues-and-deques/675a04b1a5/