All Challenges
10 PointsStaffDistributed Systems
00:00
Distributed Priority Job Scheduler with Lock Leasing & Dead-Letter Queue
Build a distributed priority task scheduler featuring min-heap priority queues, worker heartbeat lock leases, exponential backoff, and poison pill DLQ routing.
### Problem Overview
Enterprise async job pipelines (Temporal, BullMQ, Celery, SQS) orchestrate millions of concurrent background tasks across distributed worker nodes.
Build a **Priority Job Scheduler** in TypeScript supporting priority ordering, worker lock leases with heartbeat renewals, automatic failure backoff, and dead-letter queue (DLQ) routing.
### Functional Requirements
1. **Priority Scheduling**:
- Enqueue jobs with numeric priority (`priority: number`), delay (`delayMs?: number`), and max retries (`maxRetries: number`).
2. **Worker Lock Leasing (At-Least-Once Execution)**:
- When a worker leases a job (`leaseJob(workerId: string, leaseDurationMs: number)`), the job becomes invisible to other workers.
- If the worker crashes or fails to renew its lease before expiry, the scheduler makes the job available again.
3. **Heartbeat Lease Renewal**:
- `renewLease(jobId: string, workerId: string, extensionMs: number): boolean`.
4. **Exponential Retries & Dead Letter Queue (DLQ)**:
- If a job fails or times out, retry up to `maxRetries` with exponential backoff.
- When retries are exhausted (poison pill), move job to the Dead-Letter Queue with error logs.
5. **Job Metrics & Observability**:
- `getStats(): { pending: number; active: number; completed: number; failed: number; dlq: number }`.
### Non-Functional Requirements
- **High Concurrency**: Queue extraction must be (O(\log N)).
- **Fault Tolerance**: Prevent zombie jobs from locking the scheduler indefinitely.
AI Review Rubric Dimensions (1–10 Points):
- Priority scheduling precision
- Lock lease expiration and renewal
- Exponential retry backoff
- Poison pill isolation to DLQ
solution.tsTYPESCRIPT
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
51 lines•1212 characters•
AI Review Ready