In this lesson, you will learn how to use Redis as a shared memory layer to reliably track the lifecycle of asynchronous webhook callbacks across a distributed Node.js architecture.
The Challenge of Asynchronous Webhooks
When an external service (like a payment provider) sends a webhook, your server receives it, acknowledges the HTTP request immediately, and then processes the data. In a clustered environment, the process that receives the webhook might not be the same one that initiated the original request. If you store the state of that request in local memory, it becomes invisible to other processes, leading to lost events or "request not found" errors.
Using Redis as the "Source of Truth"
Redis acts as a shared, high-performance memory layer that all your Node.js instances can access. When you initiate an external operation (like a customer checkout), you generate a unique correlation_id and store an entry in Redis representing that transaction's state.
When the webhook eventually hits your server, your Node.js application uses that correlation_id to query Redis. Because Redis exists outside the lifecycle of any single process, your app can verify if the webhook is expected, update the state, and trigger the next business logic step regardless of which instance handled the incoming HTTP request.
Basic Implementation Pattern
Here is how you might track a pending request before the webhook arrives:
// Step 1: Initialize state when starting the task
await redis.set(`webhook_task:${orderId}`, 'pending', 'EX', 3600);
// Step 2: Handle the webhook callback
app.post('/webhook', async (req, res) => {
const { orderId } = req.body;
const status = await redis.get(`webhook_task:${orderId}`);
if (status === 'pending') {
await redis.set(`webhook_task:${orderId}`, 'completed');
res.status(200).send('Webhook processed');
} else {
res.status(404).send('Task not found or already processed');
}
});
By offloading this state to Redis, you decouple your business logic from the specific memory space of a single server process, allowing your webhooks to scale seamlessly across multiple instances.
Key Takeaway: Redis provides a centralized, shared memory layer that ensures webhook state remains persistent and accessible across all nodes in a distributed environment.