Quick revision
The short answer
A DelayQueue keeps pending retries only in memory, so a restart or crash silently stops retrying every delivery waiting in it, and because it is unbounded, a long receiver outage builds up a large backlog in the heap. ScheduledExecutorService would need less code but has the same durability problem. If deliveries must not be lost, store retry state in a database, or use a broker with delayed delivery, and poll for due items; in every design, add jitter to the backoff and cap the number of attempts.
What to remember
In this answer, we saw that a DelayQueue retry design loses every pending retry on restart or crash, and that its unbounded queue can fill the heap during a long receiver outage. ScheduledExecutorService simplifies the code but keeps both risks. For deliveries that must not be lost, we stored retry state in a database table polled with FOR UPDATE SKIP LOCKED, or would use a broker with delayed delivery, and we improved the backoff with full jitter, a maximum delay and a capped number of attempts ending in a dead status with replay.
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.
A DelayQueue keeps pending retries only in memory, so a restart or crash silently stops retrying every delivery waiting in it, and because it is unbounded, a long receiver outage builds up a large backlog in the heap. ScheduledExecutorService would need less code but has the same durability problem. If deliveries must not be lost, store retry state in a database, or use a broker with delayed delivery, and poll for due items; in every design, add jitter to the backoff and cap the number of attempts.
Why this question is asked
Webhook delivery is a promise to a customer: when something happens in our system, we will tell their endpoint. Endpoints fail all the time, so retries with backoff are essential, and a DelayQueue looks like a neat way to implement them. Interviewers use this scenario to see whether we can spot the hidden durability and capacity risks of an in-memory design, and know when a simple tool is enough. In this answer, we will look at the team's design, examine what is lost on restart and what grows during outages, compare ScheduledExecutorService, design a persistent scheduler with a database table, and fix the backoff with jitter and an attempt cap.
1The Team's Design
When a delivery fails, the code wraps it in a Delayed element with a deadline based on the attempt number and puts it into a DelayQueue. One worker thread loops on take, which blocks until the earliest retry is due, then calls the customer's endpoint again. If that fails, the delivery goes back into the queue with a longer delay.
It works in a test, and it is simple. The problems appear only when the process restarts, when an endpoint is down for a long time, or when many endpoints recover at the same moment.
2Risk 1: Retries Vanish on Restart
A DelayQueue lives entirely in the heap of one JVM. Every deploy, crash, OOM kill or scale-down discards it. Each delivery waiting for its next attempt simply stops being retried, with no error, no log line and no record that it was ever pending. If a deploy happens during a customer's endpoint outage, every one of their failed events is lost.
From the customer's point of view, events are missing with no explanation, and from ours, there is nothing to investigate because the state is gone. For webhooks, which are often used to drive billing, provisioning or order fulfilment, that is usually unacceptable.
3Risk 2: An Unbounded Backlog in Memory
DelayQueue has no capacity limit. Suppose a large customer's endpoint is down for six hours and we produce 50 events per second for them. With retries spreading over hours, almost every event of those six hours sits in the queue: about 1,080,000 elements. At 5 KB of payload each, that is around 5 GB of heap for one customer's outage. Garbage collection slows everything down, and the eventual OutOfMemoryError loses all pending retries of every customer at once.
The single worker thread is a further risk. A slow endpoint that takes 30 seconds to time out blocks retries for all other customers, so one bad endpoint delays everyone. Retries need timeouts and more than one worker, with limits per endpoint so one customer cannot consume them all.
4Would ScheduledExecutorService Be Better?
ScheduledExecutorService does the same job with less code. We call schedule(() -> retry(d), delay, unit) and a pool thread runs the task when the delay expires; there is no loop or Delayed class to write. Internally ScheduledThreadPoolExecutor keeps its tasks in a delay-ordered work queue, very much like a DelayQueue, which is also unbounded and in memory.
So it improves the code but not the risks: retries still vanish on restart, and the backlog still grows in the heap. It is a fine choice when losing a retry is acceptable, for example best-effort notifications, or when the persistent state lives elsewhere and the in-memory schedule is only an optimization.
| Design | Survives restart? | Backlog stored in | Effort |
|---|---|---|---|
DelayQueue + worker | no | heap, unbounded | low |
ScheduledExecutorService | no | heap, unbounded | lowest |
| Database table, polled | yes | database, indexed by due time | medium |
| Broker with delayed delivery | yes | broker | medium, plus operating the broker |
5A Persistent Retry Scheduler
If deliveries must not be lost, the retry state must be durable. The simplest durable design is a table holding one row per delivery with its status, attempt count and next attempt time. Workers poll for due rows, claim them so no two workers take the same one, and update the row after each attempt. Because the state is in the database, a restart only pauses retries; on startup, workers pick up where they left off.
After an attempt, the worker sets status to DELIVERED, or increments attempts and sets the next attempt time, in the same transaction. The backlog is now on disk, indexed by due time, so a six-hour outage costs disk space rather than heap. Since a worker can crash after sending but before updating the row, a delivery can be sent twice; we include a delivery id header so receivers can deduplicate, which is standard practice for webhooks.
A broker with delayed delivery is the other durable option: RabbitMQ with a delayed message plugin or TTL and dead-letter queues, a cloud queue with a delay setting, or Kafka retry topics with delays, which Spring Kafka supports with @RetryableTopic. These scale further but add infrastructure, and some limit how long a delay may be, so check the limits of the broker you choose.
6Jitter and a Cap on Attempts
The team's backoff doubles the delay with each attempt but has two flaws. Without jitter, all deliveries that failed at the same moment retry at the same moment, so when an endpoint recovers, it receives the whole backlog in synchronized waves and may fall over again. Without a cap, the delay keeps doubling, 2 seconds after one failure but 2 to the power of 20 seconds, about 12 days, after twenty, and retries never end.
Full jitter picks a random delay between zero and the capped exponential value, which spreads retries evenly and avoids waves. After the last attempt, the delivery is marked dead rather than dropped, so the customer can see it in a dashboard and replay it once their endpoint is fixed.
7Follow-up questions
Is it acceptable to keep the DelayQueue as a cache in front of the table?
Yes, as an optimization. Workers can load due-soon rows into memory to avoid polling the database too often, as long as the table remains the source of truth. A restart then loses only the in-memory copy, and the next poll reloads it.
How do you stop one failing endpoint from slowing everyone else?
Give each attempt a short timeout, limit concurrent attempts per endpoint, and use several workers. If an endpoint fails consistently, a per-endpoint circuit breaker can pause its deliveries for a while instead of spending attempts on it.
Why mark deliveries as dead instead of retrying forever?
Because some endpoints never come back, and infinite retries grow the backlog and cost forever. A cap with a dead status, a notification and a manual replay keeps the system bounded while still letting the customer recover their events.
8Conclusion
In this answer, we saw that a DelayQueue retry design loses every pending retry on restart or crash, and that its unbounded queue can fill the heap during a long receiver outage. ScheduledExecutorService simplifies the code but keeps both risks. For deliveries that must not be lost, we stored retry state in a database table polled with FOR UPDATE SKIP LOCKED, or would use a broker with delayed delivery, and we improved the backoff with full jitter, a maximum delay and a capped number of attempts ending in a dead status with replay.
Open with
The main risk is durability. A DelayQueue lives only in the JVM's heap, so on every deploy, crash or scale-down, every delivery waiting for its next retry is silently dropped, with no record that it was pending.
Then, if they want more
If a deploy happens during a customer's outage, all their failed events are lost. The second risk is capacity: DelayQueue is unbounded, so a long outage of a busy endpoint can put hundreds of thousands of deliveries in memory and end in an OutOfMemoryError that loses everyone's retries. A single worker thread also lets one slow endpoint delay all others.
ScheduledExecutorService would remove some code, since we just schedule a task with a delay, but its internal queue is the same kind of unbounded in-memory structure, so it has the same durability problem. I would use either only when losing a retry is acceptable.
Since webhooks usually matter, I would store retry state durably: a table with status, attempt count and next attempt time, indexed on due time, polled by several workers with FOR UPDATE SKIP LOCKED, updating the row after each attempt. A broker with delayed delivery, such as retry topics in Kafka, is the alternative at larger scale.
Delivery becomes at least once, so we send a delivery id for deduplication. Finally I would fix the backoff: exponential with a maximum delay, full jitter so recovered endpoints are not hit in waves, and a cap of perhaps twelve attempts, after which the delivery is marked dead and can be replayed.
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 "A team implements retry with backoff for failed webhook deliveries by putting failed...": https://scrivio-interview-library.netlify.app/java/queues-and-deques/c68462e0fd/