Reading — step 1 of 5
Read
~1 min readJob Queue & Execution
Min-Heap Priority Queue Ordered by next_run
A scheduler needs to know which job fires next at any moment. Naive: scan all jobs, find min next_run. O(n) per tick.
Better: a min-heap keyed by next_run. The root is always the next job to fire. Insertion + removal are O(log n).
heap[0] = (2025-03-17T03:00:00Z, "backup-db")
heap[1] = (2025-03-17T05:00:00Z, "log-rotate")
heap[2] = (2025-03-17T09:15:00Z, "fetch-feeds")
Sleep until heap[0], fire it, recompute its next_run, push back.
Python: heapq. JS: write your own. Go: container/heap.
For deletion (job removed), keep a tombstones set and skip when popping; or
use a more advanced structure (indexed heap).
Discussion
Ask a question, share an insight, or help someone who’s stuck.
Sign in to post a comment or reply.
Loading…