Background Jobs and Queues (Node.js)

Server-side Job Queues

On the backend the same idea is used at a bigger scale: an API should not make the user wait for slow work (sending emails, generating reports, processing videos). It puts a job in a queue and answers immediately; separate worker processes do the work.

Without a queue: Client → API → slow task (10 s) → response
With a queue:    Client → API → add job → immediate response
                                   ↓
                         Redis queue → Worker → email / DB / external API

BullMQ Example

// queue.js
const { Queue } = require('bullmq');
const emailQueue = new Queue('email', { connection: { host: '127.0.0.1', port: 6379 } });

// in an Express route: respond at once, work happens later
app.post('/signup', async (req, res) => {
  const user = await User.create(req.body);
  await emailQueue.add('welcome', { to: user.email }, { attempts: 3 });
  res.status(201).json(user);
});

// worker.js (separate process)
const { Worker } = require('bullmq');
new Worker('email', async (job) => {
  await sendEmail(job.data.to, 'Welcome!');
}, { connection: { host: '127.0.0.1', port: 6379 } });
LibraryBackend
BullMQRedis
AgendaMongoDB
RabbitMQAMQP message broker
AWS SQS / Google Pub/Submanaged cloud queues