Search code examples
node.jsbackground-processkue

What does it take to run background tasks in node?


If what i understand is correct, processing background tasks is a good way to free the main thread of cpu bound tasks.

What i don't get is what is used by systems like bull or kue to run the tasks out of the main thread.

Do they use threads ? Do they require an entire node process fork? Do they spawn child processes?


Solution

  • Bull is based on Redis which handles in it's own process the handling and Queuing of data for these jobs. It is a lightweight, robust and fast job processing queue. It uses redis for persistence, so the queue is not lost if the server goes down for any reason.

    the internal implementation of the Job can be seen here

    The same is with Kue module which is a priority job queue backed by redis processes, built for node.js. The background tasks are powered by Redis.

    This means that these module depends on Redis external process that enables different creating background jobs.

    Job-specific events are fired on the Job instances via Redis pubsub:

    • enqueue the job is now queued
    • promotion the job is promoted from delayed state to queued
    • progress the job's progress ranging from 0-100
    • failed attempt the job has failed, but has remaining attempts yet
    • failed the job has failed and has no remaining attempts
    • complete the job has completed
    • remove the job has been removed

    The delayed Jobs are powered by Redis Queue which notifies/triggers back the callbacks in the module.