What Is a Priority Queue Service?
A Priority Queue Service extends a standard job queue by processing tasks in priority order rather than strict FIFO. High-priority work — a payment confirmation, an SLA-critical alert, a VIP user request — jumps ahead of lower-priority background tasks. This requires careful design to prevent starvation, ensure fairness, and scale without turning into a bottleneck.
Data Model
Priority is typically an integer where lower numbers mean higher urgency (like Unix nice values), or an explicit ENUM tier.
CREATE TABLE priority_jobs (
id BIGINT PRIMARY KEY AUTO_INCREMENT,
queue_name VARCHAR(128) NOT NULL,
priority TINYINT NOT NULL DEFAULT 50, -- 0=critical, 99=low
payload JSON NOT NULL,
status ENUM('pending', 'in_flight', 'done', 'failed') DEFAULT 'pending',
attempts INT DEFAULT 0,
max_attempts INT DEFAULT 3,
run_at DATETIME NOT NULL DEFAULT NOW(),
enqueued_at DATETIME DEFAULT NOW(),
locked_until DATETIME,
locked_by VARCHAR(128),
INDEX idx_pq_dequeue (queue_name, status, priority ASC, run_at ASC)
);
The composite index on (queue_name, status, priority, run_at) drives efficient ordered dequeue scans.
Core Algorithm
Enqueue: Insert with the appropriate priority integer. Producers must not all use priority 0 or the system degenerates into a normal queue with extra overhead.
Dequeue (priority-ordered lease):
UPDATE priority_jobs
SET status = 'in_flight',
locked_by = :worker_id,
locked_until = NOW() + INTERVAL 60 SECOND,
attempts = attempts + 1
WHERE queue_name = :queue
AND status = 'pending'
AND run_at <= NOW()
ORDER BY priority ASC, run_at ASC
LIMIT 1;
Workers always pull the lowest priority number first. Ties are broken by run_at (FIFO within a tier).
Starvation Prevention
Pure priority ordering starves low-priority tasks when high-priority work never stops. Two common mitigations:
- Aging: Periodically bump the effective priority of tasks that have waited beyond a threshold. Implement with a computed column or a background job that decrements
priorityfor oldpendingrows. - Weighted fair queuing: Allocate worker slots proportionally — e.g., 60% of workers reserved for priority < 20, 30% for priority 20-60, 10% for the rest.
Failure Handling
At-Least-Once Delivery
Same lease-timeout sweeper as a plain job queue: a background process resets in_flight rows whose locked_until has expired back to pending, preserving the original priority.
Idempotency
Include an idempotency_key in the payload. High-priority tasks are especially likely to be retried aggressively by impatient callers, so the handler must check for duplicate execution (e.g., query a results table keyed on idempotency_key before doing work).
Poison Messages
Failed tasks that exhaust max_attempts are moved to status = failed. Maintain a separate DLQ table or add a dlq_priority column so operators can re-enqueue at the original priority once the underlying bug is fixed.
Scalability Considerations
- Separate physical queues per tier — instead of one table ordered by priority, use separate queues (or Kafka topics/SQS queues) per tier and dedicate worker pools to each. Simpler ordering, easier scaling.
- In-memory heap for hot path — keep the top-N pending tasks in a Redis sorted set (score = priority * 1e12 + unix_timestamp) for sub-millisecond dequeue. Persist to SQL for durability and recovery.
- Rate limiting per priority — cap how fast critical tasks can consume shared downstream resources (DB connections, external APIs) to avoid cascading failures.
- Observability — track per-priority queue depth, wait-time p50/p99, and starvation events. Alert when low-priority wait time exceeds SLO.
Summary
A Priority Queue Service ensures critical work gets processed first without abandoning lower-priority tasks. The key design decisions are: how to represent priority (integer vs. enum vs. separate queues), how to prevent starvation (aging or weighted workers), and whether to use a database-backed or in-memory heap for the hot path. In interviews, articulate the tradeoff between simplicity (one ordered table) and performance (Redis sorted set with SQL durability) and explain how aging prevents starvation in a purely numeric scheme.
See also: Scale AI Interview Guide 2026: Data Infrastructure, RLHF Pipelines, and ML Engineering
See also: Atlassian Interview Guide