← Lessons

quiz vs the machine

Gold1370

System Design

Priority Queues for Work

Serve urgent jobs ahead of bulk jobs by ordering a work queue, while guarding against starvation.

5 min read · core · beat Gold to climb

Why prioritize work

A single first in first out queue treats a payment confirmation the same as a nightly report. A priority queue lets high value jobs jump ahead so latency sensitive work is not stuck behind bulk work.

How it is built

  • Multiple queues per priority class, with workers draining higher classes first.
  • A scored queue where each job carries a priority value and the highest is served next.

The starvation trap

If high priority work never stops arriving, low priority jobs may wait forever.

  • Aging raises a job priority the longer it waits, guaranteeing it eventually runs.
  • Reserved capacity dedicates a slice of workers to lower classes so they always make progress.

Practical notes

  • Keep classes few: two or three priorities are easier to reason about than ten.
  • Watch fairness: monitor the wait time of the lowest class to catch starvation early.

Key idea

Priority queues let urgent work pass bulk work, but they need aging or reserved capacity so that low priority jobs do not starve while high priority work keeps flowing.

Check yourself

Answer to earn rating on the learn ladder.

1. What problem do priority queues solve?

2. How do you prevent low priority jobs from starving?