Full answer
Requirements
Functional:
- Create one-off jobs ("run at 2026-12-05 09:00 UTC") and recurring jobs (cron expressions).
- Run each job on time, retry failures with backoff, and record run history.
- Pause, update and delete jobs.
Non-functional:
- Jobs start within a few seconds of their time.
- A scheduler crash does not skip jobs; duplicates are possible but rare and detectable.
- Scale to 100M runs per day.
Capacity estimates
Assumptions: 100M runs per day, 1M jobs scheduled for the start of the same minute at peak, 1 KB per job definition, 50M job definitions.
- Average: 100M / 86,400 = about 1,160 runs per second.
- Peak minute: if 1M jobs fire in one minute and are spread across it, that is about 16,700 per second; if they all fire in the first second, it is 1M per second, which is why spreading matters.
- Definitions: 50M x 1 KB = 50 GB, which a sharded relational or key-value store holds easily.
API
POST /v1/jobs {"name": "daily-report", "schedule": "0 9 * * *", "timezone": "Asia/Kolkata",
"target": {"type": "http", "url": "https://..."}, "retry": {"max": 5}}
GET /v1/jobs/{id}/runs?cursor=..
PATCH /v1/jobs/{id} {"paused": true}
DELETE /v1/jobs/{id}
Data model
jobs: job_id, owner, schedule, timezone, target, retry_policy, next_run_at, paused, version.due_index: (bucket_minute, shard, job_id), where bucket_minute is next_run_at rounded down. Partitioned so each scheduler owns some shards.runs: run_id = hash(job_id, scheduled_time), job_id, status, attempt, started_at, finished_at, error.
High-level design
flowchart LR
U[Clients] --> API[Job API]
API --> DB[(Jobs and due index)]
SCH[Scheduler workers with leases] --> DB
SCH --> Q[(Run queue)]
Q --> EX[Executor workers]
EX --> T[Targets: HTTP, functions, scripts]
EX --> RUNS[(Run history)]
CO[Coordination service] --> SCHHow a job fires
- Each scheduler worker holds leases (in etcd or ZooKeeper) on a set of shards of the due index.
- Every second it reads jobs in the current and previous minute bucket for its shards whose next_run_at has passed.
- For each job, it inserts a run with
run_id = hash(job_id, scheduled_time)(unique, so a second scheduler doing the same work fails the insert), enqueues the run, and moves next_run_at to the next cron time in one transaction. - Executors take runs from the queue, call the target, and record results. Failures go back to the queue with exponential backoff until the retry limit.
If a scheduler dies, its leases expire and another worker takes over its shards; it rescans recent buckets, so missed jobs fire late but are not skipped.
Bottlenecks and trade-offs
- Top-of-minute and top-of-hour spikes. Add a stable jitter per job (for example hash(job_id) mod 60 seconds) for jobs that allow it, and pre-load the next minute's bucket into memory.
- At-least-once. A run can execute twice if an executor crashes after calling the target. Pass run_id to targets so they can deduplicate.
- Time zones and daylight saving time. Store the time zone with the job and compute next times in it; decide whether a skipped or repeated local hour runs zero, one or two times, and document it.
- Long jobs need heartbeats so the system can tell a slow job from a dead executor.
- Polling versus timers. Polling minute buckets is simple and durable; in-memory timer wheels are faster but must be rebuilt after a crash.
By level
Fresher: Explain that jobs are stored with their next run time and workers pick up the ones that are due. A fresher should know that failed jobs need retries.
Mid: Design the bucketed index, lease-based ownership, deterministic run IDs and a queue for executors. Do the peak-minute math and add jitter.
Senior: Cover failover gaps, time zones, heartbeats for long jobs, tenant limits, dependencies and polling versus timer wheels. Expect clear at-least-once reasoning.
Key points
- Index next run times in minute buckets, partitioned across schedulers
- Leases assign shards and move them on failure
- Run ID from job ID and scheduled time makes duplicates detectable
- At-least-once delivery; targets deduplicate by run ID
- Jitter spreads top-of-minute spikes
Common mistakes
- One scheduler process with no failover
- Scanning the whole jobs table every second
- No idempotency, so failover causes duplicate side effects
- Ignoring time zones and daylight saving changes
Follow-ups
- How would you support job dependencies, such as B runs after A?
- How would you cap concurrent runs per tenant?
- How do you run a job exactly at 09:00:00.000?
Tips
- Separate scheduling from execution
- Explain the deterministic run ID
- Discuss the top-of-hour spike with numbers
Evaluation rubric
- Durable job storage and due index
- Failover with leases
- Idempotent runs and retry policy
- Capacity math and spike handling