typescriptintermediate
Bull Queue Job Producer & Consumer
Create a job queue with Bull for background processing with retries and concurrency control.
typescriptPress ⌘/Ctrl + Shift + C to copy
import Queue from 'bull';
interface EmailJob {
to: string;
subject: string;
body: string;
}
const emailQueue = new Queue<EmailJob>('email', {
redis: { host: '127.0.0.1', port: 6379 },
defaultJobOptions: {
attempts: 3,
backoff: { type: 'exponential', delay: 2000 },
removeOnComplete: 100,
},
});
// Producer
export async function enqueueEmail(data: EmailJob) {
return emailQueue.add(data, { priority: 1 });
}
// Consumer
emailQueue.process(5, async (job) => {
console.log(`Sending email to ${job.data.to}`);
// await sendEmail(job.data);
return { sent: true };
});Use Cases
- Email sending queue
- Image processing pipeline
Tags
Related Snippets
Similar patterns you can reuse in the same workflow.
typescriptadvanced
Redis Cache Get/Set Helper
Type-safe Redis cache wrapper with automatic JSON serialization, TTL support, and cache-aside pattern.
Best for: Database query caching
#redis#cache
typescriptbeginner
Node.js Cron Job Scheduler
Schedule recurring tasks with node-cron using crontab syntax and timezone support.
Best for: Database cleanup tasks
#cron#scheduler
typescriptadvanced
Node.js In-Memory Task Queue
A simple in-memory task queue with concurrency control, retries, and priority support.
Best for: Rate-limited API call processing
#nodejs#queue
typescriptadvanced
Promise Queue with Concurrency Limit
A queue that processes async tasks with a configurable concurrency limit to prevent resource exhaustion.
Best for: Rate-limited API calls
#async#concurrency