DEV Community

Cover image for Handling Eventual Consistency in Webhook
Sibelius Seraphini for Woovi

Posted on

15

Handling Eventual Consistency in Webhook

What is eventual consistency?

Eventual consistency is a consistency model used in distributed computing to achieve high availability and fault tolerance. It ensures that, given enough time and the absence of new updates, all copies of a data item will converge to the same value.

This usually happens when you have more than one node for your database, when one acts as a primary and the other is the secondary. Like the below image.

MongoDB Replicaset

You write to the primary instance and read from the secondary instance to scale reads. The problem is that you can read data that is as fresh as in the primary until the data is fully replicated.
This is a common tradeoff when working in a distributed system.

Eventual consistent at Webhook

Some applications can send a webhook before the replication happens to all nodes of the database, and if the webhook payload does not have all the data that you need, you need to perform a GET in an endpoint to get the missing data. The pattern as below:

webhook pattern

When doing a GET you can receive a 400 or 404 that the resource you are trying to GET does not exist, even if you already received the webhook with the given resource. This happens because of eventual consistency.

One solution to this is to retry this GET in an async job after some time, until the replication is done. The code below is a simple implementation in node using bulljs.

import Queue from 'bull';

const queue = new Queue('WEBHOOK');

const result = await resourceGet(webhook.resource.id);

if (result.error) {
  await queue.add(
    'PROCESS_WEBHOOK',
    { webhook },
    {
      attempts: 7,
      backoff: {
        type: 'exponential',
        delay: 10000,
      },
    }
  );

  return;
}
Enter fullscreen mode Exit fullscreen mode

In Conclusion

Eventual consistency is a common topic of distributed systems that will affect your internal system and also your interaction with external systems.
Understanding why an error happens in production using observability tools is important to provide the best solution for your specific use case.


Woovi is an innovative startup revolutionizing the payment landscape. With Woovi, shoppers can enjoy the freedom to pay however they prefer. Our cutting-edge platform provides instant payment solutions, empowering merchants to accept orders and enhance their customer experience seamlessly.

If you're interested in joining our team, we're hiring! Check out our job openings at Woovi Careers.


Image by freepik

Image of Docusign

Bring your solution into Docusign. Reach over 1.6M customers.

Docusign is now extensible. Overcome challenges with disconnected products and inaccessible data by bringing your solutions into Docusign and publishing to 1.6M customers in the App Center.

Learn more

Top comments (0)

A Workflow Copilot. Tailored to You.

Pieces.app image

Our desktop app, with its intelligent copilot, streamlines coding by generating snippets, extracting code from screenshots, and accelerating problem-solving.

Read the docs

👋 Kindness is contagious

Engage with a sea of insights in this enlightening article, highly esteemed within the encouraging DEV Community. Programmers of every skill level are invited to participate and enrich our shared knowledge.

A simple "thank you" can uplift someone's spirits. Express your appreciation in the comments section!

On DEV, sharing knowledge smooths our journey and strengthens our community bonds. Found this useful? A brief thank you to the author can mean a lot.

Okay