Skip to content

πŸš€ feat(queue): implement BullMQ + Redis for async event & webhook processingΒ #19

@abhishek-nexgen-dev

Description

@abhishek-nexgen-dev

Introduce a production-grade queue system using BullMQ + Redis to handle all asynchronous workloads in CommDesk:

  • Event processing
  • Webhook delivery
  • Retry handling
  • Background jobs

This replaces synchronous execution with a scalable, fault-tolerant pipeline.


🎯 Problem

Current (sync):

Event β†’ Webhook β†’ Response

Issues:

  • API latency & timeouts
  • No retry mechanism
  • Poor scalability
  • Failure = data loss risk

βœ… Proposed Solution

Event
β†’ Queue (BullMQ)
β†’ Redis
β†’ Worker
β†’ Delivery Engine
β†’ Logs + Retry

🧱 Tech Stack

  • Queue: BullMQ (open-source, production-proven)
  • Broker: Redis
  • Runtime: Node.js + Express

πŸ“¦ Scope

1️⃣ Queue Setup

Path:

src/queue/

Files:

connection.ts
queues.ts
jobs.ts

Example:

import { Queue } from "bullmq";

export const webhookQueue = new Queue("webhook", {
  connection: {
    host: process.env.REDIS_HOST,
    port: Number(process.env.REDIS_PORT),
  },
});

2️⃣ Job Types

  • WEBHOOK_DELIVERY
  • EVENT_PROCESSING
  • RETRY_DELIVERY

3️⃣ Worker Implementation

Path:

src/workers/

Files:

webhook.worker.ts
event.worker.ts

Responsibilities:

Fetch job
β†’ Load event
β†’ Find matching webhooks
β†’ Trigger delivery
β†’ Log result
β†’ Retry if needed

4️⃣ Job Payload

{
  eventId: string,
  webhookId: string,
  attempt: number
}

5️⃣ Retry Strategy (Required)

1 β†’ immediate
2 β†’ 30 sec
3 β†’ 2 min
4 β†’ 10 min
5 β†’ fail β†’ Dead Letter Queue

6️⃣ Dead Letter Queue

  • Store permanently failed jobs
  • Support manual retry via API

7️⃣ Rate Limiting

  • Per webhook β†’ 10 req/sec
  • Per community β†’ configurable

8️⃣ Concurrency

concurrency: 10

9️⃣ Logging Integration (MANDATORY)

Must integrate with logging system defined in docs:

πŸ‘‰

Log events:

  • job queued
  • job started
  • job completed
  • job failed
  • retry triggered

πŸ”Ÿ Failure Handling

Handle:

  • timeouts
  • network failures
  • invalid webhook URLs
  • external API errors

πŸ“Š Observability

Track:

  • queue size
  • jobs/sec
  • failure rate
  • retry count

Alert on:

  • high failure rate
  • queue backlog spike
  • worker crash

πŸ“ Folder Structure

src/
 β”œβ”€β”€ queue/
 β”œβ”€β”€ workers/
 β”œβ”€β”€ modules/
 β”œβ”€β”€ logs/

πŸ§ͺ Testing

  • job enqueue works
  • worker executes jobs
  • retry logic triggers correctly
  • failure scenarios handled

🧨 Edge Cases

  • duplicate jobs
  • Redis downtime
  • worker crash
  • retry storms

⚑ Performance Targets

  • Handle 10k+ jobs/min
  • Non-blocking API
  • Horizontally scalable workers

🌍 Environment

docker run -d -p 6379:6379 redis
REDIS_HOST=localhost
REDIS_PORT=6379

βœ… Acceptance Criteria

  • Queue integrated with event system
  • Workers process jobs correctly
  • Retry + backoff implemented
  • Dead Letter Queue implemented
  • Logs captured for all job states
  • Rate limiting enforced
  • System resilient to failures

🚫 Constraints

  • Do NOT process webhooks synchronously
  • Do NOT implement custom queue logic
  • Use BullMQ best practices only

πŸ”₯ Impact

After implementation:

Scalable βœ”
Reliable βœ”
Production-ready βœ”

Without this:

System fails under load ❌

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type
    No fields configured for issues without a type.

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions