Quick revision
The short answer
I would provide one durable, shared way to schedule time-based work, either a small scheduling service or a broker's delayed delivery wrapped in a library, because in-memory delays lose work on restart and every team re-solving clocks and catch-up produces different bugs. Durable schedulers deliver at least once, so consumers must be idempotent, and clock handling, time zones and catch-up after outages are defined once. In-memory delays stay allowed only for work that may be lost, such as cache expiry, and the cost is a small team running the platform plus migration effort.
What to remember
In this answer, we chose a single durable approach for time-based work, a shared scheduling service on a database table or a broker's delayed delivery behind a library, because in-memory delays lose work on restart and per-team solutions repeat the same bugs. We required idempotent consumers because delivery is at least once, defined clock handling, time zones and catch-up policies once, kept in-memory timers for losable work such as cache expiry, and estimated the cost of building, running and migrating to the platform.
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.
I would provide one durable, shared way to schedule time-based work, either a small scheduling service or a broker's delayed delivery wrapped in a library, because in-memory delays lose work on restart and every team re-solving clocks and catch-up produces different bugs. Durable schedulers deliver at least once, so consumers must be idempotent, and clock handling, time zones and catch-up after outages are defined once. In-memory delays stay allowed only for work that may be lost, such as cache expiry, and the cost is a small team running the platform plus migration effort.
Background to this question
Retries, expirations, reminders, scheduled reports and trial ends all share one need: do something at a future time, reliably. When every team solves it separately, an organisation ends up with DelayQueues that forget work on deploy, cron jobs that double-fire on two instances, database pollers with slightly different locking, and reminders that arrive an hour late after daylight saving time changes. A senior engineer asked to fix this must choose an approach, explain its costs honestly and plan the migration. In this answer, we will look at what goes wrong with the current mix, compare the platform options, define the shared rules for idempotency, clocks and catch-up, decide what may stay in memory, and estimate cost and rollout.
1What Goes Wrong With Every Team's Own Approach
Each approach has its own failure. DelayQueue and ScheduledExecutorService keep pending work in memory, so a deploy silently drops every reminder scheduled for later. Cron jobs run on a schedule, but when the service runs on three instances, the job runs three times unless someone adds locking, and when the host is down at the scheduled moment, the run is simply skipped. Database polling is durable, but each team writes its own claiming logic, and some versions double-process or stall under load.
Beyond the individual bugs, the organisation pays repeatedly for the same decisions: how to store due times, how to avoid double execution, what to do after an outage, and how to handle time zones. Each team makes these decisions differently, often implicitly, and each learns from its own incidents.
2Comparing the Platform Options
There are three realistic options. First, a shared scheduling service: teams call an API such as schedule(key, dueAt, payload, target), the service stores the entry durably, and when it is due, it delivers the payload to a queue or endpoint the team owns. It can be built on a database table with indexed due times and SKIP LOCKED claiming, or on an existing library such as Quartz with its JDBC job store, or db-scheduler. Second, a broker with delayed delivery, wrapped in a shared client library, so a team publishes a message with a delay and consumes it when due. Third, a durable workflow engine such as Temporal, whose timers are part of long-running workflows; it is powerful but a much larger adoption.
I would usually choose the shared scheduling service on a database table for a first version, because it handles long delays of days or months well, which many brokers limit, it lets teams list, cancel and reschedule entries by key, which reminders and expirations need, and the technology is familiar. If an organisation already runs a broker with good delayed delivery and most delays are short, wrapping that is cheaper. Whatever we choose, it is one tested implementation instead of ten.
| Option | Strengths | Costs |
|---|---|---|
| Shared scheduling service | long delays, cancel and list by key | build and run a service |
| Broker delayed delivery | reuses existing broker, scales well | delay limits, hard to cancel |
| Workflow engine timers | durable long processes, rich model | large adoption and operations |
| Status quo per team | no platform work | lost work, repeated incidents |
3Rules Defined Once: Idempotency, Clocks and Catch-Up
A durable scheduler cannot guarantee exactly-once delivery: if the scheduler delivers a due item and crashes before recording it, it delivers again after restart. So the platform promises at least once, and every consumer must be idempotent, typically by using the schedule key as an idempotency key, so a second delivery of reminder-123 is recognized and ignored.
Clock handling is defined centrally. Due times are stored as UTC instants. For user-facing schedules expressed in local time, such as a reminder at 9:00 every Monday in the user's zone, we store the local time and the zone id, and compute the next instant from them, so daylight saving changes move the instant, not the local time. The platform also defines what happens to local times that do not exist or occur twice during a clock change, for example by running at the first valid moment.
Catch-up after an outage is the third rule. When the scheduler was down for two hours, thousands of items are overdue. Each schedule declares a policy: run all late items, rate-limited so targets are not flooded; run only the latest, which suits recurring jobs like a report; or skip items that are too old to be useful, such as a reminder for a meeting that already ended. Without a declared policy, teams discover their behaviour during the first outage.
- Team services
schedule(key, dueAt, payload, target); idempotent handlers - Client libraryUTC instants or local time + zone id, catch-up policy
- Scheduling serviceclaims due entries, delivers, retries, rate limits
- Durable storeentries indexed by due time; cancel and list by key
- Delivery targetsteam queues or endpoints; at-least-once
5 layers, from the top down. Step through them one at a time.
4What May Stay In Memory
Not everything needs the platform. In-memory delays remain allowed for work whose loss is harmless: cache entry expiry, debouncing, local timeouts, rate limiter windows, short retry loops within a single request. Losing these on restart only means a cache is rebuilt or a request fails and is retried by the client.
The guidance gives teams one question: if this timer were lost on restart, would anyone have to notice? If yes, it goes to the platform. If no, a ScheduledExecutorService or a DelayQueue is fine, and simpler. That keeps the platform focused on work that matters and avoids sending millions of trivial timers through a central service.
5What It Costs and How to Roll It Out
The platform has real costs. Building a first version of a scheduling service on a database table is a few engineer-months, and running it needs an owning team, perhaps two engineers part-time, on-call coverage, capacity planning for the database and dashboards for due-item lag. Teams pay migration effort: making handlers idempotent and moving their schedules. In exchange, the organisation stops losing reminders and retries on deploys, stops paying for duplicated incidents, and gets consistent behaviour for time zones and outages.
I would roll it out by first piloting with one or two teams whose lost work hurt the most, such as billing retries or trial expirations, then publishing the library and guidance, and migrating the remaining durable cases over a few quarters, prioritized by incident history. New durable timers go to the platform from the start. We measure success by incidents involving lost or duplicated timed work, and by lag between due time and delivery, which the platform publishes per team.
6Follow-up questions
Why not just use cron with a distributed lock?
A lock fixes double execution for recurring jobs, but cron still has no record of individual future items such as one reminder per user, skips runs when the host is down, and leaves catch-up and time zones to each job. It is fine for a handful of periodic maintenance tasks, not as a general platform for per-item timed work.
How would you handle cancellation, for example a reminder for a meeting that was deleted?
The schedule key makes it easy: the owning service calls cancel(key) when the meeting is deleted. Because cancellation can race with delivery, the handler also checks current state when the reminder fires, so a late cancellation still results in no message.
What metrics would the platform publish?
Due-item lag, the time between an item's due time and its delivery, as percentiles per team; the number of overdue items; delivery failures and retries per target; and catch-up activity after outages. Lag is the most important, because it is what users experience as late reminders.
7Conclusion
In this answer, we chose a single durable approach for time-based work, a shared scheduling service on a database table or a broker's delayed delivery behind a library, because in-memory delays lose work on restart and per-team solutions repeat the same bugs. We required idempotent consumers because delivery is at least once, defined clock handling, time zones and catch-up policies once, kept in-memory timers for losable work such as cache expiry, and estimated the cost of building, running and migrating to the platform.
Open with
I would move the organisation to one durable way of scheduling time-based work, with in-memory timers allowed only where loss is harmless. The current mix fails in different ways: DelayQueue and ScheduledExecutorService drop everything pending on a deploy, cron jobs double-fire on several instances or skip runs when a host is down, and every team's database poller has its own claiming bugs.
Then, if they want more
My first choice would usually be a small shared scheduling service on a database table, with an API like schedule a key, a due time, a payload and a target, because it handles long delays, and lets teams cancel and reschedule by key.
If we already run a broker with good delayed delivery and delays are short, wrapping that in a library is cheaper. Durable schedulers deliver at least once, so consumers must be idempotent, using the schedule key as the idempotency key.
The platform defines clocks once: UTC instants, and for local-time schedules the local time plus zone id so daylight saving works, with a rule for times that do not exist. Each schedule declares a catch-up policy for outages: run all late items rate-limited, run only the latest, or skip if too old.
Cache expiry, debouncing and local timeouts stay in memory. The cost is a few engineer-months to build, a small owning team with on-call, and migration effort for teams, which I would start with the cases where lost work hurt most and measure by due-time lag and incidents.
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 "Time-based work such as retries, expirations and reminders is implemented differently by...": https://scrivio-interview-library.netlify.app/java/queues-and-deques/1deef398b3/