system-design · intermediate

Design a Job Scheduler — Delayed and Recurring Work

Start here

A job scheduler runs work later:

Unlike a simple queue of immediate work, schedulers must efficiently find **what becomes due now** among millions of future jobs, execute with workers, retry failures, and avoid double runs where possible.

What you will learn

  1. Model one-off vs recurring jobs.
  2. Store jobs with run_at indexes.
  3. Dispatch due jobs to workers safely.
  4. Use leases/visibility timeouts.
  5. Handle cron calendars and catch-up policies.
  6. Discuss sharding by time and tenant.

Words you need before we begin

TermPlain English
Due jobJob whose run_at ≤ now.
WorkerProcess that executes job payload.
Lease / visibility timeoutTemporary ownership so others do not run the same job.
Idempotent jobSafe if executed more than once.
Cron expressionSchedule description for recurring jobs.
Missed run policySkip vs catch up after downtime.
Dead-letterQuarantine for permanently failing jobs.

Requirements

Functional

Non-functional

High-level design

flowchart LR
  API[Scheduler API] --> DB[(Jobs store)]
  Disp[Dispatcher] --> DB
  Disp --> Q[Ready queue]
  Q --> W[Workers]
  W --> DB

Step-by-step design

Step 1 — Job record

job_id, tenant, payload, run_at, status, attempts, recurrence, idempotency_key.

Step 2 — Index for due scans

Index (status, run_at) or partition by time buckets. Avoid full table scans.

Step 3 — Dispatcher

Periodically (or via listen/notify) select due jobs, mark LEASED with owner + expiry, push to ready queue. Use UPDATE … WHERE status=READY AND run_at<=now LIMIT n RETURNING patterns or equivalent.

Step 4 — Workers

Execute; on success mark DONE; on failure retry with backoff or DLQ. Heartbeat lease for long jobs.

Step 5 — Recurring

On success, compute next run_at from cron; insert/update next occurrence. Do not enqueue infinite future rows—materialize next only.

Step 6 — Shard

Shard by tenant_id or hash(job_id) so dispatchers do not fight one hot table. Per-shard dispatcher with leader election optional.

Step 7 — Exactly-once talk

Be honest: at-least-once + idempotent handlers. Leases reduce doubles but clocks and crashes still need app idempotency.

Failure modes

ModeImpactMitigation
Dispatcher lagLate jobsScale dispatch; partition
Worker death mid-jobRe-run after leaseIdempotent effects
Thundering herd at :00SpikeJitter schedules
Poison payloadInfinite failMax attempts + DLQ
Clock skewEarly/late runsNTP; trust DB time

Trade-offs

ChoiceBenefitCost
DB as schedulerStrong durabilityScan/contention care
Specialized systems (e.g. queues with delay)FeaturesOps/learn
Separate queue after dueWorker scaleExtra move

Common mistakes

  1. SELECT * FROM jobs WHERE run_at < now every second unindexed.
  2. Claiming exactly-once without idempotency.
  3. Materializing 10 years of cron rows.
  4. Single global lock for all jobs.
  5. No tenant isolation.

Check your understanding

  1. What makes scheduling different from a plain queue?
  2. Why lease jobs?
  3. How to store cron efficiently?
  4. What is a missed-run policy?
  5. Why jitter :00 crons?

Practice

  1. Schema + indexes for 100M future jobs.
  2. Sequence for worker crash mid-execution.
  3. Design API for create/cancel.
  4. Compare Quartz-like vs Kafka-delay approaches conceptually.
  5. Mock interview.

Deeper production notes

Calendar edge cases

Time zones, DST, and “every 30th” calendars need explicit rules—mention complexity.

Observability

Track schedule lag (now - run_at), in-flight leases, DLQ depth.

Additional teaching scenarios

Scenario A — 10× peak

Which component saturates first? First mitigation?

Scenario B — dependency down 30 minutes

What still works? What degrades?

Scenario C — five-sentence wrap

Requirements, core mechanism, scale lever, failure mode, trade-off.

Revision summary

Glossary

TermDefinition
DispatcherComponent that moves due jobs to execution.
LeaseTemporary exclusive right to run a job.
RecurrenceRule generating future run times.

Abbreviations and terminology

What to learn next

  1. Message queues
  2. Leader election
  3. Idempotency

Extra teaching notes

When you apply this lesson, write the user-visible success metric first, then the failure mode you fear most. Design the smallest mechanism that protects that metric under partial failure. Prefer explicit timeouts, idempotency, and ownership over adding more infrastructure boxes.

In interviews or design reviews, narrate assumptions, request paths, and trade-offs out loud. A correct-enough design with clear failure handling beats a buzzword diagram without numbers. Revisit the worked example and restate it for a different domain to prove you own the ideas, not the template wording.

Extra teaching notes

When you apply this lesson, write the user-visible success metric first, then the failure mode you fear most. Design the smallest mechanism that protects that metric under partial failure. Prefer explicit timeouts, idempotency, and ownership over adding more infrastructure boxes.

In interviews or design reviews, narrate assumptions, request paths, and trade-offs out loud. A correct-enough design with clear failure handling beats a buzzword diagram without numbers. Revisit the worked example and restate it for a different domain to prove you own the ideas, not the template wording.

Extra teaching notes

When you apply this lesson, write the user-visible success metric first, then the failure mode you fear most. Design the smallest mechanism that protects that metric under partial failure. Prefer explicit timeouts, idempotency, and ownership over adding more infrastructure boxes.

In interviews or design reviews, narrate assumptions, request paths, and trade-offs out loud. A correct-enough design with clear failure handling beats a buzzword diagram without numbers. Revisit the worked example and restate it for a different domain to prove you own the ideas, not the template wording.

Extra teaching notes

When you apply this lesson, write the user-visible success metric first, then the failure mode you fear most. Design the smallest mechanism that protects that metric under partial failure. Prefer explicit timeouts, idempotency, and ownership over adding more infrastructure boxes.

In interviews or design reviews, narrate assumptions, request paths, and trade-offs out loud. A correct-enough design with clear failure handling beats a buzzword diagram without numbers. Revisit the worked example and restate it for a different domain to prove you own the ideas, not the template wording.

Extra teaching notes

When you apply this lesson, write the user-visible success metric first, then the failure mode you fear most. Design the smallest mechanism that protects that metric under partial failure. Prefer explicit timeouts, idempotency, and ownership over adding more infrastructure boxes.

In interviews or design reviews, narrate assumptions, request paths, and trade-offs out loud. A correct-enough design with clear failure handling beats a buzzword diagram without numbers. Revisit the worked example and restate it for a different domain to prove you own the ideas, not the template wording.

Interview and production field guide for this topic

Use this section as deliberate practice, not filler. Rewrite the worked example for a second domain you know well—fintech, education, logistics, or media. Keep the same skeleton: requirements, estimates, high-level diagram, request path, data model, scale lever, failure modes, and trade-offs. If you cannot fill every section without copying buzzwords, you do not yet own the design.

Numbers to force yourself to state

Always speak order-of-magnitude figures: peak QPS, storage growth per day, fan-out factor, connection counts, or queue depth. Wrong numbers that are explicit beat silent hand-waving. Correct the numbers when the interviewer or teammate challenges them; that is collaboration, not failure.

Failure minute

Set a timer for sixty seconds and list only failures: timeouts, duplicates, hot keys, dependency outages, bad deploys, and data corruption paths. For each, name detection and first mitigation. Designs that only describe the happy path are incomplete for production and weak in interviews.

Ownership and operability

Name the dashboard, the alert, the runbook section, and the team that pages. If any are blank, the system will train you during an incident. Prefer progressive delivery: canaries, flags, and rollback notes written before the change lands.

Consistency and retries

State whether the design assumes at-least-once delivery, whether handlers are idempotent, and where unique constraints live. Retries without idempotency are how double charges, double messages, and duplicate fan-out jobs appear. Timeouts without bounds are how thread pools die.

What good looks like in a review

A strong design review or interview answer clarifies scope, makes assumptions audible, draws a minimal path, deepens one or two bottlenecks, and closes with trade-offs and evolution. Use that bar on design-job-scheduler round 0 every time you revisit it.

Interview and production field guide for this topic

Use this section as deliberate practice, not filler. Rewrite the worked example for a second domain you know well—fintech, education, logistics, or media. Keep the same skeleton: requirements, estimates, high-level diagram, request path, data model, scale lever, failure modes, and trade-offs. If you cannot fill every section without copying buzzwords, you do not yet own the design.

Numbers to force yourself to state

Always speak order-of-magnitude figures: peak QPS, storage growth per day, fan-out factor, connection counts, or queue depth. Wrong numbers that are explicit beat silent hand-waving. Correct the numbers when the interviewer or teammate challenges them; that is collaboration, not failure.

Failure minute

Set a timer for sixty seconds and list only failures: timeouts, duplicates, hot keys, dependency outages, bad deploys, and data corruption paths. For each, name detection and first mitigation. Designs that only describe the happy path are incomplete for production and weak in interviews.

Ownership and operability

Name the dashboard, the alert, the runbook section, and the team that pages. If any are blank, the system will train you during an incident. Prefer progressive delivery: canaries, flags, and rollback notes written before the change lands.

Consistency and retries

State whether the design assumes at-least-once delivery, whether handlers are idempotent, and where unique constraints live. Retries without idempotency are how double charges, double messages, and duplicate fan-out jobs appear. Timeouts without bounds are how thread pools die.

What good looks like in a review

A strong design review or interview answer clarifies scope, makes assumptions audible, draws a minimal path, deepens one or two bottlenecks, and closes with trade-offs and evolution. Use that bar on design-job-scheduler round 1 every time you revisit it.

FAQ from first-time learners

Q: Is cron on one server enough?
A: For tiny systems yes; it is a SPOF and does not scale—say when to evolve.

Q: Database or Redis ZSET by score=run_at?
A: Both are interview-valid; discuss durability and blocking pops.

Track: Distributed Systems

Next: Design a Notification Service — Push, Email, SMS

By Shubham Jain

All articles · Study paths

Shubham Jain · Learning Lab