skip to content
jaeaeichsysdes

Design a Distributed Job Scheduler

End-to-end architecture for 50K job triggers/sec: sharded Postgres with CDC outbox, RabbitMQ work queues, Kubernetes execution nodes, optimistic-locked retries, and Glacier archival.

The whiteboard

Trace submit & cron, execute & retry, and Glacier archival below — composing Idempotency & Outbox, Queues vs Event Logs, and Concurrency & Failure Modes.

whiteboard · press 1–4 to trace flows

INGEST & SCHEDULE (50K RPS)EXECUTION & COMPUTESTATUS, DLQ & ARCHIVALfafcronpollinsert runCDCconsumecodecreate Jobpod eventsmaxedCAS statussweep >30darchiveSubmit APIPOST /jobfaf or cronJobs PG5–10 shardsPK: job_idCron PGPK: cron_idnext_run_timeCron WatcherSKIP LOCKEDunique epochRabbitMQwork queueCDC outboxJob Launcherpulls S3 codespawns K8s JobS3 Artifactscode scripts~20 KB reusedK8s Clusterisolated podsautoscales computeOld Job Sweeperexpires old rows~500 GB/dayS3 Glaciercold historybounds PG sizeStatus Watcheroptimistic versionretry < max_retriesDLQpoison / maxedno requeue

Full job scheduler: sharded Postgres commits jobs first, CDC streams them to RabbitMQ, K8s executes them, and a sweeper archives old rows to Glacier.No dual writes between Postgres and RabbitMQ, and compute placement is cleanly delegated to Kubernetes.

Scale & APIs

50K RPS

job triggers · 5–10 PG shards

1–3K RPS

status reads

~500 GB/day

PG metadata · Glacier sweep

Tables:
Cron (PK: cron_id) { status, next_run_time, interval, max_retries, s3_path }
CronJobs (UNIQUE: parent_cron_id, scheduled_epoch) { job_id }
Jobs (PK: job_id) { status, retries, max_retries, version, s3_path }
POST /job { code_path, cmd, args, type: "faf"|"cron", cron_expression?, max_retries } -> { id }
GET /job/:id -> { status: "Pending" | "Running" | "Succeeded" | "Failed" | "Cancelled" }
DELETE /job/:id -> 200 OK (cancel pending or kill running K8s Job)

How it works

1. Separating immediate (faf) from recurring (cron) jobs

  • CDC outbox pattern: faf jobs commit to sharded Jobs PG (uuid7 / Snowflake keys) and stream via CDC into RabbitMQ, so a pod crash never strands a Pending row.
  • Lock-free cron polling: Stateless Cron Watchers run SELECT ... WHERE next_run_time <= NOW() FOR UPDATE SKIP LOCKED and insert into CronJobs with a unique (parent_cron_id, scheduled_epoch) constraint for idempotency.

2. Competing consumers & Kubernetes execution

  • Delegated compute placement: RabbitMQ distributes jobs to Job Launchers, which mount user code from S3 (~20 KB reused, never inline in PG) and submit a Kubernetes Job.
  • Poison isolation: Malformed specs go straight to the DLQ instead of crash-looping healthy workers.

3. Optimistic-locked status updates & cancellation

  • Monotonic state guard: Status Watcher updates Postgres with WHERE job_id = :id AND version = :v and blocks overwrites of terminal states (Succeeded, Cancelled) so out-of-order K8s events are safely dropped.
  • Bounded retries + cancel: Failed runs re-enqueue with exponential backoff while retries < max_retries, then stop at the DLQ. DELETE /job/:id marks CANCELLED in PG (skipped by Launcher if pending; terminates K8s Job if running).

4. Bounding Postgres with cold archival

  • Storage math: 100 B/row × 50K/s ≈ 500 GB/day (~180 TB/yr) outgrows a 64 TB cloud DB in ~4 months, so Old Job Sweeper archives completed rows older than 30 days to S3 Glacier.

lesson

Interview differentiator: Combine FOR UPDATE SKIP LOCKED (horizontal cron watcher scaling without contention) with a unique (parent_cron_id, scheduled_epoch) key (crash-safe deduplication) — that pair solves the hardest distributed cron question in two lines.

Check yourself

interactive · stress-test this scheduler against classic interview probes

01 · Preventing duplicate cron triggers

10 cron watchers · 50K triggers/s · no double enqueue

You scale the Cron Watcher to 10 stateless replicas so 50K triggers/sec don't bottleneck on 1 poller. How do you stop two watchers from picking up the exact same due cron row at 12:00:00?

02 · Why CDC instead of Submit → RabbitMQ directly?

Submit service · PG + RabbitMQ · pod crash

Why does Submit write to Jobs PG and rely on CDC to publish into RabbitMQ, instead of writing to Postgres and calling rabbit.publish() in the same HTTP handler?

03 · Out-of-order Kubernetes status events

async K8s watch · event reordering · status table

A job finishes in 200ms. Due to watcher lag, the K8s 'Running' event arrives at the Status Watcher AFTER 'Succeeded' was already written to Postgres. What stops it from reverting the job back to Running?

esc
⌘B sidebar[ ] prev/next1–5 flowsc copyt theme