Design a distributed job scheduler (run tasks at a given time or on a cron).
Answer
- API to create jobs: schedule, payload, retry policy.
- Store jobs in a database indexed by
next_run_at. - Scheduler instances find due jobs and claim them atomically (
SELECT ... FOR UPDATE SKIP LOCKED or leases), then push them to a queue. - Workers run them, report status, retry with backoff, and send to a dead-letter queue after N failures.
- Execution is at-least-once, so jobs must be idempotent.
- Partitioning or leader election to scale schedulers; heartbeats to recover jobs from dead workers; per-job monitoring.
Practice this question in Repaso