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
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:
fafjobs commit to shardedJobs PG(uuid7/ Snowflake keys) and stream via CDC into RabbitMQ, so a pod crash never strands aPendingrow. - Lock-free cron polling: Stateless
Cron WatchersrunSELECT ... WHERE next_run_time <= NOW() FOR UPDATE SKIP LOCKEDand insert intoCronJobswith a unique(parent_cron_id, scheduled_epoch)constraint for idempotency.
2. Competing consumers & Kubernetes execution
- Delegated compute placement:
RabbitMQdistributes jobs toJob Launchers, which mount user code from S3 (~20 KBreused, 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 Watcherupdates Postgres withWHERE job_id = :id AND version = :vand 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/:idmarksCANCELLEDin 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, soOld Job Sweeperarchives 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?
Select an option to check the trade-off.
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?
Select an option to check the trade-off.
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?
Select an option to check the trade-off.
outbox patternmessaging
Writing a domain change and its outgoing event to the same DB transaction, then relaying the event asynchronously to avoid dual writes.
idempotencyreliability
Property where repeating the exact same request multiple times produces the same final state as executing it once.