Course contentsShow
← Node.js
Lesson 1297 of 1,98011. Task Queues and Background ProcessingFree lesson

Implementing Task Priority Boosting

Increase the priority of a task automatically if it sits in the queue longer than a defined threshold.

In this lesson, you will learn how to implement "Task Priority Boosting," a mechanism that prevents "starvation" by dynamically increasing a task's priority as it lingers in your queue.

The Problem: Task Starvation

In a system with strict priority levels (e.g., High, Medium, Low), a constant stream of high-priority tasks can prevent low-priority tasks from ever executing. If a task stays in the queue indefinitely, it suffers from "starvation." Priority boosting solves this by checking the createdAt timestamp of a pending task and bumping its priority level once it crosses a defined "wait threshold."

The Implementation Strategy

To implement this, you shouldn't rely on the worker to handle the logic during execution. Instead, use a "Queue Maintainer"—a separate, low-frequency interval process that scans the queue for stale tasks.

Here is a simplified pattern for upgrading a task's priority in your database or state store:

const THRESHOLD = 60000; // 1 minute

async function boostStaleTasks(queue) {
  const now = Date.now();
  
  // Find tasks that are low priority but have waited too long
  const staleTasks = await queue.find({
    priority: 'LOW',
    createdAt: { $lt: now - THRESHOLD }
  });

  for (const task of staleTasks) {
    // Promote the task to MEDIUM priority
    await queue.update(task.id, { priority: 'MEDIUM' });
    console.log(`Task ${task.id} boosted due to age.`);
  }
}

// Run this periodically
setInterval(boostStaleTasks, 10000);

Why This Matters

By promoting tasks that have waited beyond their threshold, you ensure fairness without completely abandoning your priority-based architecture. This allows your system to handle bursts of high-priority work while guaranteeing that background tasks—like data cleanup or reporting—eventually move to the front of the line once they become "old enough."

When designing this, keep your threshold values generous. If you boost priorities too aggressively, you risk a "priority flood," where everything in the queue eventually becomes high-priority, effectively nullifying the benefits of having a tiered system in the first place.

Key Takeaway: Priority boosting mitigates starvation by dynamically upgrading task rank based on wait time, ensuring that even low-priority work eventually reaches completion.