LLD Guide · Module 2: Problem prep · Lesson 02.06
Prep: Priority Task Queue
Highest priority first, FIFO among equals, exactly one consumer per task, and a dead-letter queue for the ones that keep failing.
By Cruscible Engineering · Published
A queue is easy until two workers ask for the next task at the same instant. Then "the next task" has to mean exactly one task, for exactly one of them.
This lesson prepares you for Build a Priority Task Queue. It doesn't solve it.
1. What the task queue contract asks for
Named queues of tasks in Redis. Each task has a payload and a priority. Workers take the highest priority first, then report success or failure. Failed tasks can be retried a limited number of times; after that they go to the queue's dead-letter queue (DLQ).
| Method | Returns |
|---|---|
enqueue(queue, payload, priority) | a globally unique task id |
dequeue(queue) | the highest-priority PENDING task's id (it becomes PROCESSING), or null if none |
getPayload(taskId) | the original payload in any status, or null if unknown |
acknowledge(taskId) | true if it was PROCESSING (now COMPLETED), else false |
fail(taskId, error) | true if it was PROCESSING (now FAILED), else false |
retry(taskId) | true if re-enqueued; false if it wasn't FAILED or it went to the DLQ |
getStatus(taskId) | PENDING, PROCESSING, COMPLETED, FAILED, DEAD_LETTER, or null if unknown |
getDeadLetterQueue(queue) | DLQ task ids in the order they arrived; empty if none |
getQueueSize(queue) | the number of PENDING tasks (0 for an unknown queue) |
configureMaxRetries(queue, n) | the retry limit for that queue; 3 if never set |
2. Lessons to read before the task queue
- 01.03 Redis for LLD: sorted sets, hashes, and popping atomically with a script.
- 01.05 State machines: every method here is a legal or illegal move for a task.
3. Task queue design decisions to make first
- What's the task lifecycle? Draw the states and which method moves a task between them. Which moves are illegal, and what does the method return then?
- How does a sorted set order equal priorities? The contract wants FIFO among equal priorities. What does Redis do with equal scores by default, and how do you make the earlier task come out first?
- What does one atomic
dequeuedo? It must take the top task and mark it PROCESSING, so no second worker can get it. - Where does each task's data live? Payload, status, queue name, priority and retry count, keyed by task id.
- When exactly does a task go to the DLQ? With
maxRetries = 2, count how manyretrycalls re-enqueue the task and which one moves it. - How do queues stay independent? Name your keys so "queue-A" and "queue-B" never share state.
4. Task queue pitfalls
⚠️ Trap: a sorted set breaks score ties by comparing the members, not by arrival time. Equal priorities come out in id order unless the score itself encodes arrival.
⚠️ Trap:
zrangethen removing the task in a second call lets two workers take the same task. The pop must be one atomic step.
⚠️ Trap:
retryon a task that exceeded its limit moves it to the DLQ and returnsfalse: it wasn't re-enqueued.
⚠️ Trap: priorities may be negative. A negative priority is still a valid, lower priority.
⚠️ Trap: no in-process state. Task data in a Java map won't pass "all queue state must be persisted".
Before you submit
- Task ids are globally unique
- Tasks come out highest priority first, and FIFO among equal priorities
- Two concurrent
dequeuecalls never return the same task dequeueon an empty queue returnsnullacknowledgeandfailonly succeed from PROCESSING, and returnfalseotherwiseretryonly succeeds from FAILED, keeps the original payload and priority, and counts retries- A task past its retry limit moves to the DLQ, its status is
DEAD_LETTER, andretryreturnsfalse - The DLQ keeps tasks in arrival order
getPayloadworks in every status; unknown ids givenullgetQueueSizecounts only PENDING tasks- An unconfigured queue allows 3 retries
- Queues never affect each other, and all state is in Redis
Further reading
After you've tried it
Submit first. The full walkthrough of one solution: Building a Priority Task Queue with Dead-Letter Support.