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).

MethodReturns
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

3. Task queue design decisions to make first

  1. 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?
  2. 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?
  3. What does one atomic dequeue do? It must take the top task and mark it PROCESSING, so no second worker can get it.
  4. Where does each task's data live? Payload, status, queue name, priority and retry count, keyed by task id.
  5. When exactly does a task go to the DLQ? With maxRetries = 2, count how many retry calls re-enqueue the task and which one moves it.
  6. 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: zrange then removing the task in a second call lets two workers take the same task. The pop must be one atomic step.

⚠️ Trap: retry on a task that exceeded its limit moves it to the DLQ and returns false: 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

Further reading

After you've tried it

Submit first. The full walkthrough of one solution: Building a Priority Task Queue with Dead-Letter Support.

Open the problem