Careeroza
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 APIBullMQ 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 } });| Library | Backend |
|---|---|
| BullMQ | Redis |
| Agenda | MongoDB |
| RabbitMQ | AMQP message broker |
| AWS SQS / Google Pub/Sub | managed cloud queues |