← Lessons

quiz vs the machine

Gold1450

System Design

Priority Queues Design

Serving urgent messages ahead of routine ones without starving the rest.

5 min read · core · beat Gold to climb

Why priority

Not all messages are equal. A password reset should jump ahead of a nightly report. A priority queue lets higher priority messages be served before lower ones, even if they arrived later.

Common implementations

  • Multiple queues by level: one queue per priority, consumers drain high before low.
  • Score ordering: a single store ordered by a priority score.
  • Weighted fair draw: pull from levels in a weighted ratio so low priority still progresses.

The starvation risk

If consumers always drain high priority first, a steady stream of urgent messages can starve low priority ones forever. Weighted fair scheduling or aging, where waiting raises a message priority over time, prevents permanent starvation.

Trade off

Strict priority gives the lowest latency for urgent work but risks starvation. Weighted fairness sacrifices a little urgent latency to guarantee progress everywhere.

Flow

Key idea

Priority queues serve urgent messages first, but need weighted scheduling or aging so low priority work is never starved.

Check yourself

Answer to earn rating on the learn ladder.

1. What is the main danger of strict highest first priority scheduling?

2. How does aging help a priority queue?