💻 Dev

🛠️ 처음부터 만드는 백그라운드 잡 시스템 — BullMQ + Redis + TypeScript

이메일 발송, 이미지 처리, PDF 생성...
API 핸들러에서 바로 하면 응답이 느려집니다. 백그라운드 잡으로 분리하세요.
```bash
npm install bullmq ioredis
```

Queue + Worker 정의


```typescript
import { Queue, Worker } from 'bullmq';
const connection = { host: 'localhost', port: 6379 };
// Queue 생성
const emailQueue = new Queue('email', { connection });
// Worker — 실제 처리 로직
new Worker('email', async (job) => {
const { to, subject, body } = job.data;
await sendEmail(to, subject, body);
}, { connection, concurrency: 5 });
```

API에서 잡 추가


```typescript
app.post('/signup', async (c) => {
const user = await createUser(c.req.body);
await emailQueue.add('welcome', {
to: user.email,
subject: '가입을 환영합니다!',
body: welcomeTemplate(user.name),
}, {
attempts: 3,
backoff: { type: 'exponential', delay: 1000 },
});
return c.json({ ok: true }); // 즉시 응답!
});
```

이것만 기억하세요


  • `attempts` + `backoff` → 실패 시 자동 재시도

  • `concurrency: 5` → 동시 처리 수 제한으로 서버 보호

  • `delay: 60000` → 1분 후 지연 실행

  • `repeat: { cron: '0 9 * * *' }` → 매일 9시 크론 잡

  • Redis만 띄우면 별도 인프라 없이 재시도, 동시성 제어, 크론 잡까지 다 됩니다.
    프로덕션에서 검증된 패턴이니 사이드 프로젝트부터 적용해보세요.
    > 📌 [BullMQ 공식 문서](https://docs.bullmq.io)
    💬 0
    👁 0 views

    Comments (0)

    💬

    No comments yet.

    Be the first to comment!