DEV Community

MATT ROSE
MATT ROSE

Posted on

The Catch and Release Pattern: Handling High-Volume Webhooks in Node.js

If you are building an API that integrates with third-party vendors, you will eventually face the webhook flood.

When an external service sends a massive spike of webhook events, the standard approach of processing the data and inserting it into a database synchronously will block the Node.js event loop. Your API will time out, the vendor will assume the delivery failed, and you will drop critical data.

To survive unpredictable traffic spikes, you need to decouple the HTTP response from the data processing. Here is how to implement the "Catch and Release" pattern using Node.js, Express, and BullMQ.

Prerequisites

  • Node.js and Express installed.
  • A running instance of Redis (required for BullMQ).
  • Basic understanding of asynchronous JavaScript.

The Synchronous Trap (What Not to Do)

Most developers write their first webhook receiver like this:

app.post('/webhook/inventory', async (req, res) => {
  const payload = req.body;

  try {
    // ❌ Anti-pattern: Heavy processing before responding
    const normalizedData = heavyDataTransformation(payload);
    await database.insert(normalizedData);

    // Vendor waits for the database to finish...
    res.status(200).send('Success');
  } catch (error) {
    res.status(500).send('Failed');
  }
});
Enter fullscreen mode Exit fullscreen mode

The problem: If the vendor sends 500 webhooks a second and your database takes 200ms to insert a record, the database connection pool will max out. Requests will queue up, memory will spike, and the connection will close. The data is gone forever.

Step 1: Implementing "Catch and Release"

The golden rule of webhook ingestion is to acknowledge receipt immediately. We want to return a 200 OK or 202 Accepted status back to the vendor before we do any heavy lifting.

To do this safely without losing the data in memory if the server crashes, we push the raw payload to a persistent background queue.

First, install BullMQ and Redis:

npm install bullmq ioredis
Enter fullscreen mode Exit fullscreen mode

Next, configure the queue:

import { Queue } from 'bullmq';
import Redis from 'ioredis';

// Connect to Redis
const redisConnection = new Redis(process.env.REDIS_URL);

// Create the ingestion queue
const webhookQueue = new Queue('webhook-ingestion', { 
  connection: redisConnection 
});
Enter fullscreen mode Exit fullscreen mode

Now, rewrite the Express route to catch the payload, queue it, and release the connection:

app.post('/webhook/inventory', async (req, res) => {
  const payload = req.body;

  try {
    // 1. Push raw data to Redis immediately
    await webhookQueue.add('process-inventory', payload, {
      attempts: 3,
      backoff: { type: 'exponential', delay: 1000 }
    });

    // 2. Release the vendor connection instantly
    return res.status(202).send('Accepted for processing');

  } catch (error) {
    console.error('Failed to queue webhook', error);
    return res.status(500).send('Internal Server Error');
  }
});
Enter fullscreen mode Exit fullscreen mode

With this pattern, your Express server can handle thousands of requests per second. The route does nothing but write JSON to Redis, which is incredibly fast.

Step 2: Processing the Queue Safely

Now that the data is safely persisted in Redis, we can process it at our own pace using a BullMQ Worker. This worker runs on a separate thread (or an entirely separate server) so it never blocks our Express API.

import { Worker } from 'bullmq';

const worker = new Worker('webhook-ingestion', async job => {
  const payload = job.data;

  // Now we can safely perform heavy processing
  const normalizedData = heavyDataTransformation(payload);

  // If the database is locked, it throws an error, 
  // and BullMQ automatically retries based on our backoff strategy.
  await database.insert(normalizedData);

}, { connection: redisConnection });

worker.on('completed', job => {
  console.log(`Job ${job.id} processed successfully`);
});

worker.on('failed', (job, err) => {
  console.error(`Job ${job.id} failed:`, err);
});
Enter fullscreen mode Exit fullscreen mode

Conclusion

By implementing the Catch and Release pattern, you separate the HTTP transport layer from your business logic.

  1. Express acts purely as a lightning-fast catcher's mitt.
  2. Redis/BullMQ acts as the shock absorber, holding the data safely.
  3. The Worker acts as the engine, processing data only as fast as your database can handle it.

This architecture ensures zero data loss, prevents database exhaustion, and keeps external vendors happy with lightning-fast response times.

Top comments (0)