Scrivio Interview library
ScenarioIntermediate7 min read · updated 12 November 2026

A team implements retry with backoff for failed webhook deliveries by putting failed deliveries in a DelayQueue consumed by a worker thread. What are the risks, and when would you choose ScheduledExecutorService or a persistent scheduler instead?

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.

Java
DelayQueue<Retry> retries = new DelayQueue<>();
void onFailure(Delivery d, int attempt) {
long delayMs = 1_000L << attempt; // 2 s, 4 s, 8 s ... no cap, no jitter
retries.put(new Retry(d, attempt + 1, delayMs));
}
void worker() throws InterruptedException {
while (true) {
Retry r = retries.take(); // blocks until a retry is due
if (!send(r.delivery())) onFailure(r.delivery(), r.attempt());
}
}

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.

Where retry state can live
DesignSurvives restart?Backlog stored inEffort
DelayQueue + workernoheap, unboundedlow
ScheduledExecutorServicenoheap, unboundedlowest
Database table, polledyesdatabase, indexed by due timemedium
Broker with delayed deliveryyesbrokermedium, 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.

SQL
CREATE TABLE webhook_delivery (
id uuid PRIMARY KEY,
endpoint_id bigint NOT NULL,
payload jsonb NOT NULL,
status text NOT NULL, -- PENDING, DELIVERED, DEAD
attempts int NOT NULL DEFAULT 0,
next_attempt_at timestamptz NOT NULL
);
CREATE INDEX due_deliveries ON webhook_delivery (next_attempt_at) WHERE status = 'PENDING';
-- each worker, every second or so
SELECT id, endpoint_id, payload, attempts
FROM webhook_delivery
WHERE status = 'PENDING' AND next_attempt_at <= now()
ORDER BY next_attempt_at
LIMIT 100
FOR UPDATE SKIP LOCKED; -- other workers skip rows we hold

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.

Java
static final long BASE_MS = 1_000, CAP_MS = 3_600_000; // 1 s base, 1 h maximum delay
static final int MAX_ATTEMPTS = 12;
long nextDelayMs(int attempt) {
long exp = Math.min(CAP_MS, BASE_MS << Math.min(attempt, 20)); // capped exponential
return ThreadLocalRandom.current().nextLong(exp + 1); // full jitter: 0..exp
}
// attempt 1 -> 0..2 s, attempt 5 -> 0..32 s, attempt 12 -> 0..1 h
// after MAX_ATTEMPTS: status = DEAD, notify the customer, allow manual replay

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.

What to say in the interviewAbout 100 seconds aloud

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.
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/