Quick revision
The short answer
Job definitions live in a database table with an index on next_fire_time, and worker nodes poll for due jobs and claim them with SELECT ... FOR UPDATE SKIP LOCKED or a leased status with an expiry. Each execution calls its HTTP or message target with a unique execution id, so targets can ignore duplicates, failed runs are retried with backoff and then marked failed with an alert to the owner, and every run is recorded with its start, end, result and attempts. Recurring jobs get their next fire time from a cron parser such as Spring's CronExpression.
What to remember
In this answer, we designed a scheduler around two tables, with an index on next_fire_time. Worker nodes claim due jobs with SKIP LOCKED and advance next_fire_time in the same transaction, call targets with an execution id so duplicates can be ignored, retry with backoff before failing and alerting the owner, record every run, compute recurring schedules with Spring's CronExpression in the job's zone, and spread midnight bursts with jitter and per-target limits.
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.
Job definitions live in a database table with an index on next_fire_time, and worker nodes poll for due jobs and claim them with SELECT ... FOR UPDATE SKIP LOCKED or a leased status with an expiry. Each execution calls its HTTP or message target with a unique execution id, so targets can ignore duplicates, failed runs are retried with backoff and then marked failed with an alert to the owner, and every run is recorded with its start, end, result and attempts. Recurring jobs get their next fire time from a cron parser such as Spring's CronExpression.
Why this question is asked
Almost every company ends up with a pile of scheduled work: nightly reports, reminder emails, data clean-ups, calls to partner systems at fixed times. A shared scheduler service saves each team from building its own, but only if it is trustworthy: jobs must not be lost, must not run twice without the target knowing, and owners must see what happened. Interviewers ask this to see whether we can design a reliable distributed system from simple, well-understood parts. In this answer, we will define what the service promises, design the tables, claim due jobs across nodes, execute with an execution id, handle retries and alerts, compute recurring schedules, and avoid a thundering herd at midnight.
1What the Service Promises
Teams register jobs through an API. A job is either one-off, run once at 2026-12-01T09:00Z, or recurring, run at 02:00 every day in Europe/Berlin. Its target is an HTTP endpoint to call or a Kafka topic to publish to, with a payload.
The key promise is at-least-once execution. It means every due job is executed one or more times, never zero times. Exactly-once is not possible across a network: if the scheduler calls a target and the connection drops before the reply, the scheduler cannot know whether the work happened, so it must retry. We therefore promise at-least-once and give targets the means to detect duplicates. We also promise retries with backoff and a complete history of runs.
2The Tables: Definitions and Runs
Two tables carry the state. job_definitions holds what to run and when; job_runs holds what actually happened. The most important column is next_fire_time, the next moment this job is due. Workers only ever ask one question, which enabled jobs have next_fire_time in the past, so that column gets an index, and the query stays fast whether we store a thousand jobs or a million.
Notice that a run is its own row with its own execution_id. That gives us the history for free, and it lets us retry a run without touching the definition.
3Claiming Due Jobs Across Worker Nodes
Several worker nodes poll every second. Each runs a short transaction that selects due definitions with FOR UPDATE SKIP LOCKED, so rows locked by one node are skipped by the others, and for each claimed definition it inserts a job_runs row with a lease and advances next_fire_time. For a one-off job, next_fire_time becomes null; for a recurring job, it becomes the next time from the cron expression. Then the transaction commits.
Advancing next_fire_time in the same transaction as the claim is the reason no fire is ever taken twice: once committed, the definition is no longer due, and the run row is the only record of this fire. If a node dies before or during execution, its run rows stay with an expired lease, and a sweeper sets them back for another node to pick up.
- Poll due definitionsnext_fire_time <=
now(), SKIP LOCKED - Insert run with leasenew execution_id, attempt 1
- Advance next_fire_time
CronExpression.next, same transaction - Call target with execution idHTTP header or message field
- Succeeded?YesRecord result and end timeNoRetry with backoff
- Attempts left?YesSchedule the next attemptNoMark FAILED and alert owner
6 steps in order. Play them, or step through them one at a time.
4Executing With an Execution Id
Because we promise at-least-once, a target might receive the same run twice, for example when our call timed out after the target had already done the work. So every call carries the execution id. For HTTP targets it goes in a header; for Kafka targets it goes in the message. A well-built target stores processed ids and ignores repeats, which makes the job idempotent end to end.
Notice that retries of the same run reuse the same execution id, while the next scheduled fire gets a new one. That is exactly the distinction a target needs: same id means a duplicate, new id means new work. We also set a timeout per job, such as 30 seconds, so a hanging target cannot hold a worker forever.
5Retries, Backoff and Alerting
When a call fails with a network error, a timeout or a 5xx response, the run is marked RETRYING with a later lease time, using exponential backoff: 30 seconds, then 1, 2, 4 and 8 minutes, with a little random jitter so many failed jobs do not retry at the same instant. A 4xx response, such as 400 Bad Request, usually means the job is wrong rather than the target being down, so we fail it immediately rather than retrying it five times.
After max_attempts the run is marked FAILED and we alert the owning team through their configured channel. Every attempt updates the run row with start, end, attempt number and result, so the history page can show, for example, that last night's export took four attempts and finished at 02:09.
6Computing the Next Fire Time With CronExpression
For recurring jobs, we do not write our own cron parser. Spring's CronExpression parses a six-field expression, seconds first, and computes the next matching time after a given moment. Using a ZonedDateTime with the team's zone makes daylight saving changes behave as people expect.
We also need a misfire policy for when the scheduler was down. If the service was unavailable from 01:00 to 05:00, should the 02:00 job run once at 05:00, or be skipped? Each job declares it; the default is to run once, not once per missed fire.
7Avoiding a Thundering Herd at Midnight
Humans love round numbers, so thousands of jobs end up scheduled at 00:00. All of them become due in the same second, workers claim them in a burst, and targets, often the same few internal services, receive thousands of calls at once. That is a thundering herd.
We soften it in layers. Jobs may declare a tolerance window, such as run any time between 00:00 and 00:10, and the scheduler adds a stable random offset within it. Workers claim in limited batches and execution is rate-limited per target, so a burst becomes a steady flow. And the API shows teams how crowded a time slot is when they choose it, nudging them towards 00:07 instead of 00:00.
8Follow-up questions
How do you avoid a thundering herd at midnight when thousands of jobs fire?
Spread the work instead of firing everything at once. Jobs that tolerate it get a deterministic jitter inside a declared window, for example an offset from a hash of the job id within ten minutes. Claiming happens in bounded batches, executions go through per-target concurrency limits, and target teams can set their own maximum call rate. The few jobs that truly must run at exactly 00:00 can, and the rest flow out over the next minutes.
Why not use Quartz in clustered mode?
Quartz clustering works and uses database locks in a similar way, and it is a reasonable choice inside one application. For a shared service for many teams we want our own API, ownership, history, per-team limits and HTTP or message targets, so a simple custom design on the same principles is often easier to operate and evolve.
What if a target is down for an hour?
Its runs exhaust their retries and fail with alerts to the owner, unless the job's retry policy allows a longer window. A circuit breaker per target also stops us hammering it, and pauses new runs for that target until it recovers.
9Conclusion
In this answer, we designed a scheduler around two tables, with an index on next_fire_time. Worker nodes claim due jobs with SKIP LOCKED and advance next_fire_time in the same transaction, call targets with an execution id so duplicates can be ignored, retry with backoff before failing and alerting the owner, record every run, compute recurring schedules with Spring's CronExpression in the job's zone, and spread midnight bursts with jitter and per-target limits.
Open with
I would store job definitions in PostgreSQL with a next_fire_time column, indexed, plus a separate job_runs table with one row per execution. Worker nodes poll every second and claim due definitions with SELECT FOR UPDATE SKIP LOCKED; in the same transaction they insert a run row with a lease and advance next_fire_time, using Spring's CronExpression for recurring jobs in the job's time zone.
Then, if they want more
That makes each fire claimed exactly once, and a sweeper reclaims runs whose lease expired if a node dies. The promise is at-least-once execution, because after a timeout we cannot know whether the target did the work, so every call carries an execution id, as a header or message field, and retries reuse it, which lets targets deduplicate.
Failures retry with exponential backoff and jitter; 4xx errors fail fast; after the maximum attempts the run is marked failed and the owning team is alerted. Every attempt is recorded with start, end, attempt and result for the history page.
For the midnight problem, I would let jobs declare a tolerance window and add a stable jitter, claim in bounded batches, and rate-limit executions per target.
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 official documentation for the version you use. 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 "Design a distributed job scheduler service in Java that lets internal teams schedule...": https://scrivio-interview-library.netlify.app/architecture/backend-system-design/c14497e14b/